First, accessibility analysis (root search) algorithm
The JVM uses a accessibility analysis to determine whether an object survives. The basic idea of this algorithm is to use a series of GC Roots
objects called as the starting point, starting from these nodes downward search, the search through the path referred to as a reference chain, when an object to GC Roots
no reference chain connected, it is proved that this object is not available. objects,,, and objects that are available in the following figure,,, object1
object2
object3
object4
are object5
object6
object7
associated, but they GC Roots
are unreachable, so they will be judged to be recyclable objects.
In the Java language, objects that can be roots as GC include the following:
1. The objects referenced in the virtual machine stack (local variables in the stack frame).
2. The object referenced by the class static property in the method area.
3, the object that the constant references in the method area.
4. Objects referenced by JNI (native method) in the local method stack.
Second, spring source code
spring
An object is created by implementing the class of the interface BeanFactory
, like the following implementation structure:
Simplejndibeanfactory.java
public class Simplejndibeanfactory extends Jndilocatorsupport implements Beanfactory {
private final set<string& Gt Shareableresources = new HashSet ();
The Map created using new is the object referenced in the stack, which can be used as a GC Roots object as a
private final map<string, object> singletonobjects = new HashMap ();
Private final map<string, class<?>> resourcetypes = new HashMap ();
Public Simplejndibeanfactory () {
this.setresourceref (true);
}
public void Addshareableresource (String shareableresource) {
this.shareableResources.add (shareableresource);
} Public
void Setshareableresources (String ... shareableresources) {
this.shareableResources.addAll ( Arrays.aslist (shareableresources));
Public Object Getbean (String name) throws Beansexception {return
This.getbean (name, object.class);
}
Staticlistablebeanfactory.java
public class Staticlistablebeanfactory implements Listablebeanfactory {
private final map<string, object> Beans;
The public staticlistablebeanfactory () {
//map, created using new, is the object referenced in the stack and can be used as a GC Roots object
This.beans = new Linkedhashmap ();
Public
staticlistablebeanfactory (map<string, object> beans) {
assert.notnull (beans, "beans Map must Not being null ");
This.beans = beans;
}
public void Addbean (String name, Object Bean) {
this.beans.put (name, Bean);
}
}
As you can see from the source above, objects are stored in a map, which mapsingletonObjects
is used to hold a single instance object. map singletonObjects
and beans
(New in the constructor method) are created directly using keywords, new
are strong references, and satisfy conditions that are GC Roots
objects (objects referenced in the virtual machine stack (local variables in the stack frame)). Objects created in this way map
are GC Roots对
and are connected, so they are not recycled.
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.