// // HugePages in Linux

A technical troubleshooting blog about Oracle with other Databases & Cloud Technologies.

HugePages in Linux

3 min read
Computer memory is managed in blocks of storage known as pages, which have a predetermined size that depends on the computer’s architecture. These pages are organized using a page table, which is a data structure that links virtual memory addresses to their physical location in memory.

As companies store more and more information in their enterprise databases, using large amounts of memory becomes slower and more inefficient as the default memory page size remains the same.

To address this issue, several flavors of the Linux operating system have implemented a feature known as HugePages.
What are HugePages and Why Do They Matter?

" HugePages are the capability of the Linux administrator to configure the operating system to use 2 MB page sizes. The larger memory pages mean the operating system can manage less pages and access memory pages faster thus, optimizing memory management ". 

HugePages are a feature in the Linux operating system that allow the Linux kernel to manage large pages of memory.

HugePages enable you to dramatically reduce your page table overhead, especially when dealing with large System Global Areas (SGAs). 
When Should You Use HugePages?

According to Oracle, you should enable the HugePages feature if you have a system with more than 16 gigabytes of memory running Oracle databases with a total SGA larger than 8 gigabytes. Realistically, however, any database with an SGA size of 100 megabytes or more can benefit from using HugePages.

HugePages are a “use it or lose it” feature, so you should make sure that they aren’t going to waste in your Oracle environment. 
To enable the Hugepages, complete the following steps:
Run this command to determine if the kernel supports HugePages:

$ grep Huge /proc/meminfo

AnonHugePages: 0 kB
ShmemHugePages: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
 Hugetlb:               0 kB
Edit memlock setting in /etc/security/limits.conf. The memlock setting is specified in KB, and the maximum locked memory limit should be set to at least 90 percent of the current RAM when HugePages memory is enabled. For example, if the server RAM size is 128GB, the memlock can be set 
128GB * 90% = 120,795,955 KB and add the following lines into this file:

*    soft   memlock    120,795,955
 *   hard   memlock    120,795,955
Log in as oracle user again and run the ulimit -l command to verify the new memlock setting:
    $ ulimit -l
      120795955
Get the HugePage size by running this command:

$ grep Hugepagesize  /proc/meminfo
Create a script hugepages_settings.sh that computes recommended values for hugepages configuration for the current shared memory segments:

 !/bin/bash
#
# hugepages_settings.sh
#
# Linux bash script to compute values for the
# recommended HugePages/HugeTLB configuration
#
# Note: This script does calculation for all shared memory
# segments available when the script is run, no matter it
# is an Oracle RDBMS shared memory segment or not.
# Check for the kernel version
KERN=`uname -r | awk -F. '{ printf("%d.%d\n",$1,$2); }'`
# Find out the HugePage size
HPG_SZ=`grep Hugepagesize /proc/meminfo | awk {'print $2'}`
# Start from 1 pages to be on the safe side and guarantee 1 free HugePage
NUM_PG=1
# Cumulative number of pages required to handle the running shared memory segments
for SEG_BYTES in `ipcs -m | awk {'print $5'} | grep "[0-9][0-9]*"`
do
MIN_PG=`echo "$SEG_BYTES/($HPG_SZ*1024)" | bc -q`
if [ $MIN_PG -gt 0 ]; then
NUM_PG=`echo "$NUM_PG+$MIN_PG+1" | bc -q`
fi
done
# Finish with results
case $KERN in
'2.4') HUGETLB_POOL=`echo "$NUM_PG*$HPG_SZ/1024" | bc -q`;
echo "Recommended setting: vm.hugetlb_pool = $HUGETLB_POOL" ;;
'2.6'|'3.8') echo "Recommended setting: vm.nr_hugepages = $NUM_PG" ;;
*) echo "Unrecognized kernel version $KERN. Exiting." ;;
esac
 # End
Provide the execute permission of this script And execute this script to values for HugePages configuration:

$chmod +x hugepages_settings.sh

$./hugepages_settings.sh
Set the following kernel parameter, where value is the HugePages value that you determined in above step

# sysctl -w vm.nr_hugepages=value

Check the available hugepages:

 $grep Huge /proc/meminfo
Restart the database instance and run the command to check the available HugePages


 $grep Huge /proc/meminfo