Heap of runtime memory data area (1)

Heap

Core overview of the heap

  • There is only one heap memory in a JVM instance , and the heap is also the core area of ​​Java memory management. The Java heap area is created when the JVM starts, and its space size is determined . It is the largest memory space managed by the JVM.
  • The size of the heap memory can be adjusted.
  • The "Java Virtual Machine Specification" stipulates that the heap can be in a physically discontinuous memory space, but logically it should be considered continuous.
  • All threads share the Java heap, where thread-private buffers (Thread Local Allocation Buffer, TLAB) can also be divided.
  • The description of the Java heap in the "Java Virtual Machine Specification" is: All object instances and arrays should be allocated on the heap at runtime. (The heap is the run-time data area from which memory for all class instances and arrays is allocated)
  • What I'm trying to say is: "Almost" all object instances are allocated memory here. One from the point of view of practical use.
  • Arrays and objects may never be stored on the stack because the stack frame holds a reference to the location of the object or array on the heap.
  • After the method ends, the objects in the heap will not be removed immediately, but will only be removed during garbage collection .
  • The heap is the key area for GC (Garbage Collection., Garbage Collector) to perform garbage collection.

memory breakdown

Most modern garbage collectors are designed based on generational collection theory, and the heap space is subdivided into:

agreement:

New Area/New Generation/Young Generation

Elderly care area/elderly area/old generation

Permanent Area/Permanent Generation

Set the heap memory size and OOM

The Java heap area is used to store Java object instances, so the size of the heap has been set when the JVM starts, and you can set it through the options "-Xmx" and "-Xms".

  • "-Xms" is used to indicate the starting memory of the heap area, which is equivalent to -XX:InitialHeapsize
  • "-Xmx" is used to indicate the maximum memory of the zone, which is equivalent to -XX:MaxHeapsize

Once the memory size in the heap area exceeds the maximum memory specified by "-Xmx", an OutofMemoryError exception will be thrown.

Usually, the two parameters -Xms and -Xmx are configured with the same value. The purpose is to improve performance without re-partitioning the size of the computing heap after the java garbage collection mechanism cleans up the heap.

by default,

Initial memory size: physical computer memory size/64

Maximum memory size: physical computer memory size/4

Young Generation vs. Old Generation

Java objects stored in the JVM can be divided into two categories:

  • One type is transient objects with a short life cycle, which are created and destroyed very quickly.
  • The life cycle of another type of object is very long, and in some extreme cases, it can be consistent with the life cycle of JM.

If the Java heap area is further subdivided, it can be divided into the young generation (YoungGen) and the old generation (0ldGen). The young generation can be divided into Eden space, Survivor0 space and Survivor1 space (sometimes also called from area, to area).

The following parameters are generally not adjusted during development:

Configure the proportion of the new generation and the old generation in the heap structure.

  • Default -X:NewRatio:=2, which means that the new generation occupies 1, the old generation occupies 2, and the new generation occupies 1/3 of the entire heap
  • You can modify -X:NewRatio=4, which means that the new generation accounts for 1, the old generation accounts for 4, and the new generation accounts for 1/5 of the entire heap

  • In HotSpot, the default ratio of Eden space to the other two Survivor spaces is 8:1:1
  • Of course, developers can adjust this space ratio through the option "-XX:SurvivorRatio". Ratio-XX:SurvivorRatio=8
  • Almost all Java objects are newly created in the Eden area.
  • Most of the destruction of Java objects is carried out in the new generation.
    • IBM's special research shows that 80% of the objects in the new generation are "live and die".
  • You can use the option "-Xmn" to set the maximum memory size of the new generation
    • This parameter generally uses the default value.

Diagram object allocation process

Allocating memory for new objects is a very rigorous and complex task. JVM designers not only need to consider how and where to allocate memory, but also need to consider GC because the memory allocation algorithm is closely related to the memory recovery algorithm. Whether memory fragmentation will be generated in the memory space after memory reclamation is performed.

  1. The new objects are placed in the Eden area first. There is a size limit for this zone.
  2. When the space in Eden is full, the program needs to create objects again, and the garbage collector of the JVM will perform garbage collection (Minor GC) on the Eden area to destroy objects in the Eden area that are no longer referenced by other objects. Then load the new object and put it in the Eden Park
  3. Then move the remaining objects in Eden to Survivor 0.
  4. If garbage collection is triggered again, the ones that survived last time will be placed in Survivor 0. If they are not recycled, they will be placed in Survivor 1.
  5. If you go through garbage collection again, it will be put back into Survivor 0 at this time, and then go to Survivor 1 again.
  6. When can I go to the nursing home? The number of times can be set. The default is 15 times.
    1. You can set the parameter: -XX:MaxTenuringThreshold=<N> to set.
  1. In the elderly care area, it is relatively leisurely. When the memory in the retirement area is insufficient, GC:Major GC is triggered again to clean up the memory in the retirement area.
  2. If the Elderly District executes Major GC and finds that the object cannot be saved, an OOM exception will occur

Summarize:

Summary for the s0 and s1 areas of the survivors: there is an exchange after copying, whoever is empty is to.

Regarding garbage collection, it is frequently collected in the new districts, rarely collected in the elderly districts, and almost never collected in the metaspace of the permanent districts.

Guess you like

Origin blog.csdn.net/m0_73843666/article/details/130142479