Introduction / Why This Is Needed
Huge pages are a Linux mechanism that allows using memory pages of 2 MB or 1 GB in size instead of the standard 4 KB. This reduces pressure on the TLB (Translation Lookaside Buffer) and decreases the number of page faults, which is particularly beneficial for memory-intensive applications such as databases (Oracle, MySQL), virtualization systems (KVM), and high-performance computing. In this guide, you will learn how to configure both standard huge pages and transparent huge pages (THP) in Linux to optimize performance.
Requirements / Preparation
Before you begin, ensure that:
- You have access to a Linux system with superuser privileges (root or via sudo).
- You are familiar with basic terminal commands and file editing.
- The utilities
grep,sysctl,grub2-mkconfig(for GRUB2) orupdate-grub(for Ubuntu/Debian) are installed. - Your application supports huge pages (for example, Oracle Database requires explicit configuration
use_large_pages=TRUE). - The system has enough free memory to reserve huge pages (each 2 MB page is reserved at boot).
Step 1: Check Current Huge Pages Configuration
First, determine which huge pages are already configured. Run:
grep -i huge /proc/meminfo
Pay attention to the fields:
HugePages_Total: total number of reserved huge pages.HugePages_Free: number of free huge pages.Hugepagesize: size of one huge page (usually 2048 kB for 2 MB).
For transparent huge pages (THP), check:
cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag
The output will look like:
[always] madvise never
where the current mode is highlighted in square brackets. always means THP are always used, madvise means only upon application request, and never means they are disabled.
Step 2: Determine the Required Number of Huge Pages
Calculate how many huge pages your application needs. For example, if the application requires 16 GB of memory and the huge page size is 2 MB:
Count = (16 * 1024 MB) / 2 MB = 8192 pages.
Keep in mind that huge pages are reserved in physical memory and are unavailable for other processes. Allocate with a margin, but not too much, to avoid exhausting memory. For applications that do not use all memory simultaneously, you can allocate less.
Step 3: Configure Standard Huge Pages via sysctl
For temporary configuration (effective until reboot), use sysctl:
sudo sysctl vm.nr_hugepages=8192
To make the configuration permanent, edit /etc/sysctl.conf:
vm.nr_hugepages=8192
Apply the changes:
sudo sysctl -p
However, sysctl applies the setting after system boot, and huge pages will only be allocated if there are enough contiguous free memory blocks. For guaranteed reservation at boot, it is better to use a kernel parameter in GRUB.
Step 4: Configure via GRUB (Recommended)
Edit the GRUB configuration:
sudo nano /etc/default/grub
Find the line starting with GRUB_CMDLINE_LINUX and add hugepages=8192. For example:
GRUB_CMDLINE_LINUX="quiet splash hugepages=8192"
If you need to use 1 GB huge pages (requires kernel support and configuration), also add hugepagesz=1G:
GRUB_CMDLINE_LINUX="quiet splash hugepages=8192 hugepagesz=1G"
After making changes, update the GRUB configuration:
For Ubuntu/Debian:
sudo update-grub
For RHEL/CentOS:
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
Then reboot the system so that huge pages are reserved at boot.
⚠️ Important: Ensure that the allocated number of huge pages does not exceed available physical memory. It is recommended to allocate no more than 50% of total memory to leave room for the OS and other processes.
Step 5: Configure Transparent Huge Pages (THP)
Transparent huge pages are managed automatically by the kernel, but you can change their behavior. To disable THP or set it to madvise mode (recommended for many applications), run:
Temporarily (until reboot):
echo madvise | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
For permanent configuration, create a systemd service. Create the file /etc/systemd/system/disable-thp.service:
[Unit]
Description=Disable Transparent Huge Pages
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/sh -c "echo madvise > /sys/kernel/mm/transparent_hugepage/enabled"
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable disable-thp.service
sudo systemctl start disable-thp.service
Alternatively, you can add it to /etc/rc.local, but systemd is preferable.
💡 Tip:
madvisemode is often optimal for THP because it allows the application to explicitly request huge pages viamadvise(MADV_HUGEPAGE), avoiding random latency from automatic management.
Step 6: Reboot the System
After configuring via GRUB or to apply THP changes, reboot the system:
sudo reboot
After reboot, check the configuration as described in Step 1.
Verification
After reboot, ensure that huge pages are configured correctly:
- Check standard huge pages:
grep -i huge /proc/meminfoHugePages_Totalshould match the set value, andHugePages_Freeshould be close to it (if huge pages are not used by applications). - Check THP:
cat /sys/kernel/mm/transparent_hugepage/enabled
It should display the selected mode (e.g.,madvise). - For specific applications:
- Oracle Database: the query
SELECT * FROM V$SGASTAT WHERE NAME LIKE '%Huge%';should show huge page usage. - MySQL (InnoDB): look for "Huge pages" in
SHOW ENGINE INNODB STATUSor check/proc/<pid>/smapsfor "huge" entries.
- Oracle Database: the query
If the values match expectations, the configuration is successful.
Possible Issues
- System fails to boot after adding hugepages to GRUB: This can happen if too many huge pages are allocated and there is insufficient memory at boot. Boot into recovery mode or using a Live CD, edit
/etc/default/grub, reduce thehugepagesvalue, then update GRUB. - Huge pages are not reserved after reboot: Ensure the
hugepagesparameter is correctly specified in GRUB and that the kernel supports huge pages (checkgrep CONFIG_HUGETLBFS /boot/config-$(uname -r)). Also verify there is enough free memory at boot. - Application does not use huge pages: For Oracle, ensure
use_large_pages=TRUEis set inspfileand the SGA size matches the huge pages. For other applications, consult the documentation for huge page configuration. - Transparent huge pages cause latency or performance drops: Some applications perform better with THP disabled. Experiment with
always,madvise,nevermodes.madvisemode is often a good compromise. - Errors when writing to /sys/kernel/mm/transparent_hugepage/enabled: Ensure you run the command as root and that the file exists (on very old kernels THP may be absent). In that case, update the kernel.
If problems persist, check kernel logs (dmesg | grep huge) for errors.