In-depth understanding of Java Virtual Machine garbage collection One

Source: Internet
Author: User

"Survival or death" how to determine whether the object is alive? Two algorithms are given in this paper, namely, the reference counting algorithm and the accessibility analysis algorithm.
Reference counting algorithm

The idea of the algorithm is simple and easy to implement. We add a reference counter to the object, and when there is a place to reference it, the reference counter adds one, and when the reference fails, the counter is reduced by one, and when the counter is 0 o'clock, the object cannot be referenced again.
Objective evaluation, the algorithm is highly efficient, in many cases is a good algorithm, but at least the mainstream Java Virtual machine is not adopted by this algorithm. The reason is that the algorithm cannot resolve circular reference problems between objects.
What is a circular reference? I think it is a nested reference between objects, the object B is assigned to the field in object a instance, and then the object A is assigned to the field in object B instance, thus forming a nested mutual reference relationship, the reference counter will never be 0, according to the algorithm, Two objects will naturally not be recycled, but this is not the case, the virtual machine has successfully reclaimed two objects (refer to the "in-depth understanding of Java Virtual Machine" 63 page code).

Accessibility analysis algorithm


It can be seen intuitively that determining whether an object is alive can check if the object is associated with a GC roots, or from GC roots to whether there is a path to the object, which is also known as a reference chain (Reference Chain). If available, the object can be referenced, and if the object is unreachable, the object is not referenced.
In the Java language, objects that can be used as GC roots include the following:

  1. Objects referenced in the virtual machine stack (local variable table in the stack frame)
  2. Object referenced by class static property in method area
  3. Objects referenced by constants in the method area
  4. The object referenced by JNI (that is, the general native method) in the local method stack
Another reference:

There are only two reference relationships in Java, reference and unreferenced, this definition is narrow, when we need to describe objects such as "chicken ribs", this description seems powerless, so we introduced several other references:

  1. Strong references (only good references still exist, the garbage collector does not recycle the referenced objects)
  2. Soft references (these objects will be listed in the Reclaim scope for a second recycle before the system will have a memory overflow exception)
  3. Weak references (weak references are associated with objects that only survive until the next garbage collection occurs)
  4. Virtual reference (whether an object exists by a virtual reference, does not affect its time-to-live, nor can it obtain an object instance through a virtual reference)
object of Death

In the accessibility analysis algorithm is unreachable, or no reference chain of the object will not immediately die, to formally announce the death of the object, requires two stages:
In the first stage, there is no reference chain found in the accessibility analysis algorithm, then the object will be marked for the first time and filtered once. Let's take a look at the filtering process, and the criteria for filtering is whether the object is necessary to execute the Finalize () method.
When the object does not overwrite the Finalize () method or the Finalize () method has been called by the virtual machine, the virtual machine treats both cases as "no need to execute".
When the object is judged to be necessary to execute the Finalize () method, then the object will be placed in a queue called queues and then executed by a low-priority finalizer thread that is automatically established by a virtual machine later (execution does not assume that the virtual opportunity waits for it to end), Then (the second stage), the GC will make a second small mark on the objects in the F-queue queue, and if the object is successfully referenced in the Finalize () method (associated with any object on the reference chain), then the object will be moved out of the collection that is about to be recycled, and vice versa. The object will be reclaimed by GC.

readers are advised to refer to the 67-page code of "in-depth understanding of Java Virtual Machine" by teacher Zhou Zhiming
Recycling Method Area

Garbage collection of method area (permanent generation) mainly recycles two parts: obsolete constants and useless classes
Let's look at an example of the recycling process for obsolete constants:

If a string "Hello world!" has entered the constant pool, but the current system does not have any string object called "Hello world!" , in other words, there is no string object referencing "Hello world!" in a constant pool constants, and there is no other place to reference this literal, then if a memory recycle occurs, the constant will be cleaned out of the constant pool by the system.

Let's take a look at how to determine if a class is a "useless class"

  1. All instances of the class have been reclaimed, that is, no instances of the class exist in the Java heap
  2. The ClassLoader that loaded the class have been recycled
  3. The corresponding Java.lang.class object of this class is not referenced anywhere, and cannot be accessed from anywhere by reflection on the method of the class
The symbolic references to other classes (interfaces), methods, and fields in a constant pool are similar to this virtual machines can reclaim the useless classes that meet the above three conditions, but they are not the same as the objects, they will be recycled if they are not used, or if the classes are recycled, the virtual machine gives the relevant parameters to adjust garbage Collection Algorithm

The word is unimportant, mainly see the figure ↑

Demonstrates the tag-purge algorithm
As the name implies, the tag-purge algorithm is divided into two phases: the tagging phase and the purge phase
The tagging phase has been described before, and readers can view the above; Clear phase clears objects that have been flagged
The disadvantage of the mark-clear algorithm is mainly two, the first: the algorithm marks and clears the efficiency of the two processes is not high; second: The algorithm clears the recyclable objects resulting in a discontinuous memory space, when allocating large contiguous objects, it is necessary to trigger a garbage collection action in advance.

The word is unimportant, mainly see the figure ↑

Demonstrates the replication algorithm
The basic idea of the copy algorithm is to divide the memory space into two parts of the same size, and when this half of the memory runs out, it copies the surviving objects to the other half of the memory, and then cleans up the current half of the memory space at once.
The algorithm perfectly avoids the shortcomings of the tag-purge algorithm, and also wastes a lot of memory space.
Add :
The hotspot virtual machine is divided into a larger Eden space and two smaller survivor space, each time using Eden space and one of the survivor, when recycled, Copy objects that are still alive in Eden and survivor to another survivor space, and finally clean up Eden and survivor space. The hotspot virtual machine is allocated a ratio of 8:1, that is, each effective use of space is 90%
When survivor space is not enough? How is the virtual machine handled? Here's a question about assigning a guarantee (similar to a mathematical model of bank lending), which we'll cover in more detail in our next blog post.

The word is unimportant, mainly see the figure ↑

Demonstrates the tagging-grooming algorithm
The proposed marking-finishing algorithm is related to the old age. The execution of the entire algorithm is the same as the tag-purge algorithm, but the subsequent steps are not to clean up the tagged objects directly, but rather to have all surviving objects move to one side, and then directly clean out the memory outside the end boundary.

Generational Collection Algorithms

The algorithm draws the advantages of the copy algorithm and the marker-collation algorithm. We divide the Java heap into the new generation and the old age, so that we can choose the algorithm according to the characteristics of each age:

The new generation chooses the replication algorithm, the old age chooses the mark-collation algorithm.

How does a hotspot virtual machine initiate memory reclamation? After this question I will specially send a post to carry on the simple introduction, here does not introduce in detail!

The copyright belongs to the author 0xTsmon and blog Park all, welcome reprint and Commercial, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

In-depth understanding of Java Virtual Machine garbage collection One

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.