Java 1.2 added four kinds of references: strong references, soft references, weak references, phantom references

Strong references

References on the traditional understanding, Obeject obj = new ArrayList();similar reference, as long as there is a strong relationship references, this object will not be gc ,

注意:不会被gc是说持有引用的对象 与 被引用的对象直接的可达性是绝对的,不会被虚拟机干涉;如果持有引用的对象与GC root不可达的话,依然有可能会被GC

Soft references

Soft reference object is associated, will take place in the system before memory overflow exception will be included in the list of objects that can be recycled, recovered memory after less than, an exception will be thrown out of memory, not a last resort, jvm will not move him

Weak references

If the object is used before can only survive until the next garbage collection, weak references gc judge does not interfere, as long as the object weak references willing death is collected, this weak references would not have had any influence gc

Many usage scenarios, such as inner class can maintain a weak reference to the outer class, outside of class to access the data without affecting the survival time outside of class, very effective in preventing memory leaks

False quote

Strictly speaking, can not be called a reference, the reference data can not access the object, can not find him, only role is: When the weak reference to the object of death, notify holders may obtain a system reference.

significance

In practice scenario: Focus on weak references to
four kinds of references is to allow programmers to code in a way to control the behavior of objects in part when gc

Published 28 original articles · won praise 23 · views 10000 +

Guess you like

Origin blog.csdn.net/TowerOs/article/details/104361570