Objective:
The only reason to use the garbage collector is to reclaim memory that the program is no longer using.
Target objects for:
The Java garbage collector automatically reclaims Java objects that are no longer in use and frees up memory. However, the reclaimed memory that is created with new is allocated on the heap.
Finalize ():
So, how do you recycle objects that are not created in this way? For example: Java calls the native C method to create an object, then the object is not placed on the heap. Unless you manually call C's free () method, the object will never be cleaned up.
The Finalize () method of Java solves the problem above. When the garbage collector reclaims a garbage object, it first calls the Finalize () method of the object. Therefore, you can call the free () method of C in the Finalize () method.
The general textbook will write that Finalize () is used for cleanup before garbage collection, and in fact, in addition to the very few cases mentioned above, we generally do not need to use Finalize ().
Not guaranteed to occur:
Although the Java garbage collector automatically cleans up memory based on the object's usage, it doesn't necessarily happen, and if the memory is enough, the virtual machine will not waste time doing cleanup work.
How to tell if a Java object can be recycled:
1. Non-used "reference counter method":
Each object contains a reference counter that, when a reference variable points to the object, references counter +1, when the reference variable no longer points to the object, or is set to NULL, counter-1. Such as:
When the fourth situation occurs, that is: There is no reference variable pointing to the object "John Doe", at this point, the garbage collector at the appropriate time will be John Doe where the object is collected.
It is simple and convenient, but the reason why it is not used by the Java Virtual machine is that it cannot solve the problem of circular references. To give a simple example:
Obja has a instance variable, OBJB also has a instance variable, let Obja instance point to the B object, and let OBJB's instance variable point to a object, then the B object and a object reference counter is 1, not 0, If you follow the method of referencing counters, A and B cannot be recycled, but the fact is that both reference variables, Obja and OBJB, are already null (the specific object they point to is no longer referenced).
2. Root Search algorithm
In mainstream business programming languages (Java and C #, and even the old Lisp language), the root search algorithm (GC Roots tracing) is used to determine whether an object survives.
As mentioned before, the reference to the object is placed in the stack, and the reference to the constant is placed in the constant pool.
The idea of the root search algorithm is to iterate through all the reference variables from the constant pool and the reference variable in the stack to find all the live objects (the reference is not NULL). Then continue to look for all the references that this object contains, and repeat until all the reference networks have been accessed.
A reference variable in a constant pool or stack is the root node, and the entire network that extends out is a reference chain. Finally, if the path to the root node is found to be unreachable, the object is recyclable, which solves the problem of circular referencing:
For example, Gcroots is the root node, OBJECT5, 6, and 7, although each reference, but they are unreachable to gcroots, so, they can be recycled.
How to recycle?
The recycling algorithm used by each virtual machine is different, and the classic case is as follows:
Tag-Purge algorithm:
While searching for reference variables using the root search algorithm, the virtual opportunity makes a mark on each surviving object and clears the entire tag when it is complete.
The problem is that the surviving objects are not stored continuously in the heap, so when the "dead" object is cleared, there will be a lot of fragmentation in memory, and if there is not enough memory space to use the large memory objects later, the memory should be re-organized. Before recycling:
After recycling:
Replication algorithm:
It divides the available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is exhausted, copy the surviving object to the other piece, and then clean up the used memory space once. Before recycling:
After recycling (the surviving objects are moved to the right, the left side is clean and clean.) When the right side needs to be cleaned up, similarly, move the surviving object to the left and then clear the right side):
The disadvantage of this approach: it is clear that the available memory is only the original half. There is also a drawback: if the left side of a large number of objects are surviving, the cleanup will still have to move to the right, it is a waste of time.
Today's commercial virtual machines use this collection algorithm, but the ratio of the reserved area to the operating area is different, and in detail, the heap memory is divided into the new generation, the old age. The New Generation (young) is divided into three regions: Eden, from Survivor, to Survivor. For the new generation, old age, heap memory, etc., detailed information about the Java Virtual machine can be consulted.
Resources:
1.Thinking in Java 5th. 5.4 Chapter.
2.cnblogs:java garbage Collector:
Http://www.cnblogs.com/gw811/archive/2012/10/19/2730258.html
3.blogjava:java Heap Memory:
Http://www.blogjava.net/fancydeepin/archive/2013/09/29/jvm_heep.html
For more information, please follow the subscription number: It_pupil
A talk about garbage collector