How the Java garbage collection mechanism works

Source: Internet
Author: User

How the Java garbage collection mechanism works

"Bo Master" Gao Ruilin

"Blog Address" http://www.cnblogs.com/grl214

I. The role of the garbage collection mechanism introduced in Java

After we have built the class, we create the object and allocate the memory space, in order to prevent the memory space being full, Java introduces garbage collection mechanism, reclaims the objects that are no longer referenced, frees the memory, loops incrementally, and prevents the memory space from being full.

1. How the garbage collection mechanism works

The objects that are created are stored in the heap, likened to the yard land, the object to the land of the manager, the yard analogy to the Java Virtual machine, when an object is created, the Java Virtual machine will assign land to the object, when the object is no longer used, the JVM is released.

reference count: Each object contains a counter, when the object is referenced, counter technology + 1, when not referenced-1, that is, NULL, garbage collection mechanism for its loop, when the object is null, to reclaim the memory, although the cost is not very large, However, the garbage collection mechanism for the entire life cycle of an object is recycled, and it is found to be null to retract, but there is a flaw that when an object has a circular reference, the object that should be reclaimed and the counter does not display 0, so the garbage collection mechanism is not recycled back.

Adaptive technology:

stop-copy : Suspend the program's operation, loop all objects to find "surviving" objects, copy them into another heap, and then recycle them without copying, in order to not occupy the storage, introduced in the same heap, will not occupy the memory heap to hold the copy of the "Surviving object", Re-organize the allocation space. To prevent the occurrence of fewer objects that are not referenced, the cost of copying a lot of "surviving objects" into the tag sweep, the objects that loop-tagged the object to be copied into the other heap, and not referenced to free space. Finally, the JVM provides automatic detection monitoring, and if the objects are stable and the garbage collector is inefficient, use " tag-sweep ", otherwise use stop-copy to recycle.

How the Java garbage collection mechanism works

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.