In computing, a page is a unit of memory. The size of a page depends on the operating system and hardware. In Linux, a page is usually 4096 bytes (4 KB). However, in some cases pages can be up to 16384 bytes (16 KB) in size. This larger size is called a “hugepage.” In this blog post, we will discuss what hugepages are and how to use them in Linux!
understanding hugepages in Linux
Hugepages are a performance feature in Linux that allows the operating system to use larger pages. This can improve performance by reducing the number of page table entries (PTEs) that need to be created, and by allowing the kernel to better utilize CPU caches. Hugepages can also reduce memory fragmentation and improve virtual memory address space utilization.
To use hugepages in Linux, we need to first enable the feature in the kernel. Once we have enabled hugepages in the kernel, we can begin configuring them for our applications.
Defines the number of persistent huge pages configured in the kernel at boot time. The default value is 0. It is only possible to allocate (or deallocate) huge pages if there are sufficient physically contiguous free pages in the system. Pages reserved by this parameter cannot be used for other purposes.
Default size huge pages can be dynamically allocated or deallocated by changing the value of the /proc/sys/vm/nr_hugepages file.
For more information, read the relevant kernel documentation, which is installed in /usr/share/doc/kernel-doc-kernel_version/Documentation/vm/hugetlbpage.txt by default. This documentation is available only if the kernel-doc package is installed.
Default hugepagesz size in Linux
Defines the size of persistent huge pages configured in the kernel at boot time. Valid values are 2 MB and 1 GB. The default value is 2 MB.
check hugepage size from /proc/meminfo in Linux
To check the size of a hugepage, we can use the /proc/meminfo file. This file contains information about the memory usage on our system. We can use grep to search for the “Page size” field, as shown below:
To check the size of a hugepage, we need to look for the “HugePages_Total” field. This field will tell us the total number of hugepages that are available on our system.
If we want to use more than one hugepage, we can change the “nr_hugepages” kernel parameter. This parameter defines the maximum number of hugepages that can be used on the system.
Once we have made this change, we need to reboot our system for the changes to take effect. After rebooting, we can check the /proc/meminfo file again to see that our changes have been applied.
Best practice hugepages in Linux
There are a few things to keep in mind when using hugepages. First,hugepages are not automatically allocated or deallocated by the kernel. This means that we need to explicitly allocate hugepages for our applications before they can use them.
Second,hugepages are allocated on a per-application basis. This means that each application that wants to use hugepages must allocate its own pages. Finally,hugepages are not swapped out by the kernel like regular pages. This means that we need to be careful not to allocate more hugepages than we have physical memory available.
Overall, hugepages can be a great way to improve performance on Linux systems. They can reduce memory fragmentation and increase virtual memory address space utilization. However, we need to be careful when using them to avoid running out of physical memory.
What do you think? Have you ever used hugepages in Linux? Let us know in the comments below!