Java garbage collection mechanism

Source: Internet
Author: User

1. Java's garbage collection mechanism works mainly on determining which objects are garbage, reclaiming the space occupied by useless objects, and keeping the surviving objects in the heap tightly aligned.

2. Java Garbage collection algorithm:

Reference count (basic not used):

    When a reference is directed to an object, the object's reference counter is +1;

    The reference counter-1 when the reference leaves the object waving marked null;

When the reference counter is 0 o'clock, the space occupied by the object is freed.

Flaw: If there is a circular reference between objects, the object may not be recycled.

Stop-copy:

    Object discovery:

All references are traversed from the stack and from the static store, the referenced object is found, and all references contained by the object are found to find all the living objects.
Processing:
Pauses the program to run, copying live objects from one heap to another;
Objects that are not copied are garbage, the copied objects are arranged on the new heap, and references to the copied objects need to be fixed;
Disadvantages:
Requires more space (two heaps);
For the stable state of the program, may only produce very little garbage, this time "stop-copy" the comparison of waste time;

  Marking--Cleaning:

Object discovery:

The process is similar to "stop copying", which sets the tag for the surviving object during traversal;

Processing:
Objects that are not marked are garbage, recycled, not duplicated, and may cause memory discontinuities.

  Generational garbage collection (based on the life cycle of the object, with different garbage collection algorithms in different regions):

Young objects are placed in young areas, and objects in this area face more frequent garbage collection;
After several garbage collection, the target of the young region will be put into the old area, and the frequency of garbage collection is relatively low.
When there is insufficient space in the old area, a global garbage collection is triggered.

    

Information on generational recycling:http://developer.51cto.com/art/201201/311696.htm

Java garbage collection 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.