ThreadLocal prelude: I understand java four kinds of reference types

Foreword

To understand ThreadLocal, grasp the concept of reference is very necessary.

A reference to the object

java, we pass a reference point to objects in memory.

//创建一个引用,引用可以独立存在,并不一定需要与一个对象关联
User user;
user =  new User();
复制代码

user is referenced by '=' pointing to objects in memory.

Four kinds of reference

After the JDK1.2, depending on the intensity of a reference object, the reference is divided into four types.

Strong references:

Usually the most used, the most common reference.

User user;
user =  new User();//强引用
复制代码
  • user is cited strong
  • Heap User object instance, is "applied";

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

user = null;断开了引用,User对象不可达,会被回收。
复制代码
Soft references:

We can be SoftReferencea soft references to define

SoftReference<User> softRef=new SoftReference<User>(new User());//软引用
复制代码
  • If an object has only soft references , when sufficient memory is not recovered when he GC. If the content of the lack of space, the recovery will be soft references to memory objects.
  • As long as no other recovery, may also be used

: Under explain the object only soft reference case, there are two objects referenced

User user  = new User();//强引用
SoftReference<User> softRef=new SoftReference<User>(user);//软引用
复制代码

Object reference only soft case

SoftReference<User> softRef=new SoftReference<User>(new User());//软引用
复制代码

or

User user  = new User();//强引用
SoftReference<User> softRef=new SoftReference<User>(user);//软引用
user = null;//断开了强引用。 此时只有软引用。
复制代码

Soft references can be used in conjunction with a reference queue (ReferenceQueue), when you create a soft references, related ReferenceQueue. If the soft reference object references are recovered, Java virtual machine to be added to the soft references cited associated queue.

Soft references can be used as memory-sensitive caches.

Weak references:

By WeakReferencea defined weak references

WeakReference<> weak = new WeakReference<>(new User());//定义一个软引用指向堆内User对象
复制代码
  • If the object has only weak references , GC time, regardless of whether or not enough memory. Will be recovered, the weak points of the reference object.

Weak references may also be used in combination with the reference queue (the ReferenceQueue), when creating a weak reference associated ReferenceQueue. If the weak object references cited are recovered, Java virtual machine to the weak reference is added to the reference associated with the queue.

ThreadLocal used to weak.

False quote:

Virtual reference is weakest reference to weak, you can not access to the object with weak references.

By PhantomReferencea defined virtual reference.

False quote, primarily used to track objects recovered garbage collector activity.

Must be used with reference to an imaginary reference queue (the ReferenceQueue), when the garbage collector to prepare a target recovery, if it is found there is a virtual reference, will be recovered before the memory, this reference is added to the virtual reference associated queue. We can quote recycling activities are in the queue to determine the object.

Why do we need a different reference types

We see different references of different strength, the life cycle of objects, rich garbage collection policy is conducive to better manage objects in memory.


to sum up:

  • References associated with the object life cycle.
  • Objects may be of various types, a plurality of reference points, but as long as there is a strong reference. The garbage collector is not recovered.

When understanding the key: an object can be referenced by multiple types at the same time point, the highest decision intensity of his life cycle .

Note: Memory here should refer to " heap " and the distinction between reference types and primitive 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());
复制代码

Guess you like

Origin juejin.im/post/5dc3700ef265da4d270b4864