The garbage collection

  • Garbage collection only reclaims objects in heap memory, not any physical resources (database connections, network IO, etc.)
  • The program does not have precise active control over garbage collection
  • Before collecting any object, the system will always call the Finalize () method of the object to be collected, which may resurrect the object

To actually declare an object dead, there must be at least two marking processes:

If an object is found to have no reference chain connected to GC Roots after the root search, it will be marked for the first time and filtered based on whether it is necessary to execute the Finalize () method

If Finalize has not been called, the system will call this method

Finalize () method is the last chance for objects to escape death (because the finalize () method of an object can only be called automatically by the system once at most). Later, GC will make a second small tag for objects in the F-queue. If you want to save yourself successfully in Finalize () method, Just make the object rereference any object on the chain in the Finalize () method to establish the association

If the object is not yet associated with any reference on the chain, it will be recycled

Garbage collector objects can be in three states in memory: reachable — with variable references; Recoverable state — no variable references; Unreachable state — No variable references, and the system cannot make objects reachable after calling Finalize (). Only objects in this state are reclaimed.

Mandatory garbage collection:

System.gc(); Or the Runtime. GetRuntime (). The gc ();Copy the code

The above methods, which the virtual machine will do its best to implement, are not 100% reliable. Tip: java-verbose :gc class name This command displays information after each garbage collection

Finalize () method:

  • Do not actively call this method on an object;
  • There is some uncertainty when finalize method is called.
  • When the system executes the Finalize () method of recoverable objects, objects or other objects in the system may become reachable state again.
  • When an exception occurs when FINALIZE is executed by JVM, garbage collection mechanism will not report the exception and the execution will continue.

Force to call Finalize () :

Runtime.getRuntime().runFinalization();
System.runFinalization();
Copy the code

As with mandatory GC, virtual machines do their best to implement and are not 100% reliable. If you want to ensure resource reclamation, try Finally is recommended

In general, the following combination is called to enforce garbage collection:

System.gc();
System.runFinalization();
Copy the code

Soft, weak, and virtual references to objects

  • Strong reference A common reference in which an object is reachable if it is strongly referenced by at least one variable. The system cannot reclaim objects with strong references.
  • Soft references When an object has only soft references, it may be garbage collected when garbage collection is running, depending on whether memory is tight.
  • Weak References When an object has only weak references, when the garbage collection mechanism is running, it is collected regardless of whether the system memory is sufficient.
  • Virtual references are similar to no references and are used to track the status of an object being garbage collected. Objects that have only virtual references can be reclaimed at any time. Virtual references cannot be used alone and must be used in conjunction with ReferenceQueue.

Each of these references contains a get() method to get the object they refer to.

1. The program can check whether the reference queue associated with a virtual reference contains the virtual reference, so as to know whether the object referred to by the virtual reference is about to be reclaimed;

2. Tip: If you want to test a reference, create a String object with String STR = new String(” test String “). The latter is managed directly by the system using the constant pool (also strong references), but is not collected by the garbage collection mechanism.

3. If you want to use a special reference class feature, do not keep a strong reference to the object. *