I haven't read a book about Java for a long time. Recently, I read James Gosling's <<Java Programming Language>> and took some reading notes. This part is about garbage recycling.
1. Garbage recycling
Objects are created using new, but there is no corresponding delete operation to recycle the memory occupied by the object. When we complete the use of an object, we just need to stop the reference to that object:
-> Change the reference to point to another object
-> Point the reference to null
->Return from the method so that the local variables of the method no longer exist
Key points:
-> When we cannot reach an object from any executable code, the space it takes can be recycled.
->Garbage recycling means we never have to worry about dangling references. A false reference refers to a reference to the memory space that has been deleted. This problem exists in systems where programmers can directly control when objects are deleted.
-> Garbage collector model: reference counter method (cannot solve circular reference), mark-and-sweep.
2. Summary
Finalize method
-> After the garbage collector determines that the object is unreachable and that the object's space will be recycled, the garbage collector will call this method.
-> This method can clear all non-memory resources used by the object, and can only be called once for each object. Even if the execution of this method makes the object reachable again and then immediately becomes unreachable again, the method can only be called once.
The ->finalize method can be called in any specific time period, and it may never be called (the java virtual machine ends).
Override finalize method
->When an object becomes garbage, the other objects it refers to are also likely to become garbage. These garbage may have been terminated before calling the finalize method we wrote, so they may be in an unpredictable state.
-> Overwrite the finalize method is to add the super.finalize method. It is best to add it in the finally sentence. Ensure that some of the content declared in its superclass can also be terminated.
III. Related classes and methods for interacting with garbage collectors
Class: Runtime.getRuntime(), System
Methods: gc(), runFinalization(), freeMemory(), totalMemory(), maxMemory()
The System class supports static gc() and runFinalization() methods, which will call the corresponding methods on the current Runtime object.
IV. Accessibility state and reference objects
An object can only be garbage collected when no reference specifies it, but sometimes we may want to collect the object as garbage when there is still a selected reference pointing to the object.
The only purpose of a reference object is to maintain a reference to another object called a reference. Usually we maintain references to objects through fields or local variables, but now we can maintain direct references to reference objects, which wraps the object we actually need. The garbage collector may determine whether the residual reference to an object is referenced to the object via the reference object face, so it can decide whether to recycle the object. The strength of the reference object will determine the behavior of the garbage collector, and ordinary references are the most powerful references.
Reference class
-> Package:java.lang.ref
->Typical methods: get(), clear(), enqueue(), isEnqueued()
Citation and Accessibility Strength
-> Objects are strongly reachable: ordinary reference
-> Object is softly reachable:SoftReference
-> Objects are weakly reachable: WeakReference
-> Objects are virtually accessible (phantom reachable): PhantomReference
-> Objects are unreachable: No reference links Once the object becomes weakly accessible (or column weak), it can be terminated. If the object is unreachable after the termination, it can be recycled.
The object reachability phase triggers the garbage collector to perform appropriate behavior on the relevant reference object types:
->Soft reachable objects may be allowed to be recycled by the garbage collector. What we can be sure of is that all SoftReferences for SoftReference objects will be cleared before throwing an outofMemoryError error.
->Well accessible objects will be recycled by the garbage collector.
->The virtual reachable object is not truly reachable, because its referential object cannot be accessed through PhantomReference, and its get method always returns null. But the existence of virtual references can prevent objects from being recycled before explicitly clearing virtual references. Virtual references allow us to handle objects whose finalize methods have been called, making it safe to think that they are "dead".