Home  >  Article  >  Java  >  Detailed explanation of several common functions of Reference in Java

Detailed explanation of several common functions of Reference in Java

怪我咯
怪我咯Original
2017-07-02 10:34:051907browse

This article mainly introduces you to the relevant information about the multiple functions of Reference in Java. The article introduces it in detail through sample code, which has certain reference and learning value for everyone. Friends who need it will follow the editor. Study and study.

Preface

Several Reference functions in Java are also questions that are often asked during interviews. I used to remember them once and forget them again. Now that I have time, I might as well write a demo to test it out. Let’s take a look at the detailed introduction:

The specific code is as follows:

JVM Parameters: -Xmx10m -Xms5m -XX:+PrintGC

##SoftReference:


weakReference:


StrongReference:

Since strong is the JVM default, I won’t do it here, it will not be recycled at all until OOM

PhantomReference:

Virtual

Reference will not change the memory The recycling mechanism is only put into the ReferenceQueue during recycling to notify the user to do some additional operations, such as printing logs, etc.

The following code, in essence, byte[] is still a strong reference.

Output:


##To summarize:

    StrongReference: JVM default, unless there is no
  • object

    reference during GC, even OOM will not recycle

  • WeakReference: Once it is not referenced, it will be recycled during GC
  • SoftReference: Same as strong, unless there is no object reference during GC, otherwise...before OOM will be recycled, so SoftReference can generally be used for
  • caching

  • sun.nio.ch.Util has private
  • static

    ThreadLocal< ;SoftReference> localSelector

    ##= new ThreadLocal>();
  • is used for temporary Selector .
  • PhantomReference: The ReferenceQueue passed through the
  • constructor
  • is used as a notification to perform additional operations when the object is recycled.

The above is the detailed content of Detailed explanation of several common functions of Reference in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn