JVM-heap

Overview of the core of the heap

  1. Only one heap memory exists for a JVM instance.
  2. It has been created when the JVM is started, and the space size is determined. You can use -Xms and -Xmx to set the maximum and minimum space. Usually the settings are the same. The purpose is to be able to calculate the size of the heap area without re-separation after GC
  3. The pair can be physically discontinuous but logically continuous.
  4. All threads share a heap, where thread private buffer TLAB can be divided.

Memory breakdown

After Jdk8, the stack is logically divided into three parts: newborn area + senior care area +Meta space

  • Young Generation Space
    • It is divided into Eden and Survivor1 and Survivor2
  • Tebure generation space retirement area
  • Mate Space Meta space (method area)

Young and old

  • Objects stored in the JVM can be divided into two categories (see the figure below for specific division)

    • What kind of short-term objects
    • The other is objects with a very long life cycle
      Insert picture description here
  • Configure the proportion of young generation and old generation in the heap structure (generally not adjusted during development)

    • Default-XX: NewRatio=2 means 1 for the new generation and 2 for the old generation
    • In HotSpot, Eden: s0: s1 = 8:1:1 can be adjusted by -XX: SurvivorRatio = 8. -XX: -UseAdaptiveSizePolocy Turn off adaptive memory allocation strategy (useless). Need to show designation
  • Almost all Java objects are new in the Eden area,

  • Most of the destruction of Java objects takes place in the newborn area.

  • You can use the option "-Xmn" to set the maximum memory size of the new generation.

Object allocation space

  1. The new object is first placed in the Eden area (the size is limited)
  2. When the memory in the Eden area is full, then the young generation will be garbage collected (YGC/Minor GC)
  3. And put the object that has not been cleared into the empty survivor area, and set its age counter to 1, and put the object in another survivor area into the survivor area and add one to its age. If the age of any object reaches 15 ( It can be promoted to the Tenured area through -XX: MaxTenuringThreshold = setting).Insert picture description here

TLAB

why would?

  • Heap area city thread shared area, any thread can access the shared data in the heap area
  • Since the creation of instance objects is very frequent in the JVM, the partitioning of the memory space from the heap area needs to be locked under concurrent conditions, which affects the speed of the allocation number.

TLAB came into being

  • From the perspective of the memory model, continue to divide the Eden area, and JVM allocates a private cache area for each thread.
  • When multi-threaded allocation, TLAB can avoid a series of non-thread safety issues. Improve throughput, which can be calledQuick allocation strategy
    Insert picture description here
    Insert picture description here

Parameters commonly used in heap space

  • -XX:+PrintFlagsInitial: View the default initial values ​​of all parameters

  • -XX:+PrintFlagsFinal: View the final value of all parameters (there may be modifications, no longer the initial value)

    • jps: View the currently running process
    • jinfo -flag SurvivorRatio process id
  • -Xms: initial heap space memory (default is 1/64 of physical memory)

  • -Xmx: Maximum heap space memory (default is 1/4 of physical memory)

  • -Xmn: Set the size of the young generation (initial value and maximum value)

  • -XX:NewRatio: Configure the proportion of the new generation and the old generation in the heap structure

  • -XX:SurvivorRatio: Set the ratio of Eden and S0/S1 space in the new generation

  • -XX:MaxTenuringThreshold: Set the maximum age of the new generation of garbage

  • -XX:+PrintGCDetails: output detailed GC processing log

  • -XX:+PrintGC or -verbose:gc: print brief information about gc

  • -XX:HandlePromotionFalilure: Whether to set space allocation guarantee

Space allocation guarantee

Before the occurrence of Minor GC, the virtual machine checks whether the maximum available continuous space in the old generation is greater than the total space of all objects in the young generation.

If it is greater than, the Minor GC is safe.
If it is less than, the virtual machine checks whether the -XX:HandlePromotionFailure setting value allows the guarantee to fail.
If HandlePromotionFailure=true, it will continue to check whether the maximum available continuous space in the old generation is greater than the average size of the objects promoted to the old generation.
If it is greater than, then try a Minor GC, but this time Minor GC is still risky;
if it is less than, then perform a Full GC.
If HandlePromotionFailure=false, then a Full GC is performed.

Not all objects are on the heap

