WeakReference 与 SoftReference 区别
装载自:http://flyneil.iteye.com/blog/1345177
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!");
- }
- }
运行结果为:
- 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!");
- }
- }
运行结果为:
- 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去保存以下图片对象缓存,UI间传递的对象引用等。
WeakReference 与 SoftReference 区别的更多相关文章
- Java 中 WeakReference 与 SoftReference 的区别?
虽然 WeakReference 与 SoftReference 都有利于提高 GC 和 内存的效率, 但是 WeakReference ,一旦失去最后一个强引用,就会被 GC 回收,而软引用 虽然不 ...
- Java中的 WeakReference 和 SoftReference
我们知道Java语言中没有指针,取而代之的是引用reference.Java中的引用又可以分为四种:强引用,弱引用(WeakReference),软引用(SoftReference),虚引用(Phan ...
- WeakReference与SoftReference
WeakReference与SoftReference都可以用来保存对象的实例引用,这两个类与垃圾回收有关. WeakReference是弱引用,其中保存的对象实例可以被GC回收掉.这个类通常用于在某 ...
- 【转】Java之WeakReference与SoftReference使用讲解
Java 2 平台引入了 java.lang.ref 包,其中包括的类可以让您引用对象,而不将它们留在内存中.这些类还提供了与垃圾收集器(garbage collector)之间有限的交互. 1.先“ ...
- java基础回顾(六)——WeakReference、SoftReference
在Java里, 当一个对象o被创建时, 它被放在Heap里. 当GC运行的时候, 如果发现没有任何引用指向o, o就会被回收以腾出内存空间. 或者换句话说, 一个对象被回收, 必须满足两个条件: 1) ...
- Java的引用StrongReference、 SoftReference、 WeakReference 、PhantomReference
1. Strong Reference StrongReference 是 Java 的默认引用实现, 它会尽可能长时间的存活于 JVM 内, 当没有任何对象指向它时 GC 执行后将会被回收 @Te ...
- Android内存管理(3)缓存不要用SoftReference, 用android.util.LruCache
A reference that is cleared when its referent is not strongly reachable and there is memory pressure ...
- 转:最近5年133个Java面试问题列表
最近5年133个Java面试问题列表 Java 面试随着时间的改变而改变.在过去的日子里,当你知道 String 和 StringBuilder 的区别就能让你直接进入第二轮面试,但是现在问题变得越来 ...
- [翻译]Understanding Weak References(理解弱引用)
原文 Understanding Weak References Posted by enicholas on May 4, 2006 at 5:06 PM PDT 译文 我面试的这几个人怎么这么渣啊 ...
随机推荐
- 关于axios如何在请求头添加参数
vm.$http.post(apiUrl.refundOrder, data,{ headers:{ 'lz-shopid':vm.orderRecords.shopId } }).then(res ...
- ubuntu默认root密码问题,第一次使用ubuntu需要设置root密码
http://www.voidcn.com/article/p-yvnoogkc-ng.html 新接触ubuntu(baseondebian)的人,大多会因为安装中没有提示root密码而不太清楚为什 ...
- Oracle GoldenGate OGG管理员手册
第一章 系统实现简述 前言 编写本手册的目的是为系统管理员以及相关操作人员提供 Oracle Goldengat 软 件的日常维护和使用的技术参考: 3 ORACLE 第二章 OGG 日常维护操作 ...
- Fabric 查看zookeeper和kafka
进入kafka容器: sudo docker exec -it kafka bash cd opt/kafka 查看Kafka自动创建的topic bin/kafka-tipiccs.sh --lis ...
- JAVA计算字符串UTF-8字节数
String str = "C++/C#/JAVA软件开发"; try { System.out.println(str.getBytes("UTF-8").l ...
- @ControllerAdvice和@ExceptionHandler
1. 使用 @ControllerAdvice和@ExceptionHandler处理全局异常 1. 新建异常信息实体类 非必要的类,主要用于包装异常信息. package com.test.exce ...
- apue-ubuntu环境搭建
目录 apue环境搭建 title: apue环境搭建 date: 2019/11/19 19:25:18 toc: true --- apue环境搭建 下载编译 wget http://www.ap ...
- shell 入门学习
目录 shell 入门学习 注释 执行与启动 变量 语法 调试 title: shell 入门学习 date: 2019/7/16 15:47:49 toc: true --- shell 入门学习 ...
- thinkphp5相关
THINKPHP5代码风格规范(基于PSR): https://www.jianshu.com/p/e53d26407e68
- golang的定时器NewTimer、NewTicker使用
package main import ( "fmt" "sync" "time" ) /** *ticker只要定义完成,从此刻开始计时, ...