http://wiseideal.iteye.com/blog/1469295
WeakReference与SoftReference都可以用来保存对象的实例引用,这两个类与垃圾回收有关。
WeakReference是弱引用,其中保存的对象实例可以被GC回收掉。这个类通常用于在某处保存对象引用,而又不干扰该对象被GC回收,通常用于Debug、内存监视工具等程序中。因为这类程序一般要求即要观察到对象,又不能影响该对象正常的GC过程。
最近在JDK的Proxy类的实现代码中也发现了Weakrefrence的应用,Proxy会把动态生成的Class实例暂存于一个由Weakrefrence构成的Map中作为Cache。
SoftReference是强引用,它保存的对象实例,除非JVM即将OutOfMemory,否则不会被GC回收。这个特性使得它特别适合设计对象Cache。对于Cache,我们希望被缓存的对象最好始终常驻内存,但是如果JVM内存吃紧,为了不发生OutOfMemoryError导致系统崩溃,必要的时候也允许JVM回收Cache的内存,待后续合适的时机再把数据重新Load到Cache中。这样可以系统设计得更具弹性。
WeakReference的一个测试程序:
- import java.lang.ref.WeakReference;
- public class WeakReferenceTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- A a = new A();
- a.str = “Hello, reference”;
- WeakReference<A> weak = new WeakReference<A>(a);
- a = null;
- int i = 0;
- while (weak.get() != null) {
- System.out.println(String.format(“Get str from object of WeakReference: %s, count: %d”, weak.get().str, ++i));
- if (i % 10 == 0) {
- System.gc();
- System.out.println(“System.gc() was invoked!”);
- }
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- }
- }
- System.out.println(“object a was cleared by JVM!”);
- }
- }
- import java.lang.ref.WeakReference;
- public class WeakReferenceTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- A a = new A();
- a.str = “Hello, reference”;
- WeakReference<A> weak = new WeakReference<A>(a);
- a = null;
- int i = 0;
- while (weak.get() != null) {
- System.out.println(String.format(“Get str from object of WeakReference: %s, count: %d”, weak.get().str, ++i));
- if (i % 10 == 0) {
- System.gc();
- System.out.println(“System.gc() was invoked!”);
- }
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- }
- }
- System.out.println(“object a was cleared by JVM!”);
- }
- }
程序运行结果:
- Get str from object of WeakReference: Hello, reference, count: 1
- Get str from object of WeakReference: Hello, reference, count: 2
- Get str from object of WeakReference: Hello, reference, count: 3
- Get str from object of WeakReference: Hello, reference, count: 4
- Get str from object of WeakReference: Hello, reference, count: 5
- Get str from object of WeakReference: Hello, reference, count: 6
- Get str from object of WeakReference: Hello, reference, count: 7
- Get str from object of WeakReference: Hello, reference, count: 8
- Get str from object of WeakReference: Hello, reference, count: 9
- Get str from object of WeakReference: Hello, reference, count: 10
- System.gc() was invoked!
- object a was cleared by JVM!
- Get str from object of WeakReference: Hello, reference, count: 1
- Get str from object of WeakReference: Hello, reference, count: 2
- Get str from object of WeakReference: Hello, reference, count: 3
- Get str from object of WeakReference: Hello, reference, count: 4
- Get str from object of WeakReference: Hello, reference, count: 5
- Get str from object of WeakReference: Hello, reference, count: 6
- Get str from object of WeakReference: Hello, reference, count: 7
- Get str from object of WeakReference: Hello, reference, count: 8
- Get str from object of WeakReference: Hello, reference, count: 9
- Get str from object of WeakReference: Hello, reference, count: 10
- System.gc() was invoked!
- object a was cleared by JVM!
SoftReference的一个测试程序:
- import java.lang.ref.SoftReference;
- public class SoftReferenceTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- A a = new A();
- a.str = “Hello, reference”;
- SoftReference<A> sr = new SoftReference<A>(a);
- a = null;
- int i = 0;
- while (sr.get() != null) {
- System.out.println(String.format(“Get str from object of SoftReference: %s, count: %d”, sr.get().str, ++i));
- if (i % 10 == 0) {
- System.gc();
- System.out.println(“System.gc() was invoked!”);
- }
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- }
- }
- System.out.println(“object a was cleared by JVM!”);
- }
- }
- import java.lang.ref.SoftReference;
- public class SoftReferenceTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- A a = new A();
- a.str = “Hello, reference”;
- SoftReference<A> sr = new SoftReference<A>(a);
- a = null;
- int i = 0;
- while (sr.get() != null) {
- System.out.println(String.format(“Get str from object of SoftReference: %s, count: %d”, sr.get().str, ++i));
- if (i % 10 == 0) {
- System.gc();
- System.out.println(“System.gc() was invoked!”);
- }
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- }
- }
- System.out.println(“object a was cleared by JVM!”);
- }
- }
程序运行结果:
- Get str from object of SoftReference: Hello, reference, count: 1
- Get str from object of SoftReference: Hello, reference, count: 2
- Get str from object of SoftReference: Hello, reference, count: 3
- Get str from object of SoftReference: Hello, reference, count: 4
- Get str from object of SoftReference: Hello, reference, count: 5
- Get str from object of SoftReference: Hello, reference, count: 6
- Get str from object of SoftReference: Hello, reference, count: 7
- Get str from object of SoftReference: Hello, reference, count: 8
- Get str from object of SoftReference: Hello, reference, count: 9
- Get str from object of SoftReference: Hello, reference, count: 10
- System.gc() was invoked!
- Get str from object of SoftReference: Hello, reference, count: 11
- Get str from object of SoftReference: Hello, reference, count: 12
- Get str from object of SoftReference: Hello, reference, count: 13
- Get str from object of SoftReference: Hello, reference, count: 14
- Get str from object of SoftReference: Hello, reference, count: 15
- Get str from object of SoftReference: Hello, reference, count: 16
- Get str from object of SoftReference: Hello, reference, count: 17
- Get str from object of SoftReference: Hello, reference, count: 18
- Get str from object of SoftReference: Hello, reference, count: 19
- Get str from object of SoftReference: Hello, reference, count: 20
- System.gc() was invoked!
- Get str from object of SoftReference: Hello, reference, count: 21
- Get str from object of SoftReference: Hello, reference, count: 22
- Get str from object of SoftReference: Hello, reference, count: 23
- Get str from object of SoftReference: Hello, reference, count: 24
- Get str from object of SoftReference: Hello, reference, count: 25
- Get str from object of SoftReference: Hello, reference, count: 26
- Get str from object of SoftReference: Hello, reference, count: 27
- Get str from object of SoftReference: Hello, reference, count: 28
SoftReference比WeakReference生命力更强,当JVM的内存不吃紧时,即使引用的对象被置为空了,Soft还可以保留对该对象的引用,此时的JVM内存池实际上还保有原来对象,只有当内存吃紧的情况下JVM才会清除Soft的引用对象,并且会在未来重新加载该引用的对象。
而WeakReference则当清理内存池时会自动清理掉引用的对象。
___________________________________________________________________________________________________________________________________
http://blog.csdn.net/historyasamirror/article/details/6076505
SoftReference的语义就是当内存不够用的时候,GC会回收SoftReference所引用的对象。所以,在memory sensitive的程序中将某些大型数据设置成SoftReference再合适不过了。
创建一个SoftReference:
- Object obj = new Object();
- SoftReference softRef = new SoftReference(obj);
- obj = null;
最后那句“obj = null”很重要。如果不将obj设置成null,那么new出来的Object就会有一个”strong reference”,如果这样,softRef就不能发挥作用了。
使用SoftReferene:
- Object obj2;
- obj2 = sr.get();
- if (obj2 == null) // GC freed this
- sr = new SoftReference(obj2 = new Object());
因为是SoftReference,所以有可能已经被GC回收了,所以需要判断sr.get()的返回值是否为null。如果是的话,就再重新new一个Object。这里的最后一行代码也很有讲究,比如,也许会这么写:
- Object obj2;
- obj2 = sr.get();
- if (obj2 == null) {
- sr = new SoftReference(new Object());
- obj2 = sr.get();
- }
这样的问题就在于GC可能发生在”sr = new SoftReference(new Object());”和”obj2 = sr.get()”之间,那么obj2仍然有可能为null。
Reference:
http://www2.sys-con.com/itsg/virtualcd/java/archives/0507/shields/index.html
今天的文章SoftReference 与 WeakReference分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/12426.html