In-depth garbage collection of Java virtual machines

Source: Internet
Author: User

Objective:

Speaking of garbage collectors, Java developers must have heard the ears cocoons. If you were to design a Java garbage collector, what would you focus on?

// 1.哪些内存需要回收?// 2.什么时候回收?// 3.如何回收?

This blog post is the answer to these questions. Gossip doesn't speak much, start writing.

Which memory needs to be recycled?

Let's look at the knowledge points of the runtime data area first. We all know that the program counter , the virtual machine stack , the local method stack are all dead with the line Cheng. The stack frame allocation in the stack is determined when there is a class structure defined. So their memory allocation and recycling are very deterministic, because the method ends or the thread ends up memory is naturally recycled.
However, the Java heap and the method area are different, multiple implementation classes in an interface may require different memory, and multiple branches of a method may require different memory, and only during program execution can you know which objects are created. This part of the memory allocation and recycling are dynamic, the garbage collector is concerned about this part of memory.

When do I recycle?

In the Java heap, where almost all of the object instances in the Java world exist, the first thing to do before the garbage collector reclaims the heap is to identify objects that cannot be used by any other means . This is of course given to the algorithm to do, the reference counting algorithm , the root search algorithm

    1. Reference counting algorithm

Add a reference counter to the object, and whenever there is a place to refer to it, the counter adds 1, and when the reference fails, the counter value is reduced by 1. If the counter is 0 at any time the object is impossible to be used again. The book says that the Java virtual machine does not use this algorithm to manage memory, because it is difficult to solve the problem of circular referencing between objects . For example: A object, B object, they reference each other, then their reference counter will not be 0, then the GC will never be able to reclaim them, in fact, they can be recycled. This case can also be used by the Java Virtual machine in the end is not the reference counter algorithm.

2. Root Search Algorithm
The basic idea of the algorithm is: through a series of objects called "GC Roots" as the starting point, starting from these nodes to search for the elements, the search for all traversed by the path is called the reference chain. When an object does not have any links to the GC roots, it indicates that the object is not available.

3. References
If the value stored in data of type reference is the starting address for another piece of memory, we say that this memory represents a reference. The citations are categorized as follows:
Strong references: (such as Object obj = new Object ()), objects that are never referenced by the garbage collector.

Soft reference: A description is also useful, but not a required object. These are added to the recovery scope for two recoveries before the system faces a memory overflow risk.

Weak references: Describes non-essential objects that only survive until the next garbage collection occurs.

Virtual references: Ghost references do not affect the time to live, nor can you get an object instance from a virtual reference. The only purpose is to receive a system notification when recycling.

How to Recycle

Garbage collection algorithm
It says that the algorithm is used to determine whether the object is included in the Recycle list, and then the garbage collection algorithm is explained. This paper introduces the thinking and development process of several algorithms, and the concrete implementation is not explained in detail.
Tag-Purge algorithm
Divided into two stages: mark, clear. The objects that need to be recycled are tagged, and all tagged objects are collected uniformly after the tag is complete. The so-called Mark is what is said above, how to judge that an object has been "dead". This algorithm is inefficient because both phases are inefficient, and when the reclamation is complete, a large number of discontinuous memory fragments are generated, and another garbage collection action is triggered when a large object needs to be allocated for memory.

Replication Algorithms
To solve the efficiency problem, a collection algorithm called replication appears, which divides the available memory into two equal-sized regions, using only one block at a time, and when a piece is exhausted, copies the surviving objects to another piece, then cleans up the current full chunk at once.

Tagging-sorting algorithms
Replication algorithms when the survival rate of an object is high, a large number of replication objects are required, which reduces efficiency and reduces the amount of available memory. Tag-cleanup algorithm and marker-collation algorithm the previous stage is the same, after the mark is completed, do not clean up, but the living object, move to one side, so that the memory of Sohu can be continuously together.

Note:
There is no perfect algorithm, only the algorithm for the scenario. The above algorithms have their own advantages and disadvantages, only one algorithm can not be well covered in the virtual machine memory recovery scenario. So there is a generational recovery algorithm. In fact, this is nothing new, that is, the object according to the survival cycle of different memory regions, so that different memory regions for different algorithms of garbage collection.

Generational recovery algorithm
The general Java heap is divided into the new generation and the old age. According to the characteristics of each generation, the appropriate algorithm is used for recycling. New generation of objects in the creation rate is very high, the death rate is very high, so suitable for the use of replication algorithm, as long as the cost of a small number of objects can be recycled. In the old age, where the object creation rate is low and survival is extremely high, it is appropriate to use the marker-collation algorithm.

Note:
The Java heap divides memory areas for memory recovery in a way that can be more nuanced, Eden space, Survivor space, tenured Gen;
Here is a comparison of the image easy to remember the way to explain, from generational recycling popular explanation
1, a person (object) out (new) will be in Eden Space (Eden) Carefree life, until the GC came to break their quiet life. The GC will ask each object what the case is, there is no money (reference to this object) Ah, because the GC wants to make money, money can blackmail it. Then the rich will enter Survivor space (Survivor area), and the poor will kill directly.
2, large objects directly into the elderly generation-pension area: Some world local tyrants born. His parents directly smashed tens of billions of, the status of eminent, into the old age, money is arrogant! Don't Go to Eden Space (Eden).
3, Survivor Space (Survivor area has two areas: living and no Man's land) why there are two districts from Survivor (living area), to Survivor (no man's land). Every time a GC wants to go to the survivor area to blackmail, it goes from Survivor (living area) all people brought to Survivor (no man's Land), and then began to blackmail, including this time 15 times local tyrants, into the old-age district, can't afford to kill the protection fee, did not meet 15 times, but also a little money in the hands of living in the No man's land, this no man's land has become living quarters, The former living quarters (people were removed) became no man's land.
4, not enter Survivor Space (Survivor area) after the guarantee that the person is safe, but at least can live a period of time.
The GC will be regularly (customizable) to extort these people, and every time the billionaire gives the money, the GC is satisfied and lets it enter the genured Gen (pension area).
(Each pass minor GC, will give this rich man to add a record, when some rich man has given about 15 years protection fee, you can go to raise the old quarter) Wanyuanhu not live several times blackmail there is no money, GC see no value, just kill off.
5, enter into the old-age area of the basic can ensure personal safety, but some billionaires will also splurge into poor, as long as the money is not, GC or kill off.

Extension: Garbage collector

The algorithm is the memory recycling methodology, and the garbage collector is the implementation of the memory recovery, the Java Virtual Machine specification does not have any about how the garbage collector implementation of the rules, so different vendors, different versions of the implementation of the garbage collector has a very big difference. is a garbage collector for the 1.6HotSpot JVM.

Note:
If there are contact instructions between the two garbage collectors, they can be used together. Each garbage collector's implementation of the content of their own access to information, I will no longer here code word. The following are some of the parameters related to garbage collection, which are very useful for tuning.

In-depth garbage collection of Java virtual machines

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.