preface

To understand ThreadLocal, it is essential to grasp the concept of references.

References and Objects

In Java we point to an in-memory object through a reference.

// Create a reference that can stand on its own and does not need to be associated with an object
User user;
user =  new User();
Copy the code

User is a reference that points to an object in memory via ‘=’.

Four types of references

After JDK1.2, references are divided into four types based on the strength of the reference to an object.

Strong references:

The most commonly used and most commonly quoted.

User user;
user =  new User();/ / strong reference
Copy the code
  • User is a strong reference
  • The User object instance in the heap is “applied”;

As long as user points to the User object. The User object will not be reclaimed.

user = null; If the reference is disconnected, the User object is unreachable and will be reclaimed.Copy the code
Soft references:

You can define a SoftReference with SoftReference

SoftReference<User> softRef=new SoftReference<User>(new User());/ / soft references
Copy the code
  • If an object has only soft references and sufficient memory, GC does not reclaim it. If there is insufficient content space, the memory of the object pointed to by the soft reference is reclaimed.
  • As long as it’s not recycled, it can still be used

To clarify: only soft reference objects have two references

User user  = new User();/ / strong reference
SoftReference<User> softRef=new SoftReference<User>(user);/ / soft references
Copy the code

Object has only soft reference cases

SoftReference<User> softRef=new SoftReference<User>(new User());/ / soft references
Copy the code

or

User user  = new User();/ / strong reference
SoftReference<User> softRef=new SoftReference<User>(user);/ / soft references
user = null;// Break strong references. There are only soft references at this point.
Copy the code

A soft reference can be used in conjunction with a ReferenceQueue (ReferenceQueue), which is associated when a soft reference is created. If the object referenced by a soft reference is reclaimed, the Java virtual machine adds the soft reference to the reference queue associated with it.

Soft references can be used as memory sensitive caches.

Weak references:

A WeakReference can be defined using a WeakReference

WeakReference<> weak = new WeakReference<>(new User());// Define a soft reference to the User object in the heap
Copy the code
  • If the object has only weak references, GC regardless of whether there is enough memory. All the objects that weak references point to are recycled.

Weak references can also be used in conjunction with a ReferenceQueue (ReferenceQueue), which is associated when a weak reference is created. If an object referenced by a weak reference is reclaimed, the Java virtual machine adds the weak reference to the reference queue associated with it.

Weak references are used in ThreadLocal.

Phantom reference:

A virtual reference is the weakest reference, so weak that an object cannot be accessed with a weak reference.

A virtual reference can be defined using PhantomReference.

Virtual references are used to track the activity of objects being collected by the garbage collector.

A virtual reference must be used in conjunction with the ReferenceQueue. When the garbage collector attempts to reclaim an object and finds that it has a virtual reference, it adds the virtual reference to the ReferenceQueue associated with it before reclaiming the object’s memory. We can determine the collection activity of an object based on whether the reference is in the queue or not.

Why do you need different reference types

We see differences in reference strength, different object life cycles, and rich garbage collection policies for better managing object memory.


Conclusion:

  • References are related to the life cycle of the object.
  • Objects can be referred to by multiple references of multiple types, but only by one strong reference. Garbage collector does not collect.

Key to understand: an object can be pointed to by multiple types of references at the same time, and the intensity of the object determines its life cycle.

Note: Memory should be referred to as “heap memory” and distinguish between reference types and base types

//String user ="user"; //
String user = new String("user");
SoftReference<String> softRef=new SoftReference<String>(user);
WeakReference<String> weak = new WeakReference<String>(user);
user=null;
System.out.println(softRef.get());
System.gc();
System.out.println(weak.get());
Copy the code