Talking about Java garbage collection mechanism

Source: Internet
Author: User

Recently read some articles about the garbage collection mechanism, summarize.
To go back to Java garbage collection mechanism, answer from the following three aspects:
1. What memory will be recovered?
2. When is it withdrawn?
3, how to recover?

First, talk about which objects will be retracted.

    通过根搜索算法(GC Roots Tracing)判断对象是否还活着。该算法通过一系列的名为“GC Roots”的对象做为起始点,通过这些节点向下搜索,搜索所走过的路径称为引用链,当一个对象到GC Roots没有任何引用链接(用图论来说就是从GC Roots到这个对象不可达),则说明该对象是不可用的,它们将会被判定为可回收的对象象。

Second, when to reclaim these memory.

    cpu空闲或者内存空间不足时,进行垃圾回收,而程序员本省无法控制何时进行垃圾回收。

Finally, how to do garbage collection.
The Java language Specification does not explicitly describe which garbage collection algorithm is used by the JVM, and most garbage collection algorithms use the concept of root set (root set), which is a collection of reference variables (including local variables, parameters, class variables) that are accessible to the Java program that is being executed. A program can use reference variables to access the properties of an object and methods of invoking an object. Garbage collection preferences need to determine which ones are accessible from the root and which are unreachable, objects that can be reached from the root set are active objects, and they cannot be recycled as garbage, which also includes objects that are indirectly accessible from the root set. The root set, which is unreachable by any path, is eligible for garbage collection and should be recycled. There are roughly the following methods:
1. Reference counting method (Reference counting Collector)

   引用计数法是唯一没有使用根集的垃圾回收的法,该算法使用引用计数器来区分存活对象和不再使用的对象。一般来说,堆中的每个对象对应一个引用计数器。当每 一次创建一个对象并赋给一个变量时,引用计数器置为1。当对象被赋给任意变量时,引用计数器每次加1当对象出了作用域后(该对象丢弃不再使用),引用计数器减1,一旦引用计数器为0,对象就满足了垃圾收集的条件。

2. Tracing algorithm (Tracingcollector)

The tracing algorithm is proposed to solve the problem of the reference counting method, which uses the concept of the root set. The garbage collector based on the tracing algorithm starts scanning from the root set to identify which objects are available, which objects are unreachable, and to mark objects in some way, such as setting one or more bits for each object that can be reached. During the scan recognition process, garbage collection based on the tracing algorithm is also known as the Mark and Purge (mark-and-sweep) garbage collector.
  
3. Compacting algorithm (Compactingcollector)
To solve the problem of heap fragmentation, garbage collection based on tracing absorbs the idea of the compacting algorithm, in which the algorithm moves all objects to one end of the heap, and the other end of the heap becomes an adjacent free memory area, and the collector updates all references to all objects it moves. This allows these references to recognize the original object in the new location.
    4. Copying algorithm (Copingcollector)
The algorithm is proposed to overcome the overhead of the handle and to solve the garbage collection of heap fragments. It begins by dividing the heap into an object face and multiple free polygons, the program allocates space for the object from the object surface, when the object is full, garbage collection based on the coping algorithm scans the active object from the root set and copies each active object to the free surface (so that there is no free hole between the memory occupied by the active object), The idle face becomes the object face, the original object face becomes the idle surface, and the program allocates memory in the new object face.
A typical garbage collection based on coping algorithm is the stop-and-copy algorithm, which divides the heap into object and idle area polygons, and the program suspends execution during the switching between the object surface and the idle area.
    5. Generation algorithm (Generationalcollector)
One drawback of the stop-and-copy garbage collector is that the collector must replicate all active objects, which increases the program wait time, which is why the coping algorithm is inefficient. In the program design there is such a law: most objects exist for a short time, a few of the existence of a long time. Therefore, the generation algorithm divides the heap into two or more, each sub-heap as the object's generation (generation). Because most objects exist for a shorter time, the garbage collector collects these objects from the youngest child heap as the program discards objects that are not used. After the generational garbage collector runs, the last surviving object is moved to the next highest generation sub-heap, saving time because the old generation of sub-heaps is not often recycled.
There are many algorithms for garbage collection, such as reference counter algorithm, tag clearing algorithm, copy algorithm, marker finishing algorithm, and with the development, more and more is the generation of collection algorithm. It is divided into the Cenozoic and the old age, the new generation is suitable for frequent creation, and high mortality. The old age is suitable for the high survival rate. The way memory is allocated is shown in.

The proportion of the new generation and the old age was 1:2. A large number of rows created objects are created on the Eden space, but the surrogate will be created directly in old age, avoiding the frequent movement of large objects. A large number of newly created objects will soon die, that is, to become unreachable objects, when there is insufficient memory space on Eden, the minor GC will reclaim the memory on Eden Space and from space, use the copy algorithm, and copy the live objects into the to space. At this time all objects ' age ' +1, when reaching a certain ' age ' will be copied into the old age, while the old age also for the Cenozoic replication algorithm, there is the possibility of generating insufficient memory space issues provide a guarantee. At this time to Sapce next becomes from space, has been circulating. In the old years, when there was not enough memory space, a full GC was performed, using the tag-grooming algorithm.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Talking about Java garbage collection mechanism

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.