This is the 21st day of my participation in the August Text Challenge.More challenges in August

Reference counting method

Java virtual machines do not rely on reference counting algorithms to determine whether objects are alive or not.

Adds a reference counter to the object, incrementing its value by one each time it is referenced somewhere; When a reference is invalid, the counter value is reduced by one; An object whose counter is zero at any point in time cannot be used again.

advantages

  • The principle is simple and the judgment efficiency is high

disadvantages

  • Cannot be used in complex environments, such as object cross-references

Accessibility analysis algorithm

The Java virtual machine uses this algorithm to determine whether an object is alive

The basic idea of this algorithm is to use a series of root objects called “GCRoots” as the starting node set. From these nodes, search down according to the Reference relationship. The path that the search goes through is called “Reference Chain”. Or in graph theory terms, when the object is unreachable from GC Roots, then the object cannot be used again.

Objects as GC Roots in Java:

  • Objects referenced in the virtual machine stack (the local variable table in the stack frame), such as parameters, local variables, temporary variables, etc. used in the method stack called by each thread.

  • An object referenced by a class static attribute in a method area, such as a Java class reference type static variable.

  • Objects that are constant references in the method area, such as references in the String Constant pool (String Table).

  • Objects referenced by JNI (commonly referred to as Native methods) in the Native method stack.

  • Internal references to the Java virtual machine, such as Class objects corresponding to basic data types, resident exception objects (NullPointExcepiton, OutOfMemoryError), and system Class loaders.

  • All objects held by the synchronized keyword.

  • Jmxbeans that reflect Java virtual machine internals, callbacks registered in JVMTI, local code caches, and so on.

  • Other objects are temporarily added to form GC Roots