1. The significance of garbage collection
In C + +, the memory of an object is occupied until the program is finished running, and cannot be allocated to other objects until it is explicitly released, whereas in Java the memory becomes garbage when no object reference points to the memory originally allocated to an object. A system-level thread in the JVM automatically frees the memory block. Garbage collection means that the object that the program no longer needs is "useless information," and that information is discarded. When an object is no longer referenced, the memory reclaims the space it occupies so that the space is used by the new object later. In fact, garbage collection can erase memory fragments in addition to releasing useless objects. Memory fragmentation occurs because the object is created and the garbage collector frees up memory space for discarded objects. Fragmentation is a free memory hole between the memory blocks assigned to an object. Defragmentation moves the heap memory occupied to one end of the heap, and the JVM assigns the sorted memory to the new object.
Garbage collection automatically frees up memory space and reduces the burden of programming. This makes Java virtual machines have some advantages. First, it can make programming more efficient. In the absence of a garbage collection mechanism, it may take a lot of time to solve a difficult memory problem. When programming in the Java language, the garbage collection mechanism can greatly shorten the time. Second, it protects the integrity of the program, and garbage collection is an important part of the Java language security policy.
A potential drawback to garbage collection is that its overhead affects program performance. The Java virtual machine must track the objects that are useful in running programs and eventually release the useless objects. This process takes a processor's time. Second, the incompleteness of the garbage collection algorithm, some of the previous garbage collection algorithm can not guarantee 100% of all the discarded memory. Of course, with the continuous improvement of garbage collection algorithm and the increasing efficiency of software and hardware, these problems can be solved.
2. Garbage Collection Algorithm Analysis
The Java language Specification does not explicitly describe which garbage collection algorithm the JVM uses, but any garbage collection algorithm typically does 2 basic things: (1) Discovering unwanted information objects, and (2) reclaiming the memory space occupied by unwanted objects so that the space can be reused by the program.
Most garbage collection algorithms use the concept of root set (root set), which is the collection of reference variables (including local variables, parameters, class variables) that are accessible to the executing Java program, and the program can use reference variables to access the object's properties and invoke the object's methods. Garbage collection first needs to determine what is accessible from the root and which are unreachable, and the objects from the root set are active objects that cannot be reclaimed as garbage, which also includes objects that are indirectly accessible from the root set. The root set, which can not be reached through any path, is eligible for garbage collection and should be recycled. Here are a few common algorithms.
2.1. Reference counting method (Reference counting Collector)
Reference counting is the only method that does not use the root set for garbage collection, which uses reference counters to differentiate between surviving objects and objects that are no longer in use. In general, each object in the heap corresponds to a reference counter. When you create an object each time and assign it to a variable, 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 out of scope (discarded by the object), the reference counter is reduced by 1, and once the reference counter is 0, the object satisfies the garbage collection condition.
The garbage collector based on reference counters runs faster and does not interrupt program execution for a long time, and it is appropriate to run the program in real time. However, the reference counter increases the overhead of program execution because each object assigns a new variable to the counter plus 1, and each time an existing object is scoped, the counter is reduced by 1.
2.2. Tracing algorithm (tracing Collector)
The tracing algorithm is proposed to solve the problem of reference counting method, which uses the concept of root set. The garbage collector based on the tracing algorithm begins scanning from the root set, identifying which objects can be reached, which objects are unreachable, and marking the accessible objects in some way, such as setting one or more bits for each accessible object. In the scanning identification process, garbage collection based on the tracing algorithm is also known as the tag and Purge (Mark-and-sweep) garbage collector.
2.3. Compacting algorithm (compacting Collector)
To solve the heap fragmentation problem, 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, the other end of the heap becomes an adjacent free memory area, and the collector updates all references to all the objects it moves. So that these references can identify the original object in a new location. In the implementation of the collector based on the compacting algorithm, the handle and the handle table are generally added.
2.4. Copying algorithm (coping Collector)
The proposed algorithm is designed to overcome the cost of the handle and to solve the garbage collection of heap fragments. It starts by dividing the heap into an object area and multiple free areas, the program allocates space for objects from the object area, and 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 scratch area (so that there is no idle interval between the active object's memory), which The sample free area becomes the object area, the original object area becomes the free area, the program allocates the memory in the new object area.
A typical garbage collection based on coping algorithm is the stop-and-copy algorithm, which divides the heap into the object area and the free region area, and the program suspends execution during the handoff between the object area and the free region.