欢迎大家来到IT世界,在知识的湖畔探索吧!
//创建一个用来测试的类
public class ReferenceDemo {
private String name;
private static final int SIZE = 1000;
private Long[] longArray = new Long[SIZE];
public ReferenceDemo(String name){
this.name = name;
}
public ReferenceDemo(String name, int size) {
this.name = name;
longArray = new Long[size];
}
}
欢迎大家来到IT世界,在知识的湖畔探索吧!
欢迎大家来到IT世界,在知识的湖畔探索吧!class ReferenceTest{
public static void main(String[] args) throws InterruptedException {
System.out.println("start test strongReference");
ReferenceDemo reference = new ReferenceDemo("strongReference");
//通过给strongReference赋值,创建一个强引用
Object strongReference = reference;
//证明reference和strongReference是同一个对象
System.out.println(reference == strongReference);
//给reference赋值为null,也就是说reference变量不指向对象
reference = null;
//当把strongReference也指向null时,此时new ReferenceDemo("strongReference");
//没有任何引用,因此就会被垃圾回收器给回收
//strongReference = null;
//手动触发GC
System.gc();
Thread.sleep(500);
//由于对象还存在引用,所以在触发GC后对象未被回收
System.out.println(strongReference);
System.out.println("end test strongReference");
}
}
输出结果:
start test strongReference
true
ReferenceDemo@8efb846
end test strongReference
欢迎大家来到IT世界,在知识的湖畔探索吧!class ReferenceTest{
public static void main(String[] args) throws InterruptedException {
SoftReference<ReferenceDemo> softReference = new SoftReference<>(new ReferenceDemo("softReference"));
System.out.println(softReference.get());
System.gc();
Thread.sleep(500);
//即使手动触发后,对象仍然不会被回收
System.out.println(softReference.get());
int i = 0;
while (true) {
try {
++i;
new ReferenceDemo("oom ", 400_000_000);
} catch (Throwable e) {
System.out.println("OOM after " + i + " times");
e.printStackTrace();
break;
}
}
//当发生内存溢出时,对象被回收
System.out.println(softReference.get());
}
}
测试结果:
com.jinlei.juc.juc.T_001.lock.ReferenceDemo@8efb846
com.jinlei.juc.juc.T_001.lock.ReferenceDemo@8efb846
OOM after 1 times
java.lang.OutOfMemoryError: Java heap space
at com.jinlei.juc.juc.T_001.lock.ReferenceDemo.<init>(ReferenceDemo.java:16)
at com.jinlei.juc.juc.T_001.lock.ReferenceTest.main(ReferenceDemo.java:49)
null
class ReferenceTest {
public static void main(String[] args) throws InterruptedException {
System.out.println("start test weakReference");
//创建一个弱引用对象
WeakReference<ReferenceDemo> weakReference = new WeakReference<>(new ReferenceDemo("weakReference"));
System.out.println(weakReference.get());
//ReferenceDemo strongReference = weakReference.get(); 7
//手动触发GC
System.gc();
Thread.sleep(500);
System.out.println(weakReference.get());
System.out.println("end test weakReference");
}
}
测试结果:
start test weakReference
com.jinlei.juc.juc.T_001.lock.ReferenceDemo@8efb846
null
end test weakReference
当有强引用,第7行代码打开时,对象是不会被回收的:
start test weakReference
com.jinlei.juc.juc.T_001.lock.ReferenceDemo@8efb846
com.jinlei.juc.juc.T_001.lock.ReferenceDemo@8efb846
end test weakReference
//创建一个引用队列
ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
//创建一个虚引用
PhantomReference<ReferenceDemo> phantomReference = new PhantomReference<>(new ReferenceDemo("phantomReference"),referenceQueue);
//虚引用的get方法永远返回为null
System.out.println(phantomReference.get());
System.out.println(referenceQueue.poll());
System.gc();
Thread.sleep(500);
System.out.println(referenceQueue.poll());
测试结果如下:
null
null
java.lang.ref.PhantomReference@8efb846
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/36861.html