WeakHashMap的remove方法导致对象回收的测试
来源:互联网 发布:虚拟图片打印机软件 编辑:程序博客网 时间:2024/06/08 03:51
定义一个类,在finalize方法中添加log
public class Test{
int value;
public Test() {
// TODO Auto-generated constructor stub
}
public Test(int i) {
// TODO Auto-generated constructor stub
value = i;
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
System.out.println("Test finalize,value=" + value
+ ", this=" + this);
super.finalize();
}
}
public void testWMap()
{
//test WeakHashMap
WeakHashMap<String, Test>wmap = new WeakHashMap<String, Test>();
wmap.put(""+1, new Test(1));
wmap.put(""+2, new Test(2));
wmap.put(""+3, new Test(3));
wmap.put(""+4, new Test(4));
wmap.put(""+5, new Test(5));
System.gc();
System.out.println("wmap.size=" + wmap.size());
wmap.remove("1");
System.out.println("wmap.size=" + wmap.size());
for (int i = 6; i < 30; i++) {
wmap.put(""+i, new Test(i));
System.out.println("== wmap.size=" + wmap.size());
}
//gc do not make the Test object to release
//but remove from the weakmap and than gc can make it release
System.gc();
wmap.remove("2");
for (int i = 0; i < wmap.size(); i++) {
System.out.println("key=" + wmap.keySet().toArray()[i]);
}
System.out.println("== wmap.size=" + wmap.size());
// System.gc();
}
调用testWMap()
wmap.size=5
wmap.size=4
== wmap.size=5
== wmap.size=6
== wmap.size=7
== wmap.size=8
== wmap.size=9
== wmap.size=10
== wmap.size=11
== wmap.size=12
== wmap.size=13
== wmap.size=14
== wmap.size=15
== wmap.size=16
== wmap.size=17
== wmap.size=18
== wmap.size=19
== wmap.size=20
== wmap.size=21
== wmap.size=22
== wmap.size=23
== wmap.size=24
== wmap.size=25
== wmap.size=26
== wmap.size=27
== wmap.size=28
Test finalize,value=1, this=com.foxx.a.File1$Test@ca0b6
key=4
key=5
key=3
== wmap.size=3
可以看出,WeakHashMap中没有被引用的对象可能会被WeakHashMap去掉(remove)
被remove后,再System.gc(),才能够让对象及时的被回收。
WeakHashMap自动remove难以确定何时进行,必要的时候,还是需要调用remove方法
修改为
public void testWMap()
{
//test WeakHashMap
WeakHashMap<String, Test>wmap = new WeakHashMap<String, Test>();
wmap.put(""+1, new Test(1));
wmap.put(""+2, new Test(2));
wmap.put(""+3, new Test(3));
wmap.put(""+4, new Test(4));
wmap.put(""+5, new Test(5));
System.gc();
System.out.println("wmap.size=" + wmap.size());
wmap.remove("1");
System.out.println("wmap.size=" + wmap.size());
for (int i = 6; i < 30; i++) {
wmap.put(""+i, new Test(i));
System.out.println("== wmap.size=" + wmap.size());
}
//gc do not make the Test object to release
//but remove from the weakmap and than gc can make it release
System.gc();
wmap.remove("2");
for (int i = 0; i < wmap.size(); i++) {
System.out.println("key=" + wmap.keySet().toArray()[i]);
}
System.out.println("== wmap.size=" + wmap.size());
System.gc();
}
对象都会被回收
wmap.size=5
wmap.size=4
== wmap.size=5
== wmap.size=6
== wmap.size=7
== wmap.size=8
== wmap.size=9
== wmap.size=10
== wmap.size=11
== wmap.size=12
== wmap.size=13
== wmap.size=14
== wmap.size=15
== wmap.size=16
== wmap.size=17
== wmap.size=18
== wmap.size=19
== wmap.size=20
== wmap.size=21
== wmap.size=22
== wmap.size=23
== wmap.size=24
== wmap.size=25
== wmap.size=26
== wmap.size=27
== wmap.size=28
Test finalize,value=1, this=com.foxx.a.File1$Test@1a758cb
key=4
key=5
key=3
== wmap.size=3
Test finalize,value=13, this=com.foxx.a.File1$Test@69b332
Test finalize,value=29, this=com.foxx.a.File1$Test@173a10f
newString=com.UCMobile
Test finalize,value=28, this=com.foxx.a.File1$Test@530daa
Test finalize,value=27, this=com.foxx.a.File1$Test@a62fc3
Test finalize,value=26, this=com.foxx.a.File1$Test@89ae9e
Test finalize,value=25, this=com.foxx.a.File1$Test@1270b73
Test finalize,value=24, this=com.foxx.a.File1$Test@60aeb0
Test finalize,value=23, this=com.foxx.a.File1$Test@16caf43
Test finalize,value=22, this=com.foxx.a.File1$Test@66848c
Test finalize,value=21, this=com.foxx.a.File1$Test@8813f2
Test finalize,value=20, this=com.foxx.a.File1$Test@1d58aae
Test finalize,value=19, this=com.foxx.a.File1$Test@83cc67
Test finalize,value=18, this=com.foxx.a.File1$Test@e09713
Test finalize,value=17, this=com.foxx.a.File1$Test@156ee8e
Test finalize,value=16, this=com.foxx.a.File1$Test@47b480
Test finalize,value=15, this=com.foxx.a.File1$Test@10d448
Test finalize,value=14, this=com.foxx.a.File1$Test@e0e1c6
Test finalize,value=12, this=com.foxx.a.File1$Test@6ca1c
Test finalize,value=11, this=com.foxx.a.File1$Test@1bf216a
Test finalize,value=10, this=com.foxx.a.File1$Test@12ac982
Test finalize,value=9, this=com.foxx.a.File1$Test@1389e4
Test finalize,value=8, this=com.foxx.a.File1$Test@c20e24
Test finalize,value=7, this=com.foxx.a.File1$Test@2e7263
Test finalize,value=6, this=com.foxx.a.File1$Test@157f0dc
Test finalize,value=2, this=com.foxx.a.File1$Test@863399
阅读全文
0 0
- WeakHashMap的remove方法导致对象回收的测试
- 对WeakHashMap的使用不慎导致内存溢出分析
- List对象remove方法的使用
- List对象remove方法的使用
- WeakHashMap的神话
- WeakHashMap的神话
- WeakHashMap HashMap的区别
- WeakHashMap的一点理解
- WeakHashMap的基本使用方法
- WeakHashMap的用法
- WeakHashMap的使用
- WeakHashMap的理解
- WeakHashMap的使用
- WeakHashMap的一些理解
- WeakHashMap的Weak原理
- WeakHashMap的实现原理
- WeakHashMap的使用场景
- WeakHashMap存在的原因
- 初级面向对象
- 数据结构——二叉树的构建与遍历
- Xshell远程连接工具相关设置技巧
- Java 3.0 java流程控制之分支语句
- QAQ & ORZ 的签到题||多线联盟(三)D
- WeakHashMap的remove方法导致对象回收的测试
- DNS服务器的配置、DNS反解、主从DNS、动态域名解析
- HPU 1415 小ho的01串 (字符串)
- 统计难题
- .Net基础视频教程之面向对象继承
- 问题 E: QAQ & 君临天下 || 天行九歌
- 青蛙跳台阶问题 java实现
- 插入类排序:直接插入和希尔排序
- 图片切换的实现(自定义属性的应用)