In-depth understanding of Java garbage Collection mechanism

Source: Internet
Author: User

In- depth understanding of the Java garbage collection mechanism----I. The significance of garbage collection mechanism

A notable feature of the Java language is the introduction of a garbage collection mechanism, which makes it possible for the C + + programmer to solve the most troublesome memory management problems, which makes it unnecessary for Java programmers to consider memory management when writing programs. Because there is a garbage collection mechanism, objects in Java no longer have a "scope" concept, and only references to objects are scoped. garbage collection can effectively prevent memory leaks and effectively use free memory.

  PS: Memory leak refers to the memory space is not recycled after use, in the general case, without involving complex data structures, Java memory leakage is represented as a memory object life cycle beyond the length of time the program needs it, we sometimes call it "object free."

second, the algorithm of garbage collection mechanism

The Java language Specification does not explicitly describe which garbage collection algorithm the JVM uses, but any garbage collection algorithm typically does 2 basic things: (1) Discovers useless information objects, (2) reclaims the memory space occupied by the useless objects so that the space can be reused by the program.  

1. Reference counting method (Reference counting Collector)

1.1 Algorithm Analysis

The reference count is an early policy in the garbage collector. In this approach, each object instance in the heap has a reference count. When an object is created and the object instance is assigned to a variable, the variable count is set to 1. When any other variable is assigned a reference to this object, the count is incremented by 1 (a = B, then the counter of the object instance referenced by B + 1), but when a reference to an object instance exceeds the life cycle or is set to a new value, the reference counter of the object instance is reduced by 1. Any object instance that references a counter of 0 can be garbage collected. When an object instance is garbage collected, the reference counter of any object instance it references is reduced by 1.

1.2 Advantages and Disadvantages

Advantages:

The reference count collector can be executed very quickly and interleaved in the program's running. It is advantageous to the real-time environment that the program needs not to be interrupted for a long time.

Disadvantages:

  A circular reference could not be detected. If the parent object has a reference to a child object, the child object in turn references the parent object. In this way, their reference count will never be 0.

The 1.3 Reference counting algorithm does not resolve circular reference problems, such as:

 PublicclassMain { PublicStatic voidMain (string[] args) {MyObject Object1=NewMyObject (); MyObject Object2=NewMyObject (); Object1.object=Object2; Object2.object=Object1; Object1=NULL; Object2=NULL; }}

  The last two sentences assign Object1 and object2 null, which means that Object1 and Object2 object are no longer accessible, but because they reference each other and cause their reference counters to be 0, the garbage collector never recycles them.

2.tracing algorithm (tracing Collector) or marker-purge algorithm (Mark and sweep)

2.1 Root Search algorithms

  The root search algorithm is introduced from the graph theory of discrete mathematics, the program regards all referential relationships as a graph, starts with a node GC root, looks for the corresponding reference node, finds the node, and continues to look for the node's reference node, after all the reference nodes have been searched, The remaining nodes are considered nodes that are not referenced, that is, useless nodes.

The objects in Java that can be used as GC root are

1. Objects referenced in the virtual machine stack (local variable table)

2. Objects referenced by static properties in the method area

3. Objects referenced by constants in the method area

4. Objects referenced in the local method stack (native objects)

of the 2.2tracing algorithm

  

2.3 Mark-Sweep algorithm analysis

The tag-purge algorithm takes a scan from the root collection, marks the surviving object object, and then scans the entire space for objects that are not tagged, as shown in. The tag-purge algorithm does not need to move objects, and it only handles objects that are not alive, and is extremely efficient in the case of many surviving objects, but because the tag-purge algorithm directly reclaims objects that are not alive, it can cause memory fragmentation.

3.compacting algorithm or marker-collation algorithm

The tag -grooming algorithm uses the tag-sweep algorithm to mark objects in the same way, but when it is cleared, all surviving objects are moved to the left-hand free space after reclaiming the space occupied by the objects that are not alive, and the corresponding pointers are updated. The tag-collation algorithm, which is based on the tag-purge algorithm and moves the object, is more expensive, but solves the problem of memory fragmentation. In the implementation of the collector based on the compacting algorithm, the handle and the handle table are generally added.

4.copying algorithm (compacting Collector)

  

The algorithm is proposed to overcome the overhead of the handle and to solve the garbage collection of heap fragments. It begins by dividing the heap into an object face and multiple free polygons, the program allocates space for the object from the object surface, when the object is full, garbage collection based on the copying algorithm scans the active object from the root set and copies each active object to the free surface (so that there is no free hole between the memory occupied by the active object), The idle face becomes the object face, the original object face becomes the idle surface, and the program allocates memory in the new object face. A typical garbage collection based on coping algorithm is the stop-and-copy algorithm, which divides the heap into object and idle area polygons, and the program suspends execution during the switching between the object surface and the idle area.

5.generation algorithm (generational Collector)

  The generational garbage collection strategy is based on the fact that the life cycle of different objects is not the same . Therefore, different life cycle objects can take different recycling algorithms to improve the efficiency of recycling.

Younger generation (young Generation)

1. All newly generated objects are first placed in the younger generation. The goal of the young generation is to collect as quickly as possible those objects with short life cycles.

