Garbage collector and memory allocation policy

Source: Internet
Author: User

This article refers to the book "in-depth understanding of virtual machines"

Does the virtual machine determine if the object is dead?

Garbage collection objects are primarily heap-in-memory objects that need to be judged before they survive, if they are recycled, and if they live, no GC is performed.

Algorithms used to determine whether an object can be recycled: a reference counting algorithm

Add a reference counter to the object, whenever there is a place to reference it, the number of the counter is added one, when the reference fails, the counter value is reduced by one, any time the counter value of 0 of the object is not possible to use the object, but it is difficult to solve the problem of circular reference between objects.

Mainstream algorithm: Accessibility analysis algorithm:

Through a series of objects called "GC Roots", from which the search takes place is called the reference chain, proving that the object is not available when an object is connected to the GC Roots without any reference chain.

GC Roots Object Types:

Objects referenced in the virtual machine stack, objects referenced by static properties in the method area, objects referenced by constants in the method area, objects referenced by native

Above is the idea of what kind of object to recycle and reuse, the accessibility analysis algorithm just shows that some non-conforming objects are not available, but does not pronounce the object's time of death, it still has the opportunity to revive, if there is non-conforming to the object of accessibility analysis, this object will be marked once, At this point it is in the probation stage, this is whether to sentence the object of the death penalty needs to determine whether the object executes the Finalize method, when the object does not overwrite the Finalize method, or the virtual machine has called the Finalize method, this is no need to execute the Finalize method, this object is sentenced to death, Need to be recycled. If an object executes a Finalize method and can save itself in a method, simply assigning itself to a class variable or a member variable of an object, the object will be removed from the collection that is about to be garbage collected and self-saved, and the Finalize method can only be executed once for each object.

Garbage collection for method areas

The object reclaimed by the method area is primarily an obsolete constant in a constant pool, which is very similar to the heap, such as a string "abc", and if no reference is directed to ABC, then ABC will be cleaned out of the constant pool by the system.

Garbage collection algorithm tag-purge algorithm

Objects that need to be cleared at the mark, and then cleared, but inefficient, resulting in more space fragmentation.

Replication Algorithms

Divide the available memory into two pieces, and once you're done, copy the surviving objects to another piece of memory, and then clear the memory space that you've used so that you can reclaim half of the memory each time, without creating a space fragmentation problem.

But a one-time loss of 50% of memory space, so the replication algorithm has been improved, the memory is divided into a larger Eden area and two smaller survivor area, each use of Eden and a survivor area, garbage collection, The Eden and survivor live objects are copied one at a time to another survivor area, and the size of Eden is 8:1, which can only cause 10% of the memory waste at once. Of course, we cannot guarantee that no more than 10% of the objects will survive each recovery, and when the survivor area is not enough, it is necessary to rely on other memory (old age) for allocation guarantees.

Tagging-sorting algorithms

Replication algorithms require a lot of replication, efficiency will be affected, and if the object 100% survived, need to rely on other memory to share, the old age generally do not use this algorithm, for the old age of the recovery has a labeling-sorting algorithm, the recyclable objects are marked first, and then all the surviving objects moving toward one end, Then directly remove the memory outside the boundary.

Generational collection Algorithms

For the characteristics of the new generation and the old age, the recovery algorithm used for the new generation, high mortality, all using replication algorithm, the old age survival rate is high, there is no additional space to guarantee, using the mark-clean or marker-collation algorithm.

Garbage collector and memory allocation policy

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.