Guaranteed objects created after spring startup are not reclaimed by the garbage collector _java

Source: Internet
Author: User

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

springAn 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 singletonObjectsand 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.