This article analyzes the destruction method of objects in Java in more detail. Share it with everyone for your reference. The specific analysis is as follows:
Basic data type variables and object name reference variables in Java are all local variables if they are defined in a method. But the object itself does not necessarily have a local life cycle. If there are other reference variables to the object outside the function, the life cycle of the object extends to the block where the other reference variables are located.
If the value or return value is passed from the parameter reference of the called function to the object type variable of the main calling function, the object still exists (but the life cycle of the reference variable of the object of the called function ends, so the reference variable is a local variable ), at this time the object breaks through the local lifetime of the local variable.
Java object destruction
The garbage collector in Java automatically scans the dynamic memory of Java objects regularly and marks all reference objects. After the object runs (no reference variables are associated with the object), it clears its marks and all unmarked objects. The objects are recycled as garbage and the memory space occupied by the garbage objects is released.
After the object runs or when its life cycle ends, it will become a garbage object, but it does not mean that it will be recycled immediately. They will only be recycled when the garbage collector is idle or there is insufficient memory.
Every object in Java has a finalize() method:
protected void finalize()throws Throwable{}
The garbage collector automatically calls the object's finalize() method to release system resources when recycling an object.
finalize() function prototype:
protected void | finalize () Called by the garbage collector on an object when garbagecollection determines that there are no more references to the object. |