Why do you want garbage recycling? Because the JVM itself implements memory stacks, it is impossible to have automatic variables (temporary variables) of C , so all objects have to be collected. Let's take a look at C :
ClassType VarName; // Auto variable, created in the memory stack, automatically disappears with the disappearance of the work domain.
ClassType VarName = New classType (); // Create in the inner stack, to be manually released by the programmer, such as:
Delete varname; equivalent to Free of C.
Since the JVM itself is implemented in the internal stack, it is impossible to create an automatic variable. All objects have manual release. This "manual" refers to the implementation of the program, does not necessarily want programmer programming, JVM will implement itself. The release of the "General" object, this is the JVM garbage collector.
However, for non-"conventional" objects, such as direct or indirectly call local resources. (Some people think that the image is not called the local method, in fact it is just AWTs in indirectly call the local method), is the programmer's own programming to release resources. .
A object is marked with garbage (recycled object)? The previous implementation uses "reference count", which is an object when the reference count adds 1. When the reference handle disappears, the reference count reduction is 1, when the reference count is 0, it can be recovered.
Someone said that the NEW is not recycled?
The survival of an object is divided into new generation, old birth, new generation, and divided into Eden and two survival spaces. It guarantees that there is a space at any time, the object is just in Eden, garbage collector Will not recycle objects in Eden,
Only when the object is full of Eden, it is copied to the next living space. When the living space is full, a small recycle will occur. When the object is a "aging" value, it is copied to the old when the living space is too long. Born, there is a big recycling when the older is full.
However, Java is not a reference count to mark whether an object can be recycled. Because the object in Java is likely to be referenced directly or indirectly, it is an attribute that reference to object B in Object A, and the object B has referred one one. Attributes, this may cause reference to 0.
Java is a "by root traversal" to mark the object, which is to find the object referenced by the handle, then find the object referenced by this object, which recursively finds the leaves from the root of the tree, If an object is not found to find out that it has no object to reference it.
How to release
Non-JAVA objects? We have already said that conventional Java objects will be recycled by JVM's garbage collectors, but for local resources (generally to call local resources), we must release it.
Generally speaking, we can define the code to release the local resource in Finalize (), but this method is not certain, Finalize () is not running when the object is exited, but is called when the object is called as garbage. It is possible that JVM has not needred, so this method will never be called, and if the required object must be a slight object before exiting, you must write the code that releases the object in the finally block of Try {} finally {}. This is why I have repeatedly emphasized the number of discharge data. It must be written here. There is also a method in Java1.1 is System.RunfinalizersoneXit (), but it is not as Finally to be more effective.
The same system.gc () method does not guarantee the occurrence of garbage collection, it is just "suggestions", while when the garbage returns? Small recycling is full of survival, the big recycling is the old life, this is just a premise. Because Garbage Recycling is a low priority manner, only when other threads hang, it starts to release the memory of the object.