Escape analysis

  1. This is a cross-function global data flow analysis algorithm that can effectively reduce the synchronization load and memory heap allocation pressure in Java programs.
  2. Through escape analysis, the Java Hotspot compiler can analyze the use range of a new object reference to determine whether to allocate this object to the heap.
  3. The basic behavior of escape analysis is to analyze the dynamic scope of the object:
    • When an object is defined in a method and the object is only used inside the method, it is considered that no escape has occurred.
    • When an object is defined in a method, it is referenced by an external method, it is considered to have escaped. For example, as a call parameter passed to other places.
  • After the JDK 1.7 version, the escape analysis has been enabled by default in HotSpot. If you are using an earlier version, developers can pass:

    • The option "-XX:+DoEscapeAnalysis" explicitly enables escape analysis
    • View the screening results of escape analysis through the option "-XX:+PrintEscapeAnalysis"

Allocation on the stack

  • According to the results of escape analysis during compilation, the JIT compiler found that if an object has no escape method, it may be optimized for stack allocation.
  • After the allocation is completed, execution continues in the call stack, and finally the thread ends, the stack space is recycled, and the local variable object is also recycled. This eliminates the need for garbage collection.
  • Common scenarios of allocation on the stack: In the escape analysis, it has been explained that they are assigning member variables, method return values, and instance reference passing.

Synchronous omission

  • The cost of thread synchronization is quite high, and the consequence of synchronization is to reduce concurrency and performance.

  • When dynamically compiling a synchronization block, the JIT compiler can use escape analysis to determine whether the lock object used by the synchronization block can only be accessed by one thread and not released to other threads. If not, then the JIT compiler will cancel the synchronization of this part of the code when compiling this synchronization block. This can greatly improve concurrency and performance. This process of canceling synchronization is called synchronization omission, also calledLock elimination

public void f() {
    
    
    Object hellis = new Object();
    synchronized(hellis) {
    
    
        System.out.println(hellis);
    }
}
//可以优化到。因为同步的对象只在方法体内有效。
public void f() {
    
    
    Object hellis = new Object();
	System.out.println(hellis);
}

Separate objects

  • Scalar refers to data that cannot be broken down into smaller data. The primitive data type in Java is a scalar.

  • In contrast, data that can be decomposed is called Aggregate. Objects in Java are aggregates because they can be decomposed into other aggregates and scalars.

  • In the JIT stage, if it is found that an object cannot be accessed by the outside world after escape analysis, then after JIT optimization, the object will be disassembled into several member variables contained therein instead. This process is scalar replacement

Parameter -XX:+ElimilnateAllocations: scalar replacement is enabled (default enabled), allowing objects to be scattered and allocated on the stack.

to sum up

insufficient

  1. The paper on escape analysis was published in 1999, but it was not realized until JDK1.6, and the technology is not very mature until now.

  2. The root cause is There is no guarantee that the performance cost of escape analysis will be higher than his cost. Although scalar replacement, stack allocation, and lock elimination can be done after escape analysis. However, escape analysis itself also requires a series of complex analysis, which is actually a relatively time-consuming process.An extreme example is that after escape analysis, it is found that no object does not escape. Then the process of escape analysis is wasted.

  3. Although this technology is not very mature, it is also a very important method in the optimization technology of just-in-time compiler. Note that there are some opinions that through escape analysis, the JVM will allocate those objects on the stack that will not escape. This is theoretically feasible, but it depends on the choice of the JVM designer.

  4. As far as I know, this is not done in Oracle Hotspot JVM, which has been explained in the escape analysis related documents, so it is clear that all object instances are created on the heap.

  5. At present, many books are still based on the previous version of JDK7. The JDK has undergone great changes. The cache and static variables of intern strings have been allocated to the permanent generation, and the permanent generation has been replaced by the metadata area. But the intern string cache and static variables are not transferred to the metadata area, but directly allocated on the heap, so this is also in line with the conclusion of the previous point: object instances are all allocated on the heap.

Heap summary

  1. The young generation is the area where objects are born, grow, and die. An object is produced and applied here, and finally collected by the garbage collector and ends its life.

  2. The long-lived objects placed in the old age are usually Java objects copied from the Survivor area.

  3. Of course, there are special cases, we know that ordinary objects may be allocated on TLAB;

  4. If the object is too large to be allocated on TLAB, the JVM will try to allocate it directly to other locations in Eden;

  5. If the object is too large to find enough continuous free space in the young generation, the JVM will directly allocate it to the old generation.

  6. When GC only occurs in the young generation, the behavior of reclaiming objects in the young generation is called Minor GC.

  7. When GC occurs in the old age, it is called Major GC or Full GC.

  8. Generally, the occurrence frequency of Minor GC is much higher than that of Major GC, that is, the frequency of garbage collection in the old generation will be much lower than that of the young generation.

Guess you like

Origin blog.csdn.net/null_zhouximin/article/details/112743239