A4. JVM memory allocation and recovery strategies

[Overview]

  Java technology system advocated in the final automatic memory management can be attributed to solve two problems for the automation: the object to allocate memory and memory recall allocated to the object.

  Memory allocation object, to the general direction of speaking, is allocated on the heap, allocated mainly targeted at the new generation of the Eden area, if local thread allocation cache start (using ThreadLocal class), then the thread priority in TLAB (Thread Local Allocation assignment on) Buffer. In rare cases the rules may also be assigned directly in the old time, not 100% fixed allocation, the details depend on the current use is to set what kind of combination of garbage collection, as well as virtual machine memory-related parameters.

[Basics]

  1) Memory allocation area of ​​Java objects, mainly in Java Heap (Java heap), Java Heap divided into the new generation and the old era, the Cenozoic is divided into Eden District, Survivor From district, Survivor To area.

  2) In most cases, objects in the new generation of Eden allocated region. When the Eden area is not enough space allocated, the virtual machine will launch a Minor GC.

  . 3) Minor GC and Full GC:

  • New Generation GC (Minor GC): refers to the place in the new generation garbage collection action, because most of Java objects have properties Chaosheng evening off, so Minor GC very frequently, usually recover relatively fast speed.
  • Old's GC (Major GC / Full GC): GC refers to occur in old age, appeared Full GC, often accompanied by at least one of the Minor GC. Full GC generally slower than the speed of more than 10 times Minor GC.

  4) large objects directly into the old year

  It refers to the so-called big pairs are: Java object requires a lot of contiguous memory space, the most typical of large objects is the kind of long strings and arrays. Large Object is a bad news for memory allocation for virtual machines, often easily lead to large objects ahead of garbage collection is triggered when there are a lot of memory space in order to obtain enough contiguous space to "placed" them.

  5) Long-term survival of the object will enter the old year

  Since the virtual machine to the idea of ​​generational collection to manage memory, memory recall when it must be able to identify which objects should be placed in the new generation, which objects should be placed in the old era. To do this, a virtual machine for each object defines a target age (Age) counter. If the object was born in Eden, and after the first Minor GC still alive, and can accommodate Survivor, it will be moved to Survivor space, and the object is set to age 1. Survivor objects in every area "get through" a Minor GC, increased age, 1 year old, the age when it is to a certain extent (the default is 15 years old), will be promoted to the old era. Object promoted's old age threshold parameters can be -XX: maxTenuringThreshold settings.

  6) Dynamic Age Determination: In order to better adapt to different memory status of the program, virtual machines are not always subject to the requirements of the age must be reached before promotion maxTenuringThreshold years old, the same age, if the sum of all the objects in the size of the space Survivor more than half the space Survivor, age greater than or equal to the target age you can go directly years old, no need to wait until the age maxTenuringThreshold in requirements.

  7) space allocation guarantee: Minor GC before it occurs, first check whether the old virtual machine's maximum available contiguous space is greater than all the objects in the new generation of total space, if this condition is met, then the Minor GC to ensure that it is safe. If not satisfied, the virtual opportunity to review whether to allow HandlePromotionFailure settings guarantee failure. If allowed, it will continue to check whether old's maximum available contiguous space larger than previous years old was promoted to the average size of the object, if greater than, will attempt to conduct a Minor GC, despite the Minor GC is risky; if less than, or HandlePromotionFailure settings do not allow adventure, this time it is required to conduct a Full GC.

  Below explain the "adventure" What does this mean? Cenozoic use the copy collection algorithms, memory utilization to only use one space Survivor rotation as a backup, so the case when a large number of objects Minor GC appears still alive (the most extreme case is the new generation of memory recovery after all the objects have survived), we need to be allocated guarantee years old, can not accommodate the Survivor objects directly into the old era. And loan guarantees in life, similar to the old year to make such guarantees, provided that the remaining space itself, there's the old hold these objects, a total of how many objects will survive before the actual memory recovery is not clear know, so I had to before every take was promoted to the average size of the capacity of the old value of the object's value as an experience, and the remaining space of the old year comparison, Full GC decide whether to let the old year to free up more space.

  Compare fact still averaged a means of dynamic probability, that is, if the object after a sudden increase in survival time Full GC, far higher than the average, it will still lead to a security failure (Handle Promotion Failure). If there is a HandlePromotionFailure failure, it had to re-initiate a Full GC after a failure. Although the circle around the time of the security failure is the largest, but in most cases are still open will HandlePromotionFailure switch, Full GC avoid too frequent.

  After JDK 1.6 Update 24, HandlePromotionFailure parameter space will not affect the virtual machine allocation guarantee policy, it becomes a rule: Only the largest contiguous space old age is greater than the total size of the new generation of the object or the average size of the previous promotion, proceed Minor GC otherwise, Full GC.

Guess you like

Origin www.cnblogs.com/zlxyt/p/10989827.html