destruction of Java objects
Each object has a lifecycle, and when the object's lifecycle ends, the memory address assigned to the object is reclaimed.
what objects are considered garbage by the Java Virtual machine:
(1) object reference exceeds its scope, this object is treated as garbage
{
Example e = new Example ();
}
A range outside the curly braces is considered to be beyond the scope of E.
(2) Assigning the object to null
{
Example e = new Example ();
e = null;
}
Note: The garbage collector can only recycle objects created by the new operator. Java provides a finalize () method if some objects do not get an area in memory from the new operator, which may not be recognized by the garbage collection mechanism. This method is a method of the object class, which is declared as protected, and the user can define this method in its own class. If the user defines the Finalize () method in the class, the method is called first at garbage collection, and the memory occupied by the object is actually reclaimed when the next garbage collection action occurs.
Note: One thing to be clear about is that garbage collection and finalize () methods are not guaranteed to occur, such as when Java Virtual machine memory consumption is exhausted, it does not perform garbage collection. How about that. Java provides a System.GC () method to force the garbage collector to start.