小编Sla*_*ade的帖子

完整GC期间是否清除了WeakHashMap?

我遇到了WeakHashMap的一些麻烦.

考虑以下示例代码:

List<byte[]> list = new ArrayList<byte[]>();

Map<String, Calendar> map = new WeakHashMap<String, Calendar>();
String anObject = new String("string 1");
String anOtherObject = new String("string 2");

map.put(anObject, Calendar.getInstance());
map.put(anOtherObject, Calendar.getInstance());
// In order to test if the weakHashMap works, i remove the StrongReference in this object
anObject = null;
int i = 0;
while (map.size() == 2) {
   byte[] tab = new byte[10000];
   System.out.println("iteration " + i++ + "map size :" + map.size());
   list.add(tab);
}
System.out.println("Map size " + map.size()); …
Run Code Online (Sandbox Code Playgroud)

java jit weak-references

11
推荐指数
3
解决办法
1311
查看次数

标签 统计

java ×1

jit ×1

weak-references ×1