Java Virtual Machine Parsing article---garbage collector

Source: Internet
Author: User

The last one said about the virtual machine's memory model, when talking about heap memory we mentioned that heap memory is the largest part of Java memory area, and the garbage collector is mainly to reclaim this part of the content. Then this article will introduce the garbage collector principle and the recovery algorithm.

Java garbage collector (GC) is a unique feature in Java, do not need to manually manage an object, do not want to C + + constructors and destructors, the need for the program ape to manually manage, it is easy to cause memory leaks. Of course, if you learn OC language, we know that the OC language has the concept of automatic release pool, of course, we use Retain/release to manually manage objects. So from this we can see the android (Java) in this respect compared to iOS (OC), compared card, because the garbage collector in Java needs algorithm calculation, this may be a bit time-consuming, but the advantage is not the need for manual management. But OC needs to be managed manually, so the system does not require complex algorithms to manage, running faster than Android (of course, iOS is smoother than Android, there are many reasons, this is just one aspect of the content)


how the object is managed in Java

In the heap, where almost all of the object instances in the Java world are stored, the first thing the garbage collector can do to determine which of these objects is "alive" and "dead" before the heap is recycled

Here are two ways to manage objects

First, the reference technology algorithm

This algorithm is very simple, but also a more common way to manage the object

To add a reference counter to the object, whenever there is a place to reference it, the value of the counter is added 1, when the reference fails, the value of the counter is reduced by 1, any time the counter is 0 of the object is not possible to be used, the algorithm is very simple, and early in many object-oriented language used this way, But this is not the way to manage objects in mainstream Java virtual machines, and the main reason is that it is difficult to solve the mutual circular references between objects . Example:

public class Demo {public Object instance = null;public static void Main (string[] args) {Demo demo1 = new demo ();D emo Demo2 = new Demo ();d emo1.instance = Demo2;demo2.instance = demo1;//now Demo1,demo2 set NULL, can Demo1,demo2 be recycled? Demo1 = Null;demo2 = null;//manual call to garbage collector recycle System.GC ();}}
There is a variable in the Demo object instance, when we new two demo objects, when we are associated with each other, when we set them null, actually these two objects are not possible to be accessed, but they are quoted each other, resulting in their reference count is not 0, The reference counting algorithm cannot tell the GC collector to reclaim them. This haspoints are similar to deadlocks in concurrency.

So to solve the problem above, the second way appears.


second, accessibility analysis algorithm

The basic idea of this algorithm is to use a series of "GC Roots" object as the starting point, starting from these nodes to search down, search all traversed path as a reference chain, when an object to the GC Roots no reference chain necklace, it is not available when proving this object, the following look at the example:



In this diagram above, objects object5, OBJECT6, object7 are not related to each other, but they are not accessible to GC roots, so they will be judged as recyclable objects

Note: In the Java language, objects that can be used as GC roots include the following:

1) objects referenced in the virtual machine stack (local variable table in the stack frame)

2) object referenced by class static property in the method area

3) objects referenced by constants in the method area

4) objects referenced by JNI (that is, generally referred to as the native method) in the local method stack


ii. reference types in Java

Since JDK1.2, Java has expanded the concept of references into strong references, soft references, weak references, and virtual references, with the strength of these four references waning at a time

1) A strong reference is a common reference in program code, such as "Object obj = new Object ()", as long as a strong reference exists, the garbage collector never reclaims the referenced object.

2) Soft references are used to describe some useful but not necessary objects, for soft references associated with the object, before the system will occur in memory anomalies, these objects are listed in the recycling scope for a second collection, if this collection does not have enough memory, will throw a memory exception

3) A weak reference is also used to describe a non-required object, but its strength is weaker than the soft reference, and the object associated with the weak reference can only survive the next garbage collection occurs, and when the garbage collector is working, the object associated with the weak reference is reclaimed, regardless of the current memory release.

4) A virtual reference , also known as a phantom reference or phantom Reference, is the weakest reference relationship, whether an object has a virtual reference, does not affect its time to live at all, and cannot obtain an object instance through a virtual reference. The sole purpose of setting a virtual reference association on an object is to be able to receive a system notification when the object is reclaimed by the collector


third, garbage collection algorithm

1) Mark-Clear algorithm

The most basic collection algorithm is the "mark-clear" (mark-sweep) algorithm, like its name, the algorithm is divided into "mark" and "clear" two stages: first mark out all the objects that need to be recycled, after the mark is completed, the unified collection of all tagged objects, Its tagging process has been basically introduced in the previous section about object tag determination. The reason is that it is the most basic collection algorithm, because the subsequent collection algorithms are based on this idea and improve their shortcomings. Its main shortcomings are two: one is the efficiency problem, the labeling and removal process is not efficient, and the other is a space problem, the mark after the purge will produce a large number of discontinuous memory fragmentation, too much space debris may cause, When the program needs to allocate large objects in the future, it cannot find enough contiguous memory and has to trigger another garbage collection action in advance . Tag-Clears the execution process of the algorithm



