Garbage collection Algorithms in Java

Source: Internet
Author: User

1 ) Two key elements of garbage collection:

Found useless objects.

Reclaims memory space for useless objects.

2 ) 6 Kinds of garbage collection algorithms

Reference counting method, tracing algorithm, compacting algorithm, copying algorithm, generation algorithm, adaptive algorithm.

3 ) Detail:

Reference counting method (Reference counting Collector)
Reference counting is the only method of garbage collection that does not use a root set, and the algorithm uses reference counters to differentiate between surviving and unused objects. In general, each object in the heap corresponds to a reference counter. When an object is created and assigned to a variable each time, the reference counter is set to 1. When an object is assigned to any variable, the reference counter is incremented by 1 each time the object is scoped (the object is discarded), the reference counter is reduced by 1, and once the reference counter is 0, the object satisfies the garbage collection condition.
A garbage collector based on a reference counter runs faster and does not interrupt program execution for long periods of time, and it is appropriate to run programs in real time. However, the reference counter increases the cost of executing the program because each time the object is assigned to a new variable, the counter adds 1, and each time the existing object is scoped, the counter is reduced by 1.

"Java Learning materials Free Collection: tangniu520666, Notes" 4 "need to come soon"

The

Tracing algorithm (tracing Collector)
Tracing algorithm is proposed to solve the problem of reference counting, 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.

Compacting algorithm (compacting Collector)
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. In the implementation of the collector based on the compacting algorithm, the handle and the handle table are generally added.
Copying algorithm (coping Collector)
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.
Generation algorithm (generational Collector)
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.
Adaptive algorithm (Adaptive Collector)
In certain cases, some garbage collection algorithms are better than other algorithms. The garbage collector based on the adaptive algorithm monitors the usage of the current heap and will select the appropriate algorithm for the garbage collector.

Garbage collection Algorithms in Java

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.