Reading Notes of Java Virtual Machine-policies on garbage collector and Memory Allocation,

Source: Internet
Author: User

Reading Notes of Java Virtual Machine-policies on garbage collector and Memory Allocation,

Almost all object instances in the java World are stored in the heap. The Garbage Collector needs to know which objects are still alive and which objects are already dead before reclaiming the heap. How can we determine whether the object is alive?  

 

I. Algorithm for determining whether an object is alive

1. Reference COUNTING METHOD

Implementation: Add a reference counter to the object. Each time a counter is referenced, the counter is incremented by 1; when the reference is invalid, the counter is subtracted by 1. Objects whose counter is 0 at any time cannot be used again.

Advantages: simple implementation and high efficiency.

Disadvantage: It is difficult to solve the issue of repeated references between objects.

2. Accessibility Analysis Algorithms

Implementation idea: Use the GC Roots object as the starting point to search down from these nodes and search for the path that has passed into the reference chain. When an object to GC Root is not connected to any reference chain, it proves that the object is unavailable.

Advantage: it can solve the problem of repeated object reference.

Disadvantage: I did not expect

 

  2. What objects can be used as GC Roots in java?

1. Objects referenced in the Virtual Machine stack (the local variable table in the stack frame)

2. Objects referenced by static class attributes and constants in the method Area

3. Objects referenced by JNI (Native method) in the local method Stack

 

Iii. Object Tag recycling process

If an object is not reachable in the accessibility analysis algorithm, will it be recycled?

The answer is no. These objects have a chance to be revived. To truly declare the death of an object, it must go through at least two marking processes: if the object does not find any reference chain connected to any GC Roots after the Accessibility analysis, it will be marked for the first time and filtered by the finalize () method. Under what circumstances will the finalize method of the object not be executed? 1. When the object does not overwrite the finalize () method. 2. The finalize () method of the object has been called by the virtual machine. If an object is determined to be necessary to execute the finalize method, the object will be placed in an F-Queue and will be executed by a low-priority Finalizer thread created by the virtual machine. The finalize method is the last chance for these objects to escape the fate of death. If an object wants to successfully save itself in finalize, you only need to re-associate it with any object in the reference chain, for example, assign a value to a variable or a member variable of an object. The second mark is that it will be removed from the "to be recycled" set. Otherwise, you have to wait for recovery.

However, the finalize method runs at a high cost and has a high uncertainty. Therefore, the call sequence of each object cannot be guaranteed. We strongly recommend that you do not use this method in daily development. If you need to disable External Resources, try-finally or other methods can be used to do better and more timely.

 

Iv. Garbage Collection Algorithms

1. Mark-clear Algorithm

Implementation Method: The tag algorithm is easy to implement. The previously introduced accessibility analysis algorithm is used to mark all objects to be recycled, and then all marked objects are recycled in a unified manner. It is the most basic collection algorithm, and subsequent collection algorithms are obtained based on this idea and its shortcomings are improved.

Disadvantages: low efficiency and memory fragmentation

2. replication algorithm V1

Implementation idea: divide the memory into two equal parts by capacity and use one of them each time. When one piece of memory is used up, the surviving objects are copied to the other, and the used memory space is cleared once. In this way, the whole half-zone memory is recycled each time, so you do not need to consider the memory fragmentation issue.

Advantages: simple and efficient, no memory fragmentation issues

Disadvantage: the memory size will be reduced to half of the original size at a high cost.

3. replication algorithm V2 (New Generation Algorithm)

Implementation idea: instead of the original solution that split the memory into two parts, the memory is divided into a larger Eden space and two smaller memory vor space. Each Eden uses one of the memory vor. When recycling, the objects that are still alive in Eden and vor are assigned to another vor space at one time. Finally, the Eden and the used vor space are cleared. By default, HotSpot virtual machines have a ratio of 8:1 between Eden and zoovor (research shows that 98% of the objects in the new era are born and died ), that is, the available memory space in each new era is 90% of the capacity in the new era. Only 10% of memory will be wasted. However, what if the memory occupied by the surviving object is greater than 10% of the memory occupied by the new era? This requires other memory (in the old age) for allocation guarantee.

Advantage: the disadvantage in the second point is improved.

4. Tag-sorting algorithm (algorithms used in the old age)

Implementation idea: the process is the same as the markup-clearing algorithm, but the subsequent steps do not directly clean the recyclable objects. Instead, all objects are moved to one end, and the memory outside the end boundary is cleared directly.

 

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.