Personally, the garbage collection mechanism is one of the best functions in the Java language. It is existence with garbage collection mechanism, so that Java programmers don't have to consider memory leaks (of course, there is still a problem with the possibility of memory leaks), it is not only a question of delete (). The java language garbage collection mechanism is closely related to the mechanism of Java's single inheritance system and object space allocation. It can be said that the Java language has the entire architecture to build such excellent garbage collection mechanism. As for C , Bjarne Stroustrup refers to the problem of garbage recovery in his FAQ. He claims that there are some nice business and public domain garbage collectors, and C garbage collection and other languages that support garbage collection are better compared to the language of the garbage collection in the applicable program for garbage collectors. In addition, C also supports other memory management technology, and the memory can be managed safely without garbage collection. Unfortunately, I have C too, I have to study hard, study it.
Garbage Recycling is a dynamic storage management technology that automatically releases objects that are no longer referenced by programs, and implements resource automatic recycling according to specific garbage collection algorithms. Java programmers do not need to consider too many memory management, and virtual opportunities automatically carry out garbage recycling operations when the memory is tight. While releasing resources, the garbage collection mechanism will also clear the debris in memory. This feature enhances the efficiency of the object space allocation, because garbage recycling will close objects in memory, and the allocation of the new object is simply moved forward, and it is not necessary to traverse the stack to find the memory block of the appropriate size. Such Java allocates the efficiency of space in the heap even approximates allocation efficiency in the stack.
But garbage recovery will still take a lot of resources when the resource is released. The high and low efficiency is related to the algorithm used by the garbage collection mechanism.
The most simple and intuitive algorithm of garbage collection is the reference count. Each object has a reference count table. Add a reference to the object, count, and then reduce one. When garbage is recycled, the reference count is 0 to be released. This algorithm is very slow. Because the garbage collector is to traverse all objects in memory and release it when discovered that the reference count is zero. In addition, for a group of objects referenced by loop interaction, if the whole group object has become garbage, it is still possible to have a non-zero count. Therefore, the reference count is hardly used on any real virtual machine.
The algorithm used in the actual use is: starting from the REFERENCE in stack and static storage space, accessing the objects they pointing, and objects referenced inside these objects. The object accessed throughout the process also survived, and the unacceptable naturally became garbage. There are many mechanisms when dealing with objects. The main thing is "Stop-and Copy", "Mark and Sweep" and "Generation".
Stop-and-copy is about to be found in the program being executed, then Copy is in the new stack. At the same time, the Reference of the object will point to the new address. This approach allows objects to be compact in new stacks. But there are also some problems. This mechanism should maintain two stacks, the objects between the two stacks, so the memory to use this mechanism is twice the actual needs. In addition, if the program only produces little garbage, the garbage collection will still affect the object Copy to the new hem, seriously affect the execution efficiency of the program.
Mark and Sweep is also from stack and static storage, labeled the found object. After the entire markup process is completed, it will be cleaned, and the object that is not marked will be cleaned. This approach produces a discontinuous memory block. To make memory arrangements, you must re-organize work. The above two practices must be carried out in the abstinence procedure.
The generation method divides the memory into a number of large blocks called Generation. There will be a generation record in each block. The new block is more "young". After each garbage collection, the object in which you viable in the young block is moved to the elderly block. This method is suitable for processing a large and short-lived small object. A more stable object will be in the elderly block, will not be copied, will only change its generation record. Virtual machines typically adopt a self-organized garbage disposal mechanism. He will choose the appropriate garbage collection mechanism based on the memory situation at the time. For example, Mark-and-SWEEP is used when the memory object is stable, reducing the number of copys. If the debris in memory is too much, the method using stop-and-copy is compact.