http://blog.csdn.net/mengern/article/details/38150431
Java provides a method for garbage-forced reclamation (SYSTEM.GC), but the system does not guarantee immediate garbage collection, but rather the JVM determines, based on a defined set of garbage collection algorithms, that the algorithm is used to improve the efficiency of garbage collection.
The basis for judging whether a storage unit is garbage is whether the object that corresponds to the storage unit is still being used by the program, that is, whether a reference is pointing to the object. The Java garbage collector automatically scans the object's dynamic memory area, marks the referenced object, and then collects the unreferenced objects as garbage and frees them.
Java does not provide a destructor method, but provides a similar approach: protected void Finalize ();
Program Description:
[Java]View PlainCopy
- Class J_book {
- private String name;
- J_book (String name) {
- this.name = name;
- }
- //Override the default Finalize method
- protected Void Finalize () {
- System.out.println ("book,\" "+ name + " \ ", is destroyed!");
- }
- }
- Public class J_finalize {
- public static void Main (string[] args) {
- J_book Book1 = new J_book ("Gone with Wind");
- ///Anonymous instance, determined by Java as garbage memory, will be treated as garbage collection
- New J_book ("Java How to Program");
- New J_book ("Roman Holiday");
- Book1 = New J_book ("thingking in Java");
- //Forced garbage collection, note that the compilation system does not immediately garbage collection, and its own algorithm to decide when to perform garbage collection
- System.GC ();
- }
- }
Program Run Result:
Book, "Roman Holiday", isdestroyed!
Book, ' Java How Toprogram ', is destroyed!
Book, ' Gonewith Wind ', is destroyed!
Description: The result is eclipse in debug mode!
A detailed description of the memory recycling mechanism in Java