2. The Cenozoic memory is divided into a Eden area and two Survivor (Survivor0,survivor1) regions according to the 8:1:1 ratio. One Eden area, two survivor districts (in general). Most objects are generated in the Eden area. The Eden zone survivor is copied to a Survivor0 area before being reclaimed, and the Eden area is emptied, and when the Survivor0 area is fully stocked, the Eden and Survivor0 zone survivors are copied to another Survivor1 area. Then empty the Eden and the Survivor0 area, where the Survivor0 area is empty, and then swap the survivor0 and Survivor1 areas, that is, keep the Survivor1 area empty, and so forth.

3. When the Survivor1 area is not sufficient to store the surviving objects of Eden and Survivor0, the surviving objects are stored directly in the old age. If the old age is full, it will trigger an all-time GC, that is, the new generation, the old generation are recycled

4. The occurrence of the new generation of GC is also called minor GC,MINORGC frequency is higher (not necessarily when the Eden area is full before triggering)

Old Generation.

1. Objects that survived after n garbage collection in the younger generation will be placed in the old age generation. Therefore, it can be considered that older generations are storing objects with longer life cycles.

2. The memory is much larger than the new generation (approximately 1:2), when the old memory is full, triggering major GC that is, Gc,full GC occurs relatively low frequency, the old age object survival time is longer, survival marker is high.

Durable generation (Permanent Generation)

  used to store static files, such as Java classes, methods, and so on. The persistence generation has no significant impact on garbage collection, but some applications may dynamically generate or invoke some classes, such as Hibernate, at which point a large, persistent generation space is required to store the new class in these runs.

three. GC (garbage collector)

Collectors used by Cenozoic collectors: Serial, pranew, Parallel scavenge

Old age collector used by collectors: Serial, Parallel, CMS

Serial collector (copy algorithm)

  The new generation of single-threaded collectors, marking and scavenging are single-threaded, with the advantage of being simple and efficient.

Serial Old collector (marker-collation algorithm)

The old age single-threaded collector, the old age version of the serial collector.

Parnew Collector (stop-copy algorithm)

The new generation collector, which can be considered a multithreaded version of the serial collector, has a better performance than serial in multi-core CPU environments.

Parallel Scavenge Collector (stop-copy algorithm)

A parallel collector that pursues high throughput and uses CPUs efficiently. Throughput is typically 99%, throughput = user thread time/(user thread time +GC thread time). Suitable for background applications, such as the corresponding requirements for the interaction of the scene is not high.

Parallel Old Collector (stop-copy algorithm)

Parallel scavenge collector's old version, parallel collector, throughput priority

CMS (Concurrent Mark Sweep) collector (tag-cleanup algorithm)

High concurrency, low pause, the pursuit of the shortest GC recovery pause time, high CPU consumption, fast response time, short pause time, multi-core CPU to pursue high response time selection

IV. Enforcement mechanism of GC

Because objects are processed in a generational way, garbage collection areas and times are different. There are two types of GC:scavenge GC and full GC.

Scavenge GC

In general, when a new object is generated and the Eden application space fails, the scavenge GC is triggered, GC is performed on the Eden Zone, the non-surviving objects are cleared, and the surviving objects are moved to the survivor area. Then tidy up the two districts of survivor. This method of GC is carried out on the young generation of the Eden area and does not affect the old generation. Because most objects start in the Eden area, and the Eden area is not very large, GC in the Eden area is frequent. Thus, it is generally necessary to use fast and efficient algorithms, so that Eden can be free as soon as possible.

Full GC

Organize the entire heap, including young, tenured and perm. The full GC is slower than the scavenge GC because it needs to be recycled for the entire pair, so it should be as low as possible. In the process of tuning the JVM, a large part of the work is to adjust the FULLGC. The full GC may be caused by the following reasons:

1. The old generation (tenured) was written full

2. Persistent generation (Perm) is fully written

3.SYSTEM.GC () is displayed call

4. Dynamic changes in the domain allocation policy of the heap after the last GC

Five, Java has a GC will also have a memory leak problem

1. Static collection classes like HashMap, vectors, etc. are most prone to memory leaks, and the lifetime of these static variables is consistent with the application, and all object objects cannot be freed, as they will always be used by vectors and so on.

 Static Vector v = new   Vector ();   for  (int  i = 1; i<100; I++)  {     Object o  = new   Object ();     V.add (o);     o  = null  ;&NBSP;}  

In this example, there is a reference to the vector object in the code stack with reference to the V and object o. In the For loop, we constantly generate new objects, add them to the Vector object, and then empty the O reference. The question is, if a GC occurs when the O reference is empty, can we create an object that is recycled by GC? The answer is in the negative. Because, when the GC traces a reference in the code stack, it discovers a V reference, and continues to trace, it finds that the memory space pointed to by the V reference has a reference to the object. This means that although the O reference is already empty, there are still other references to object objects that can be accessed, so the GC cannot release it. If after this loop the object object has no effect on the program, we assume that the Java program has a memory leak.

2. Various connections, database connections, network connections, IO connections, etc. do not show call Close closed, not being garbage collected by GC causing a memory leak.

3. The use of listeners can also cause memory leaks if the object is freed without a corresponding delete listener.

In-depth understanding of Java garbage Collection mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.