1. Accessibility analysis (root search) algorithm
JVM determines whether the object survives through accessibility analysis. The basic idea of this algorithm is to use a series of objects called GC Roots as the starting point, and search downward from these nodes. The paths searched through are called reference chains. When an object is connected to GC Roots without any reference chains, it is proved that this object is unavailable. In the figure below, objects object1 , object2 , object3 , and object4 are available objects. Although object5 , object6 , and object7 are related to each other, they are unreachable to GC Roots , so they will be judged to be recyclable objects.
In Java language, the following objects that can be used as GC Roots include:
1. Object referenced in the virtual machine stack (local variables in the stack frame).
2. Object referenced by class static attributes in the method area.
3. Object referenced by constants in the method area.
4. Object referenced by JNI (Native method) in the local method stack.
2. Spring source code
spring creates objects through the class that implements interface BeanFactory , with the following implementation structure:
SimpleJndiBeanFactory.java
public class SimpleJndiBeanFactory extends JndiLocatorSupport implements BeanFactory { private final Set<String> shareableResources = new HashSet(); //The map created using new is an object referenced in the stack and can be used as a GC Roots object 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; public StaticListableBeanFactory() { //The map created using new is an object referenced in the stack, which 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 be null"); this.beans = beans; } public void addBean(String name, Object bean) { this.beans.put(name, bean); }} From the source code above, we can see that the object is stored in a Map, where mapsingletonObjects are used to store singleton objects. map singletonObjects and beans (new in the constructor) are both created directly using the keyword new , which is a strong reference and meets the conditions as GC Roots objects (objects referenced in the virtual machine stack (local variables in the stack frame). The objects created in this way are connected to GC Roots对in map , so they will not be recycled.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.