|
1. Garbage collection mechanism is only responsible for recycling heap objects in memory and will not recover any physical resources (such as database connections, network IO and other resources)
2. The program can not accurately control the garbage collection operation, the garbage collector runs at the right time. When the object is a permanent loss of reference, the system will recover its occupied memory at the right time.
3. Before any objects recovered in garbage collection, there is always the first to call its finalize () method.
Object in memory state:
Reachable state: When an object is created, if more than one reference variable reference to him, then the object is reachable in the program, the program can be called instance variables and methods of the object variable by reference.
Recoverable state: If the program is not a variable in any reference variables refer to it, it enters a recoverable state. In this state, the garbage collection mechanism will be ready to reclaim the variable corresponding to the memory, prior to recovery of the object, the system calls used to restore the object fnalize () method, if at this time there is a reference to the object variable reference, the Object re become unreachable.
Unreachable: when used in reference objects and variable contact is cut off, and the system has been called the object's finalize () method, still does not make the object becomes unreachable. In this case the object into the unreachable state, only when the object is unreachable, the system will begin recycling the resources occupied by the object.
public class StatusTranfer {
public static void test () {
// When performing this step, the object is reachable 12345
String a = new String ( "12345");
// Perform this step when the object 12345 in a recoverable state
// Objects up to 123 in state
a = new String ( "123");
}
public static void main (String [] args) {
test ();
}
}
Mandatory garbage collection
Program can control when a target is not referenced variable references, but you can not control when garbage collection.
Java program can not precisely control the timing of garbage collection, but still be able to force the garbage collection system ---- such coercion only notification system for garbage collection, but the garbage collection system is still uncertain. Most of the time, the program forces the system garbage collection will have some effect.
Mandatory garbage collection in two ways:
1. Static class method call System gc (): System.gc ()
2. Call Runtime's gc () instance method: Runtime.getRuntime () gc ().. |
|
|
|