Unlock JVM Performance: A Practical Guide to Enabling Large Pages on Linux and Windows
This article explains what large (huge) pages are, why they improve JVM performance, and provides step‑by‑step instructions for configuring Transparent Huge Pages and HugeTLB pages on Linux as well as enabling large pages on Windows, including verification techniques.
1. Introduction
I recently spent a lot of time on JVM memory reservation code after receiving an external contribution to support multiple large‑page sizes on Linux. While refactoring, I realized a concise summary of JVM large pages would make an interesting article.
2. What are large pages?
Large pages (also called huge pages) reduce pressure on the processor TLB cache, which speeds up virtual‑to‑physical address translation. Most architectures support multiple page sizes, typically 4 KB base pages. For memory‑intensive applications like large Java heaps, using larger page granularity improves TLB hit rates. On x86‑64, 2 MB and 1 GB pages are available and can have a significant impact on memory‑bound workloads.
Benchmarks such as SPECjbb® 1 show impressive gains when the high‑performance JVM enables large pages.
3. Enabling large pages
The generic JVM switch is -XX:+UseLargePages, but the operating system must be configured correctly.
3.1 Linux
JVM can use large pages via Transparent Huge Pages (THP) or HugeTLB pages, each with different configuration and performance characteristics.
3.1.1 Transparent Huge Pages (THP)
THP simplifies large‑page usage on Linux. It can be set to three modes: always – all applications automatically use THP. madvise – THP is used only when the application marks memory with madvise() and the MADV_HUGEPAGE flag. never – THP is never used.
The mode is configured in /sys/kernel/mm/transparent_hugepage/enabled:
echo "madvise" > /sys/kernel/mm/transparent_hugepage/enabledJVM can request THP with -XX:+UseTransparentHugePages. Sufficient contiguous physical memory is required; otherwise the kernel will attempt defragmentation, configurable via /sys/kernel/mm/transparent_hugepage/defrag.
3.1.2 HugeTLB pages
HugeTLB pages are pre‑allocated by the OS. Applications reserve them with mmap() using the MAP_HUGETLB flag. Enable them for the JVM with -XX:+UseLargePages or -XX:+UseHugeTLBFS.
When using HugeTLB, the JVM reserves the entire memory range supported by large pages, so the required number of pages must be pre‑allocated.
Check available page sizes: ls /sys/kernel/mm/hugepages/ Allocate a specific number of 2 MB pages, for example:
echo 2500 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepagesVerify the actual number in nr_hugepages before proceeding.
View the system’s default large‑page size: cat /proc/meminfo | grep Hugepagesize To use a different size, set the JVM flag -XX:LargePageSizeInBytes, e.g., -XX:LargePageSizeInBytes=1g for 1 GB pages.
3.1.3 Which to use
THP is easier to set up, while HugeTLB offers more control. Choose THP for lower memory consumption and when latency is less critical; choose HugeTLB if you need deterministic allocation without waiting for the OS to free contiguous memory.
3.2 Windows
On Windows, the user running the JVM must have the "Lock pages in memory" privilege. Enable it via gpedit.msc under Local Security Policy → User Rights Assignment → Lock pages in memory, then reboot.
After granting the privilege, the JVM can use large pages with -XX:+UseLargePages. The implementation mirrors Linux HugeTLB pages, pre‑allocating the entire reserved range.
A recent bug prevented G1 GC from using large pages for heaps larger than 4 GB; the bug is now fixed, and enabling large pages can noticeably improve performance for large Minecraft servers.
4. Verifying JVM large‑page usage
After configuring the environment and enabling large pages at runtime, verify usage with JVM logging options. Use -Xlog:gc+init to see basic GC configuration:
jdk-16/bin/java -Xlog:gc+init -XX:+UseLargePages -Xmx4g -versionSample output shows "Large Page Support: Enabled (Explicit)" for HugeTLB or "(Transparent)" for THP.
For detailed page‑size information, enable -Xlog:pagesize:
[0.002s][info][pagesize] Heap: min=8M max=4G base=0x0000000700000000 page_size=2M size=4GThis detailed log helps confirm which JVM components are backed by large pages.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
