"In-depth understanding of Java Virtual Machines" Reading notes-garbage collector and memory allocation policy

Source: Internet
Author: User

In the heap, where almost all of the object instances in the Java world are stored, the garbage collector needs to know which objects are still alive and which objects are dead before the heap is reclaimed. How to judge whether the object is alive?  

First, determine whether the object survival algorithm

  1. Reference counting method

Implementation idea: Add a reference counter to the object. The counter is incremented by 1 whenever there is a reference to it, minus 1 when the reference fails. At any moment the object of counter 0 is impossible to be used again.

Advantages: Simple to achieve, high efficiency.

Disadvantage: It is difficult to resolve mutual circular references between objects.

  2. Accessibility Analysis algorithm

Realization idea: Through the GC roots object as the starting point, searches down from these nodes, searches through the path becomes the reference chain, when an object to the GC root does not have any reference chain to connect, proves the object is not available.

Advantages: It can be a good solution to the problem of circular referencing between objects.

Cons: I didn't think

  Second, in Java, which objects can be used as GC roots?

1. Objects referenced in the virtual machine stack (local variable table in the stack frame)

2. Static class properties and constants reference objects in the method area

3. Objects referenced by JNI (native method) in the local method stack

Third, the OBJECT tag recycling process

If an object is unreachable in the accessibility analysis algorithm, is it not the object that must be recycled?

The answer is no, these objects have a chance of resurrection. to actually declare an object dead, at least two times. Marking process: If the object finds no reference chain connected to any GC roots after the accessibility analysis, it is first labeled and filtered, The criteria for filtering is whether this object is necessary to perform a finalize () method. Under what circumstances does the object's Finalize method 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 judged to be necessary to perform a Finalize method, the object is placed in a f-queue queue, waiting for a low-priority finalizer thread created by the virtual machine to execute. The Finalize method is the last chance for these objects to escape the fate of death, if the object is to successfully save itself in finalize, simply re-associate with any object on the reference chain, such as assigning itself to a variable or member variable of an object. That in the second time the mark is that it will be removed from the collection "About to be recycled". Otherwise, you can only wait for recycling.

However, the Finalize method runs at a high cost and is uncertain, and cannot guarantee the order of calls of individual objects. This method is strongly not recommended in daily development , and if you need to work with "close external resources", you can do it better and in a timely manner using try-finally or other means.

Four, garbage collection algorithm

  1. Mark-Sweep algorithm

Implementation of the idea: the implementation of the tag algorithm is very simple, through the accessibility analysis algorithm described above to mark all the objects that need to be recycled, and then uniformly reclaim all the tagged objects. It is the most basic collection algorithm, and the subsequent collection algorithm is based on this idea and the improvement of its shortcomings is obtained.

Cons: Inefficient, resulting in memory fragmentation

2, Replication algorithm V1

Implementation idea: divide memory by capacity into two blocks of equal size, one at a time. When one piece of memory is used up, the surviving object is copied to the other, and then the used memory space is cleaned out once. This is done every time for the entire half of the memory to be recycled, regardless of the memory fragmentation issue.

Advantages: Simple and efficient, no memory fragmentation issues

Disadvantage: the memory will be reduced to the original half, the cost is high

  3, Replication algorithm V2 (new generation of the algorithm used)

The idea: Instead of splitting the memory into two scenarios, divide the memory into a larger Eden space and two smaller survivor space, each time Eden uses one of the survivor. When recycled, objects that are still alive in Eden and Survivor are assigned to another survivor space at a time, finally clearing out Eden and the survivor space just used. The default Eden and survivor size ratio for the hotspot virtual machine is 8:1 (the study shows that 98% of the objects in the new era are going to die), that is, the available memory space for each new era is 90% of the capacity of the entire new age. Only 10% of the memory will be wasted. But what if the surviving object consumes more memory than the new 10%? This relies on other memory (the old age) to make the allocation guarantee.

Pros: Improved the 2nd of the shortcomings.

  4. Marking-sorting algorithm (algorithm used in old age)

Implementation idea: The process is the same as the tag-purge algorithm, but the next step is not to clean up the recyclable objects directly. Instead, all objects are moved to one end, and then the memory outside the end boundary is cleaned up directly.

"In-depth understanding of Java Virtual Machines" Reading notes-garbage collector and memory allocation policy

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.