JVM: garbage collector and memory allocation strategy

Three pieces GC needs to be done:

What memory need to recycle? When the recovery? How to recycle?

java runtime memory area of ​​the program counter, stack virtual machine, native method stacks with three regions born threads, with threads off, the stack with the stack frame of the entry and exit method for an orderly execution of the stack and into the stack operations. How much memory each allocation frames frame structure type substantially known per finalized, so the memory allocation and recovery of the region are provided with certainty. The java heap and method area is not the same, a plurality of interface implementation class memory may be different, multiple branches need a method of memory may not be the same, we only run during the program in order to know what will be created Object, this memory allocation and recovery are dynamic, that is, this part of the GC major concern.

 

Which objects can be recycled:

Reference counting algorithm: 1 plus references, minus 1:00 failure to achieve simple, but if there is a circular reference is a problem, so the Java virtual machine does not take this route.

Reachability analysis algorithm: mainstream achieve mainstream programming languages ​​are used in reachability analysis to determine whether the object is alive. The basic idea is through a series of objects called "GC Roots" as a starting point, to start the search downward from these nodes, the search path traversed by the chain of references is referred to when an object is not connected to any reference GC Roots chain, then this object is not available. As shown below:

 

 

Java object may include the role of GC Root:

1. VM stack referenced objects

2. The method of object reference region class static properties.

3. The method area constant object reference

4. A native method stacks NATIVE subject method references.

 

 

Recycling method area:

In the heap, especially in the new generation, conventional applications can be recycled garbage collection generally 70-95%

Garbage collection algorithms:

1. mark - sweep algorithms: mark and sweep is divided into two stages, first mark all objects need to be recovered and then recycled object marked.

There are two problems: one is efficiency, and second, will produce large amounts of memory fragmentation disconnected after clearing.

2. Copy algorithm: the available memory is divided into two size treat each time only one of them, when it runs out of a memory, copying the surviving object to another one above, then the used memory once cleaned up, they do not consider the complexities of debris. Simple and efficient, sacrificed half the memory.

Now the virtual machines are using this collection algorithm to recover the new generation. 98% of the new generation object Chaosheng Xi die, it does not require a 1: 1 ratio by dividing the memory space. But the memory is divided into a larger piece of eden space and survivor spot smaller space, which each use eden and one survivor. hotspot night eden: survivor is 8: 1

3. Mark - Collation Algorithm:

Copy Collation Algorithm copy operation will be carried out more objects at higher survival rate, efficiency will be reduced, but the key is you do not want to waste space, additional space will be allocated guarantees to respond memory is used by All objects have survived extreme situations, so the old era is generally not directly choose this algorithm.

Where the use of markers in the old year - finishing algorithms: algorithms and clear, like Tam subsequent steps are not directly recycled objects to clean up, but to all live objects are moved to the end, then clean out the memory directly outside the terminal boundary.

4. generational collection algorithm:

The current garbage collection business virtual machine implements a generational collection: Depending on whom the survival period of the memory is divided into a few: that's the new generation and the old: This can be the most appropriate collection method based on the characteristics of the kind of the last decade.

New Generation: Because a large number of objects will die, a small amount of survival, using the copy algorithm: the old year: because of the high survival rate of the object, using markers or tags to organize clean-up algorithm.

 

Hotspot algorithm:

Enumeration root GC Root:

Reachability analysis of the implementation of the third period also reflected in the GC pause, because the work must be able to ensure a consistent snapshot, the process of analyzing the situation of the object reference relationship is still evolving not appear the point is not satisfied, then the results of the analysis accuracy can not be guaranteed. This must lead to a standstill when the GC for all threads

Safety point: only in a specific location, just stop the thread, this position is called a safe point safepoint.

Regional security: Security in the region is a piece of code fragments, a reference relationship will not change, such as thread sleep, blocked.

 

Garbage collector:

 

 

 

Serial serial collector

For the new generation;

      Using replication algorithm; single-threaded collection; when garbage collection, threads must suspend all work until it is completed.

Scenarios

      HotSpot is still the default in the Client mode Cenozoic collector;

      There are also superior to other local collector:

    Simple and efficient (compared to the other single-threaded collector);

        For defining a single CPU environments, Serial no thread interacting collector (handover) overhead, to achieve the maximum collection efficiency single-threaded;

        In the user's desktop application scenario, the available memory is generally small (tens to a hundred M M), garbage collection can be completed relatively short period of time (more than a hundred to tens of MS MS), they do not occur frequently in this is acceptable

ParNew Collector:

ParNew garbage collector is multi-threaded version of the Serial collector.

Feature

      In addition to multi-threading, the rest of the behavior, characteristics and Serial collector as;

  In a single CPU environment, there will be better results than Serail collector because of thread interaction overhead

Parallel Scavenge collector

There are some similar features with ParNew collector

      The new generation of collectors;

      Using replication algorithm;

      Multithreading collection;

The main features are: Different its concerns with other collectors

      CMS and other collectors focus is to shorten the pause time garbage collector thread of the user as much as possible;

      Parallel Scavenge the target collector is controlled up to a certain (Throughput);

Serial Old collectors

For years old;

      A "mark - finishing" algorithm (as well as compression, Mark-Sweep-Compact);

      Single-threaded collection;

Parallel Old collectors

Parallel Old Parallel Scavenge garbage collector is a collector's version of the old;

CMS collector

Concurrent mark Cleanup (Concurrent Mark Sweep, CMS) collector, also known as concurrent low pause collector (Concurrent Low Pause Collector) or low latency (low-latency) garbage collector;

Feature

      For years old;

      Based on the "mark - sweep" algorithm (compression operation is not performed, memory fragmentation);            

      For the shortest recovery time objectives pause;

      Concurrent collection, low pause;

G1 collector

 

(Garbage-First) is JDK7-u4 before launching commercial collector;

(A), in parallel with the concurrent

      You can take full advantage of multi-CPU, multi-core hardware advantages in the environment;

      In parallel to shorten the "Stop The World" pause time;

      Concurrent garbage collection can also let the user program at the same time;

(B), generational collection, the collection's old and new generation include    

C), with a variety of garbage collection algorithms, spatial integration, not fragmentation

      On the whole, it is based on the mark - Collation Algorithm;

      From the local (between two Region) See, replication algorithm is based;

      This is a similar train algorithm implementation; 

      Will not produce memory fragmentation, it is conducive to long-running;

(D), predictable pause: low pause while achieving high throughput

      In addition to the pursuit of low pause at the G1, but also dwell time model to establish a predictable;

      M can explicitly specify the millisecond time slice, time consuming garbage collection does not exceed N millisecond;

      You can independently manage the entire GC heap (the old and the new generation's), without the need to mix with other collectors;

      It can be processed in different ways at different times;

 

Reference: https: //www.cnblogs.com/cxxjohnson/p/8625713.html

 

Memory allocation and recovery strategies:

In most cases, the new generation of objects allocated in eden region, eden zone when there is not enough space allocated, the virtual machine to conduct a Minor GC.

Minor GC: refers to the new generation garbage collection action

Major GC / Full GC: refers to the old GC's happening

Large objects directly into the old era. China is a large object requires a lot of java object contiguous memory space, such as long strings and arrays.

Long-term survival of the object will enter the old year: If an object after several (default 15) minorGC still alive, will enter the old era.

Guess you like

Origin www.cnblogs.com/hankuikui/p/11479621.html