Java GC Working mechanism

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/hzdtf/articles/5419987.html

GC: The garbage collector cleans up Java's useless heap objects and frees up memory to avoid a memory leak. Before introducing the Java Recycle Bin, first introduce the following recycling mechanisms

1. Reference count:

When an object A is referenced by other object B, object A refers to +1, and when the -1,GC works, the reference count in all objects is checked, and if 0 means to clear, >0 indicates that there are other object references that cannot be purged. One of the fatal drawbacks of this mechanism is that when two objects cross-reference, the two object arguments may never be 0 when traversed, and will never be deleted. This mechanism is never used by Java.

  2. Stop-copy:

  The program pauses to run, starting GC,GC from the heap or static storage to start traversing all objects, judging whether the object is "Live" object, if it is live not deleted, and vice versa. Deciding whether to "live" is to determine if the object is referenced by another object and to find it from the chain. When a live object, the GC will open from the other heap to avoid a large space, and then copy the live object into a new space, in the copy by the tight arrangement, while updating all the reference address as a new address, not live objects intact. After the traversal, and then traverse again, this time is not live object completely to delete.

Pros: All objects can be re-arranged tightly, without memory fragmentation, which can provide faster efficiency for creating objects later.

Disadvantage: Occupy a large amount of memory space, to the original object of twice times the space; this mode in any case all living objects to copy one, assuming that the object is very stable, only a small number of garbage objects or no garbage object, then has done the duplication work, wasting resources.

  3. Mark-Delete:

  The program pauses to run, starting GC,GC from the heap or static storage to start traversing all objects, judging whether the object is "Live" object, if it is live not deleted, and vice versa. Deciding whether to "live" is to determine if the object is referenced by another object and to find it from the chain. When a live object, the object will be given a marker symbol, the dead object is not marked, after traversing, the second traversal, only the marked object, all unmarked objects are deleted.

  

The 2nd and 3rd of the above are to traverse two times, and is the last time to perform a real deletion, compared to the 1th type is much slower, because to traverse 2 times, especially the 2nd species, but also copy the action, but these 2 to avoid the 1th type of fatal shortcomings.

Earlier JVMs used the tag-delete pattern, and after Java5, the JVM used adaptive technology to automatically switch between "stop-copy" and "Mark-delete" to achieve the fastest efficiency, depending on its state. On the 1th traversal, if you determine that the object needs to be recycled, the object's Finalize () method is executed first.

  How Java GC works:

The JVM allocates memory in large chunks, and if the object is large, the object itself is a block. The GC starts the "stop-copy" mode, and when the object is traversed, it encounters a live object, then copies the object into the new block, while the block algebra +1, if the object is large, remains fixed, algebra +1, if the garbage object is detected to be sparse, automatically switches to "mark-delete" mode, if the object is detected too fragmented, It will automatically switch to stop-copy mode to rearrange the memory tight distribution. This is adaptive technology.

When the JVM allocates memory, there is a heap pointer, the heap pointer points to the last allocated memory block, because the "stop-copy" mode, most of the objects are continuously arranged, then the heap pointer moves the next block, is not yet allocated memory, then do not have to find the entire heap space of unallocated memory, This has greatly improved the speed of creation.

  About the Finalize () method of an object:

  The Finalize method refers to some cleanup work performed when an object is completed, which is a protected method in the objects that cannot be invoked outside the environment. Essentially this method is called to the GC, and when to invoke the above has to say. This approach is to emphasize that further objects need to release unmanaged objects, which is a function of detection insurance, such as a class containing access to a stream content, as follows:

  

Class Accessstream
{
Private InputStream stream;

Private Boolean isclose = false;

/* (Non-javadoc)
* @see Java.lang.object#finalize ()
*/
@Override
protected void Finalize () throws Throwable
{
if (!isclose)
{
Stream.Close ();
}
Super.finalize ();
}
}

When this class is finished, the correct approach is to close the stream immediately (call Close ()), and sometimes because the programmer does not close the stream in time because some of the reasons for his rent are not closed, then rewrite Finalize () as the last insurance function, and when it is not closed, close the stream before releasing the object.

It is usually not recommended to rewrite finalize, in addition to requiring the programmer strict habits, the most important is that finalize is not executed immediately, even if the explicit invocation of System.GC () is not guaranteed to execute immediately, can only say that GC execution is recommended, whether the GC to execute, depends on the current memory consumption and other factors. If finalize cannot be executed immediately, this means that the stream should have been released earlier and that it would have taken a long time to release.

Java GC Working mechanism

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.