Strong and weak references in java

A strong citation

String str="this is strong reference";

The above is a simple example of a strong reference: str is referenced by "this is strong reference". If the reference does not disappear (is released), even if it does not work, it will always occupy the memory and will not be reclaimed by the garbage collection mechanism. .

2. Weak reference
The typical weak reference in java is the ThreadLocal object: ThreadLocal inherits WeakReference. If it does not work, it will still be recycled by the garbage collection mechanism. ThreadLocal also solves the problem of memory leaks through this.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324837557&siteId=291194637