Profile Image
vishnupriya

What is JVM startup parameter: -XX:AllocateInstancePrefetchLines?

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

  • x-allocateinstanceprefetchlines

  • JVM startup parameter

Please Sign In or to post your comment or answer

Profile Image

Pavel Khodakovsky

Usage:


 This flag allows the JVM to set a number of lines to prefetch before of the instance allocation pointer.-XX:AllocateInstancePrefetchLines=value, where value is a number between 1 and 64

 

Since:

 

Starting from JDK 6.

 

Examples:

 

This will instruct JVM to set 10 lines:

 

-XX:AllocateInstancePrefetchLines=10

 

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.

 

One of the reasons for JVM to introduce this parameter is to provide a space for engineering to choose optimization techniques. This parameter allows you to set the number of lines to prefetch before instance allocation pointers.

 

Default Value:

 

By default, the number of lines set is 1.

 

Note:

 

This parameter only supported by the Java HotSpot Server VM.

 

Error:

 

If the value used is outside of the range 1 and 64

 

Arguments related:

 

AllocatePrefetchDistance, AllocatePrefetchInstr, 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-allocateinstanceprefetchlines

  • x-allocateinstanceprefetchlines

  • JVM startup parameter