GC mechanism

work purpose

Find useless objects in the heap, reclaim the space occupied by these objects, and reuse them

Algorithm ideas

Objects form sets, or tree structures, starting from the root

All that can be found are surviving objects, and those that cannot be found should be recycled

Judging survival

Available count: Each object has a reference count, which is incremented by 1 for new references and decremented by 1 for release. When the count is 0, it can be recycled. Unable to resolve the problem of objects referencing each other

Reachability analysis: The search starts from the GC Roots, and the path followed is called the reference chain. When an object has no reference chain directly to GC Roots, that is, the object has no reference, is unreachable, and can be recycled

The new domain will be divided into 3 parts: The first part is called Eden. (Eden?? Maybe because Adam and Eve are the earliest objects of human activity?), the other two parts are called auxiliary living space (kindergarten), I here one is called A space (From sqace), the other is called B space ( To Space).

Copying algorithm for new domains in the heap

For newly generated objects, they are placed in Eden; when Eden is full (too many children), the GC will start to work, first stop the application and start collecting garbage;

Copy all the objects that can be found into the A space. Once the A space is full, the GC will copy all the objects that can be found in the A space to the B space (the original storage objects will be overwritten). When the B space is full time, GC copies all objects that can be found in space B to space A, and AB switches roles in this process;

After the active objects go through a certain number of GC operations, these active objects will be put into the old domain, and for these active objects, the kindergarten life of the new domain is over.

Why is the new domain so frustrating? Most of the objects generated by the application are short-lived. The ideal state of the copying algorithm is that all objects moved out of Eden will be collected, because these are short-lived ghosts and should be collected after a certain number of GCs. Then the objects moved into the old domain are long-lived, which can prevent the back and forth copying of the AB space from affecting the application.

In fact, this ideal state is difficult to achieve. There are inevitably long-lived objects in the application program. The inventor of the copying algorithm wants these objects to be placed in the new domain as much as possible to ensure small-scale replication and compress the old domain. can be much more expensive than replication in a new domain.

old domain tracing algorithm

Called the mark-sweep-compact collector, note that this has a compaction, which is an expensive operation.

The algorithm can be detailed: https://www.cnblogs.com/ityouknow/p/5614961.html

 

The larger the proportion of young generation, the better.

Setting the young size larger than half of the total heap size creates inefficiencies. If it is set too small, it can cause bottlenecks because the young generation collector has to run frequently.

 

Summary
1. The size of the JVM heap determines the running time of the GC. If the size of the JVM heap exceeds a certain limit, the GC will take a long time to run.
2. The longer the object lives, the longer the recovery time required by GC, which affects the recovery speed.
3. Most objects are short-lived, so if the lifetime of these objects can be made within one running cycle of the GC, wonderful!
4. The speed at which objects are created and released determines the frequency of garbage collection.
5. If the GC runs for more than 3-5 seconds at a time, it will affect the operation of the application. If possible, the size of the JVM heap should be reduced.
6. Experience from seniors: Usually, the size of the JVM heap should be 80% of the physical memory.

 

Reference: https://blog.csdn.net/jiafu1115/article/details/7024323

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326803650&siteId=291194637