Profile Image
vishnupriya

What is JVM startup parameter: -XX:+UseTransparentHugePages?

What is JVM startup parameter: -XX:+UseTransparentHugePages? Have you used this JVM arguement before? What are the pros & cons of using this Java argument? Can you share your perspective/experience in using this JVM argument?

  • jvmargument

  • xx-usetransparenthugepages

  • x-usetransparenthugepages

  • JVM startup parameter

Please Sign In or to post your comment or answer

Profile Image

Pavel Khodakovsky

Usage


 This flag allows the JVM to use Transparent Huge Pages (THP) dynamically.

 

Since:

 

Starting from JDK 6.

 

Deprecated:

 

Obsoleted in JDK12.

 

Expired in JDK13.

 

Examples:

 

This will enable the use of Transparent Huge Pages:

 

-XX:+UseTransparentHugePages

 

Description

 

Lately, few programmers manipulate physical memory directly. In contrast, each process has its own virtual address space, which is mapped to actual physical memory. This feature allows two processes to have different data at the same virtual address because the it is mapped to different physical addresses. So, when a program accesses a virtual address, a component will translate the virtual address to a physical address.

 

This process is generally implemented by "Page Table" maintained by the operating system and the hardware performs address translation by "Traversing Page Table". Although it is easier to do address translation in units of pages, there is a lot of overhead due to the translation that occurs every time the memory is accessed. To this end, a Translation Lookup Buffer (TLB) is introduced to cache the most recent transition records. It is required to be at least as fast as the L1 cache, so there are usually fewer than 100 caches. For heavy workloads, TLB misses and the resulting page table walks can take a lot of time.

 

While we can't make the TLB bigger, we can make the page bigger. Most hardware supports base pages of 4K size, and "large pages" of 2M/4M/1G. Having larger pages can shrink the page table, making it cheaper to move the page table.

 

There are at least two ways to implement larger pages in Linux, “hugetlbfs” and “Transparent Huge Pages (THP)”. We will be focusing only on “Transparent Huge Pages”. What is a Transparent Huge Page?

 

Transparent Huge Pages is an abstraction layer which main purpose is to create, manage and using huge pages, it’s transparent to the application, and the application can allocate memory as normal. Ideally, the application does not need to make any changes. But in fact, there is a space cost (because the entire huge page is also allocated for some small objects) and time cost (because sometimes THP needs to defragment memory). Fortunately, there is a compromise: applications “canmadvise(2)” tell Linux where to use THP.


The JVM introduced multiple arguments to manipulate large pages, such as the argument “UseTransparentHugePages” which instruct the JVM when it is initialized to memory map the Java heap to hugetlbfs of the operating system.


Let us do some benchmarking, with this code snippet:

 

@State(Scope.Benchmark)

 

@BenchmarkMode(Mode.Throughput)

 

@Warmup(iterations = 3, time = 1)

 

@OutputTimeUnit(TimeUnit.NANOSECONDS)

 

@Measurement(iterations = 3, time = 1)

 

@Fork(value = 3, jvmArgsAppend = {"-Xmx1g", "-Xms1g"})

 

public class UseTransparentHugePagesBenchmark {

 

    @Param({"1000",

 

            "10000",

 

            "1000000",

 

            "10000000"})

 

    int size;

 

    List<String> strings;

 

    @Setup

 

    public void setup() {

 

        strings = initData();

 

    }

 

    @Benchmark

 

    public String test() {

 

        return strings.get(ThreadLocalRandom.current().nextInt(size));

 

    }

 

    private List<String> initData() {

 

        List<String> data = new ArrayList<>();

 

        for (int i = 0; i < size; i++) {

 

            data.add(String.format("Name: %d", i));

 

        }

 

        return data;

 

    }

 

}


We will run it using JMH (a library to run benchmarks in java) with and without the argument “UseTransparentHugePages” and see the difference.

  • Without the flag “UseTransparentHugePages”, we have these results:

  • With the flag “UseTransparentHugePages”, we have the following results:

We can notice that when we are using the flag, there are few errors than using cache. That’s why we can see that the performance of the cache (when not using the flag) is reducing as we increase the size of the list.

 

Default Value:

 

By default, this flag is disabled.

 

Error:

 

An error is thrown if the JDK is not supporting the flag.

 

Note:

 

This flag only works on Unix based Operating System.

 

Arguments related:

 

UseHugeTLBFS, UseLargePages

 

Related Posts:

 

 

Got something else on mind? Post Your Question

Not the answer you're looking for? Browse other questions tagged
  • jvmargument

  • xx-usetransparenthugepages

  • x-usetransparenthugepages

  • JVM startup parameter