Introduction
The Java heap is a run-time data area in which instances (objects) of the class allocate space. The Java Virtual machine (JVM) heap stores all objects created by running applications, which are established through directives such as new, NewArray, Anewarray, and Multianewarray, but they do not require program code to be explicitly released. In general, the heap is responsible for garbage collection, although the JVM specification does not require special garbage collection techniques or even garbage collection at all, but because of the limited memory, the JVM has a heap managed by garbage collection when implemented. Garbage collection is a dynamic storage management technology, which automatically frees objects that are no longer referenced by the program, and implements the function of automatic resource recovery according to the specific garbage collection algorithm.
The meaning of garbage collection
In C + +, the memory occupied by an object is occupied until the end of the program and cannot be assigned to other objects until it is explicitly released, whereas in Java the memory becomes garbage when there is no object reference to the memory originally assigned to an object. A system-level thread of the JVM automatically frees the block of memory. 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 later used by the new object. In fact, in addition to releasing useless objects, garbage collection can also erase memory-logged fragments. The memory is fragmented because the object being created and the garbage collector frees up the memory space that the discarded objects occupy. Fragmentation is a free memory hole between the blocks of memory allocated to an object. Defragmentation moves the occupied heap memory to one end of the heap, and the JVM allocates the compiled memory to the new object.
Garbage collection can automatically free up memory space and reduce the burden of programming. This gives Java virtual machines 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 hard-to-understand 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 strategy.
One potential drawback of garbage collection is that its overhead affects program performance. The Java Virtual machine must trace the objects that are useful in the running program and eventually release the useless objects. This process takes the processor's time. Secondly, the incompleteness of garbage collection algorithm, some garbage collection algorithms used earlier can not guarantee that 100% collected all the discarded memory. Of course, with the continuous improvement of garbage collection algorithm and the efficiency of software and hardware running, these problems can be solved. Shenyang 463 Plastic Surgery Hospital http://www.hengnaya.com/
Algorithm analysis of garbage collection
The Java language Specification does not explicitly describe what garbage collection algorithm the JVM uses, but any garbage collection algorithm typically does 2 basic things: (1) Discovers useless information objects, (2) reclaims the memory space occupied by the useless 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 a collection of reference variables (including local variables, parameters, class variables) that are accessible by a Java program that is executing, and the program can use reference variables to access the properties of the object and methods of invoking the 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. Here are a few common algorithms.
1. Reference counting method (Reference counting Collector)
Reference counting is the only method of garbage collection that does not use the root set, 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 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. Shenyang 463 Plastic Surgery Hospital
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.
2. Tracing algorithm (tracing Collector)
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 (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.
4. 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.
5. 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.
6. 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.
Comprehensive analysis of Java's garbage collection mechanism 1