JVM learning (five) reference types of objects

1. Introduction

  Earlier we learned the garbage collection mechanism of the JVM. We know that garbage collection is a self-release behavior of the JVM; although we can explicitly call garbage collection through System.gc () or Runtime.getRuntime (). Gc () , the JVM can The explicit garbage collection calls are masked out, and the JVM also has its own set of garbage collection mechanisms, so is there any way we can "tell" the JVM, which objects can be used for later collection, and which objects are retained? Here we have to talk about the reference types of JAVA objects.

Second, a brief introduction to object references

  • Strong reference: No matter whether the memory is enough, it will not be recycled.
  • Soft reference: When the memory is insufficient, the object associated with the reference is recycled.
  • Weak reference: During garbage collection, regardless of whether the memory is enough, it will be recycled.
  • Virtual reference: It may be recycled by the garbage collector at any time.

There are two main purposes for providing these four reference types in Java:

    • 1. Let programmers determine the life cycle of certain objects through code ;
    • 2. The second is conducive to JVM garbage collection.

Three, object reference code examples

Strong reference

  Strong references are the most widely used and most common type of references. The common way of creating objects is to use strong references:

// 1. Strong reference example 
String str = new String ();

Features:

  • As long as an object is strongly referenced, or has a strong reference associated with it, the JVM will definitely not recycle the object
  • In case of insufficient memory , the JVM will throw OutOfMemory error
  • When you need to recycle , you can explicitly set this object to null , and the JVM will automatically recycle this object when garbage collection occurs

Soft reference

  Soft reference is used to describe some useful but not necessary objects, which is represented by java.lang.ref. SoftReference class in Java . For objects associated with soft references, the JVM will only reclaim the object when there is insufficient memory:

// 2. Soft reference example 
SoftReference <String> softStr = new SoftReference <String> ( new String ("This is a string object of soft reference type" )); 
System.out.println (softStr.get ());

  Relative to strong references, soft references solve the problem of OOM [Out Of Memory] better. You can set some useful data but not the core content through soft references. Then when the memory is insufficient, You can reclaim free space. Common applications such as soft referencing a picture object, etc.

Features:

  • SoftReference class package reference
  • Will be recycled when the JVM memory is insufficient [Do not recycle when there is a strong reference association]

Weak reference

  Weak references are also used to describe some useful but not necessary objects, which is represented by the java.lang.ref. WeakReference class in Java . For objects associated with weak references, when the JVM performs garbage collection, regardless of whether the memory is sufficient, it will be recycled:

// 3. Example of weak reference 
WeakReference <String> weekStr = new WeakReference <String> ( new String ("This is a string object of weak reference type" )); 
System.out.println (weekStr.get ()); 
// Explicitly call the JVM garbage collection 
System.gc ();
 // Output the content of the weak reference [Null will be output at this time, because the content of the weak reference object has been recycled when the garbage collection is explicitly called] 
System. out.println (weekStr.get ());

  PS: If the weak reference object has a corresponding strong reference association, then this weak reference will not be collected during garbage collection [soft references are similar]

Features:

  • WeakReference class package reference
  • JVM whether or not sufficient memory all will be in the garbage recycling [there is no recovery time associated with strong references]

Virtual reference

  Virtual references do not affect the life cycle of objects . It is represented by java.lang.ref. PhantomReference class in java . If an object is associated with a virtual reference, it is likely to be collected by the garbage collector at any time , as if there were no reference associated with it .

// 4. Examples of virtual references
 // 4.1. Create a reference queue [Virtual references must be associated with the reference queue] 
ReferenceQueue <String> queue = new ReferenceQueue <String> ();
 // 4.2. Create a virtual reference wrapped with a character The reference address of the string object 
PhantomReference <String> phantomStr = new PhantomReference <String> ( new String ("This is a string object of virtual reference type" ), queue);
 // 4.3, output the reference content [the result is found to be null 】 
System.out.println (phantomStr.get ());

Features:

  • PhantomReference class package reference
  • Can be recycled by the garbage collector at any time
  • Must be used in conjunction with the reference queue

Reference Queue (ReferenceQueue)

effect:

  • Used to monitor whether the object pointed to by Reference has been garbage collected .

scenes to be used:

  • When a lot of references are used to wrap the instance object, although the object pointed to by the reference may be recycled, the Reference itself is also an object, so it also needs to be recycled, and then you need to use the ReferenceQueue.

Differences in recycling mode:

  • When the get () of SoftReference or WeakReference is added to ReferenceQueue or get () and returns null, it only indicates that the object indicated by it has entered the garbage collection process. At this time, the object may not have been garbage collected.
  • When PhantomReference is added to the ReferenceQueue, it indicates that the object needs and has been recycled [ PS: So step 4.3 of the virtual reference example, the return will be null when calling the get method] .

 

 

Guess you like

Origin www.cnblogs.com/riches/p/12707522.html