GC (garbage collection) in JVMAlgorithmThere are many types, such as tag clearing collector, compression collector, and generation collector. For details, see the types of hotspot vm gc.
Currently, generation-based collection is commonly used (generational collection, which is also used by Sun Vm and introduced after j2se1.2). Memory is divided into several areas and objects of different lifecycles are placed in different areas:Young Generation,Tenured generationAnd permanet generation. Most objec objects are allocated to young generation (short lifecycle), and most objects are die here. When young generation is full, minor collection (ygc) is triggered ). Objects that survive after minor collection will be movedTenured generation (Long Lifecycle). Finally,Tenured generationTrigger the major collection when it is full. Major collection (full GC) triggers the collection of the entire heap, including the collection of young generation. The permanet generation area is relatively stable and mainly stores classloader information.
Young Generation consists of Eden and two region vor regions. One of the primary vor regions is always empty, and is the destination of the Eden region and the other primary vor region living after the next copy collection. Objects are copied in the survivo region until they are transferred to the tenured region.
We need to minimize the number of full GC (Tenured generationGenerally, it takes a long time to collect data. Frequent full GC may seriously affect application performance ).
Heap memory GC
JVM (using the generational collection policy), ygc is performed on young objects (Young Generation) at a high frequency, while (TenuredGeneration) less (TenuredFull GC is performed only when generation is full. In this way, you do not need to check all objects in the memory every GC.
Non-heap memory is not GC
GC will notProgramPermgen space is cleaned up during runtime, so if your application contains many classes (especially dynamic generation classes, of course permgen space stores more than classes, the permgen space error may occur.
Memory application, object aging process
I. Memory application process
- JVM will try to initialize a memory area for the relevant Java object in Eden;
- When the Eden space is sufficient, the memory application ends. Otherwise, go to the next step;
- JVM tries to release all inactive objects (minor collection) in Eden. After the Eden space is released, if it is still insufficient to add new objects, it tries to put some active objects in the same vor area;
- The primary vor area is used as the intermediate swap area for Eden and old. When the old area has enough space, the objects in the primary vor area will be moved to the old area; otherwise, they will be kept in the primary vor area;
- When there is not enough space in the old area, the JVM will perform a major collection in the old area;
- After full garbage collection, if the primary VOR and old areas still cannot store some objects copied from Eden, the JVM cannot create a memory area for the new object in the Eden area, the error "out of memory" appears ";
Ii. Object aging process
-
- The memory of the newly created object is allocated from Eden. The minor collection process is to copy the Eden and the live objects in the consumer vor space to the idle consumer vor space. After the minor collection object in young generation goes through a certain number of times (you can configure the parameter), it will be moved to old generation, called tenuring.
- GC trigger Condition
GC type |
Trigger Condition |
What happened when the trigger |
Note: |
View Method |
Ygc |
Insufficient Eden Space |
Clear the memory occupied by all No ref objects in Eden + from external vor. Copy all surviving objects in Eden + from sur to Sur. Some objects will be promoted to old: To sur cannot be placed The number of surviving times exceeds that in turning threshold Re-calculate tenuring threshold (this will be triggered by serial parallel GC) Re-adjust the size of Eden and from (parallel GC will trigger this item) |
Suspend application throughout the process Multi-threaded processing is determined by the specific GC. |
Jstat-gcutil GC log |
FGC |
Insufficient old space Insufficient perm Space Displays scheduled calls to system. GC, RMI, and so on. Pessimistic strategy in ygc Dump live memory information (jmap-dump: Live) |
Clear No ref objects in heap Class information loaded in the uninstalled classloader in permgenIf collectgenofirst is configured, ygc is triggered first (for serial GC) If scavengebeforefullgc is configured, ygc is triggered first (for serial GC) |
Suspend application throughout the process Multi-threaded processing is determined by the specific GC.Whether to compress depends on the specific GC configuration. |
Jstat-gcutil GC log |
If the permanent generation space is insufficient, full GC is triggered, and the permgen space error is still caused.
Refer:
Http://jiangyongyuan.javaeye.com/blog/356502
Http://www.helloying.com/blog/archives/164
Recommended content:
GC pessimistic strategy: Parallel GC: GC pessimistic strategy-serial GC