Java garbage collection mechanism

Source: Internet
Author: User

1. Garbage collection mechanism (GC)
Garbage collection is the recycling of objects that are no longer used in memory;
(1) Garbage collection steps:
1) Find objects that are no longer used in memory;
2) Release the memory occupied by these objects;
(2) Find object methods that are no longer used in memory:
1) Reference counting method
If an object is not pointed to by any reference, it can be considered garbage;
Disadvantage: Can not detect the existence of the ring;
2) Root Search algorithm:
The root search algorithm is based on a series of objects called "GC Roots" as the starting point,
Starting from these nodes, the search goes through the path that becomes the reference chain, when an object
When no reference chain is connected to the GC roots, the object is considered not to be referenced.
(3) The memory that these objects occupy

There are three types of recovery algorithms that must be generated:
1) Mark-Copy
It divides the available memory capacity into two blocks of equal size, using only one piece at a time. When this piece is out of use,
Copy the surviving object to another piece, and then remove the memory space that has been used once.
It has the advantage of being simple, efficient, and without memory fragmentation. The disadvantage is that it requires twice times more memory to manage.
2) mark-Clean up
The markup cleanup algorithm is divided into "mark" and "clear" two stages: first mark the objects that need to be recycled,
The object is uniformly purged after the tag is complete. Its advantage is high efficiency, the disadvantage is that it is easy to produce memory fragmentation.
3) Marking-finishing
The tag action is consistent with the tag-clean algorithm, and subsequent operations are not just cleanup objects directly,
Instead, after cleaning up the useless objects, let all the surviving objects move to one end and update the pointers that reference their objects.
Because you want to move an object, it is less efficient than mark-clean, but does not produce memory fragmentation.
4) based on generational assumptions
Because the object has a long and short survival time, for long-lived objects,
Reduce the number of GC times to avoid unnecessary overhead. So we can divide the memory into the new generation and the old age,
The new generation stores newly created and short-lived objects, which store long-lived objects in the old age.
In this way, the efficiency of GC can be greatly improved by cleaning up the younger generation only, and then cleaning up the old age only when necessary.
Save GC time.









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.