2) Replication algorithm in order to solve the efficiency problem, a collection algorithm called "Replication" (Copying) appears, which divides the available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is exhausted, copy the surviving object to the other piece, and then clean up the used memory space once. This makes each one of the pieces of memory recycling, memory allocation will not consider the complexity of memory fragmentation, as long as the mobile heap top pointer, in order to allocate memory, simple implementation, efficient operation. But the cost of this algorithm is to reduce the memory to half of the original, it is a little too high . The execution process of the replication algorithm



3) Tag-collation algorithm the replication collection algorithm performs more replication operations when the object has a higher survival rate and becomes less efficient. More crucially, if you do not want to waste 50% of space, you need to have additional space to allocate security, in order to deal with all the objects in the memory used in 100% survival extreme situation, so in the old age generally can not directly select this algorithm.
According to the characteristics of the old age, someone proposed another "mark-and-sweep" (mark-compact) algorithm, the marking process is still the same as the "tag-purge" algorithm, but the next step is not directly to the recyclable objects to clean up, but to let all the surviving objects moved to one end, Then directly clean out the memory outside the end boundary , the "mark-and-sweep" algorithm



4) Generational collection algorithms The current garbage collection of commercial virtual machines uses the "generational collection" (generational Collection) algorithm, which does not have any new ideas, but divides the memory into several blocks based on the different life cycles of the objects. The Java heap is generally divided into the new generation and the old age, so that according to the characteristics of each era to adopt the most appropriate collection algorithm. In the Cenozoic, every garbage collection is found to have a large number of objects died, only a small number of survival, then choose the replication algorithm, only need to pay a small number of surviving objects of the replication cost can be completed collection. In the old age, because the object has a high survival rate and no additional space to guarantee it, it must be recycled using the "mark-clean" or "mark-sweep" algorithm


The Java Virtual machine has several areas of memory refinement for the heap, and these areas are the collection algorithms that are used.


The JVM memory model is divided into two chunks, one is New Generationand the other is an old Generation. In new generation, there is a space called Eden, which is mainly used for storing new objects, and two survivor Spaces (From,to), which are used to store objects that survive each garbage collection. In old Generation, a memory object with a long life cycle in the application, and a Permanent Generation, are used primarily to put the JVM's own reflective objects, such as class objects and method objects.
1) in the new Generation block, garbage collection is generally used in the replication algorithm, fast. At each GC, the surviving objects are first copied from Eden to a survivor space, and when the survivor space is full, the remaining live objects are copied directly to the old generation. Therefore, after each GC, the Eden memory block is emptied

2) in the old generation block, garbage collection is generally marked with an algorithm that is slower, but reduces memory requirements.


Garbage collection sub-level, Level 0 for all (full) garbage collection, will reclaim the old section of garbage, Level 1 or above is a partial garbage collection, will only reclaim new garbage, memory overflow usually occurs after the old or perm segment garbage collection, still no memory space to accommodate the new Java object situation.


The out of memory only occurs when the JVM does not have enough memory after the old and perm generation have been reclaimed.
When a new object is generated, the memory request process is as follows:
A. JVM tries to initialize a chunk of memory in Eden for related Java objects
B. When the Eden space is sufficient, the memory request ends. Otherwise to the next
C. The JVM tries to free all inactive objects in Eden (this is a garbage collection of 1 or more advanced), and attempts to put some of the active objects in Eden into the survivor area if Eden space is still insufficient to fit into the new object after release
D. The survivor area is used as an intermediate swap area for Eden and old, and when the old area is sufficiently large, the objects in the Survivor area will be moved to the old area, otherwise they will remain in the survivor area.
E. When there is not enough space in the old area, the JVM performs a full garbage collection in the old area (level 0)
F. After a complete garbage collection, "Out of memory error" occurs if the survivor and old areas still cannot hold portions of objects copied from Eden, causing the JVM to be unable to create an area for the new object in the Eden area


Causes of full GC
New a lot of objects, not immediately in the active release of->eden memory is not enough, and constantly moved the object to old->old full->full GC

Summary: The above describes how the Java Virtual Machine Management objects, we also see the above is mainly the collection of algorithms and heap space from the new division, so that the purpose is to garbage collection efficient implementation, but it seems, if the object to the system to manage, the process efficiency of the system will certainly have an impact, But this is a bit better, that is, do not need manual management, to bring convenience to the program ape.

Java Virtual Machine Parsing article---garbage collector

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.