Profile Image
vishnupriya

What is JVM startup parameter: -XX:AllocatePrefetchDistance?

What is JVM startup parameter: -XX:AllocatePrefetchDistance? 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-allocateprefetchdistance

  • x-allocateprefetchdistance

  • JVM startup parameter

Please Sign In or to post your comment or answer

Profile Image

Pavel Khodakovsky

Usage:


 This flag allows instruct the JVM to set a size in bytes of the prefetch distance allocated by the object.-XX:AllocatePrefetchDistance=size, where size is the size of prefetch distance in bytes

 

Since:

 

Starting from JDK 6.

 

Examples:

 

This will instruct JVM to set 2048 bytes as the prefetch distance:

 

-XX:AllocatePrefetchDistance=2048

 

Description:

 

Before talking about the flag, we need to understand these topics, Java Heap, Allocations, Prefetching, and Thread Local Allocation Buffers (TLABs).

 

The Java heap is a memory area managed by the garbage collector, so it is also called the "GC heap" in some sources. Before the appearance of the G1 collector, all garbage collectors inside the HotSpot virtual machine were designed based on "classical generation", so the Java heap is divided into "Young generation", "Old or Tenured Generation" and "Permanent Generation.

 

The Java heap which is shared by all threads can be split up into multiple Thread Local Allocation Buffer (TLAB) to improve the object allocation’s efficiency. Their main purpose is to store instances of objects and subdividing the Java heap is just to reclaim or allocate memory faster.

 

Usually, objects are allocated on the heap, because the heap is shared by threads, so many threads may apply for space allocation at the same time. If a there is a lock, it will reduce the allocation efficiency. Fortunately, JVM introduced Thread Local Allocation Buffers (TLABs) which is a thread-specific memory allocation area. When using the argument “-XX:UseTLAB”, each thread is initialized with a separate size of memory so that each thread has it’s own space. If you need to allocate memory, you can use it yourself in this way, there is no competition, which can greatly improve the allocation efficiency.

 

 JVM automatically manages the Heap memory which is very important for the performance of an application. When a program tries to allocate more memory on the Heap than that is freely available (depending on the “Xmx” config) we encounter “OutOfMemoryError”.

 

JVM manages the heap memory by garbage collection. In simple terms, it frees the memory used by orphan objects, i.e, objects that are no longer referenced from the Stack directly or indirectly (via a reference in another object) to make space for new object creation.

 

Let us take a close look at this code snippet:

 

class Student {

 

    private String firstName;

 

    private String lastName;

 

    private Integer score;

 

    public Student(String firstName, String lastName, Integer score) {

 

        this.firstName = firstName;

 

        this.lastName = lastName;

 

        this.score = score;

 

    }

 

    public void setScore(Integer score) {

 

        this.score = score;

 

    }

 

}

 

public class Exam {

 

    private final static int EXAM_BONUS = 5;

 

    private static Integer calculateExamBonus(Integer score) {

 

        int finalScore = score + 5;

 

        return finalScore > 100 ? score : finalScore;

 

    }

 

    public static void main(String[] args) {

 

        Integer score = 90;

 

        Student student = new Student("John", "Doe", score);

 

        student.setScore(calculateExamBonus(score));

 

    }

 

}

 

When we initiate a new Student instance, it will create the instance in the heap of the program and the three fields in the constructor will also create an instance of each type in the heap memory as you can see below in the image. But the static fields are not stored in the heap memory but the thread stack.

As we discussed before, the JVM is responsible for the memory management. Therefore, it has control over the instructions and its parameters. JVM can know if a given instruction is accessing to an object, what properties the object have such as type, size and addresses and the position in the memory of the object. Given all these information, the JVM can get a pattern of accessing to the object. This access pattern might be used to predict the next pages of the application consequently prefetch them.

 

The JVM introduced arguments which are related to prefetching. Such as AllocatePrefetchDistance. This parameter allows the JVM to set a size in bytes of the prefetch distance for object allocation. When a new object is created, it’s written to the memory, this flag will prefetch memories starting from the distance given in the arguments of the parameter and the address of the last allocated object. Each thread owns an allocation point.

 

The size can be a negative, which will mean that the platform will choose the prefetch distance. You can also specify a data unit such as “k” or “K” for kilobytes, “m” or “M” for Megabytes and “g” or “G” for Gigabytes.

 

  • For using 512 Kilobytes: -XX:AllocatePrefetchDistance=512k
  • For using 128 Megabytes: -XX:AllocatePrefetchDistance=128m
  • For using 12 Gigabytes: -XX:AllocatePrefetchDistance=12g

 

Default Value:

 

The default value is -1.

 

Note:

 

This flag only supported by Java HotSpot Server VM.

 

Arguments related:

 

AllocatePrefetchInstr, AllocateInstancePrefetchLines, AllocatePrefetchLines, AllocatePrefetchStepSize, AllocatePrefetchStyle

 

Related Posts:

 

 

 

 

 

Got something else on mind? Post Your Question

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

  • xx-allocateprefetchdistance

  • x-allocateprefetchdistance

  • JVM startup parameter