????WeakHashMap ???????????????ж? Key ????????????? Key ????????????????? Map ?????????????????嵥 8 ????????????????? Map ????????? HashMap??????? WeakHashMap?????????? map ?з??? A??B ?????????? HashMap ??? A?????? A??B ????? Null ???WeakHashMap ?е? A ??????????????????????????????????? A ?????????? HashMap ???????? A ??? Null ?????? WeakHashMap ?л????? A ??????????? A ???????????? WeakHashMap ??????????? a???????? B ???????????? null???? HashMap ?л?????? B ????????? WeakHashMap ?????? B ????
?????嵥 8.WeakHashMap ???????
????import java.util.HashMap;
????import java.util.Iterator;
????import java.util.Map;
????import java.util.WeakHashMap;
????public class WeakHashMapTest {
????public static void main(String[] args) throws Exception {
????String a = new String("a");
????String b = new String("b");
????Map weakmap = new WeakHashMap();
????Map map = new HashMap();
????map.put(a?? "aaa");
????map.put(b?? "bbb");
????weakmap.put(a?? "aaa");
????weakmap.put(b?? "bbb");
????map.remove(a);
????a=null;
????b=null;
????System.gc();
????Iterator i = map.entrySet().iterator();
????while (i.hasNext()) {
????Map.Entry en = (Map.Entry)i.next();
????System.out.println("map:"+en.getKey()+":"+en.getValue());
????}
????Iterator j = weakmap.entrySet().iterator();
????while (j.hasNext()) {
????Map.Entry en = (Map.Entry)j.next();
????System.out.println("weakmap:"+en.getKey()+":"+en.getValue());
????}
????}
????}
?????嵥 9 .???????
????map:b:bbb
????weakmap:b:bbb
????WeakHashMap ?????? expungeStaleEntries ??????????????????????????????????????????????????????????? WeakHashMap ????????з?????????????????????????????????????????????????????????????? WeakHashMap?????? GC ????????????? WeakHashMap?? ??????????????????
?????嵥 10. WeakHashMapTest1
????import java.util.ArrayList;
????import java.util.List;
????import java.util.WeakHashMap;
????public class WeakHashMapTest1 {
????public static void main(String[] args) throws Exception {
????List<WeakHashMap<byte[][]?? byte[][]>> maps = new ArrayList<WeakHashMap<byte[][]?? byte[][]>>();
????for (int i = 0; i < 1000; i++) {
????WeakHashMap<byte[][]?? byte[][]> d = new WeakHashMap<byte[][]?? byte[][]>();
????d.put(new byte[1000][1000]?? new byte[1000][1000]);
????maps.add(d);
????System.gc();
????System.err.println(i);
????}
????}
????}
??????????κ? JVM ??????????????嵥 10 ??????????? Java ???????? 64M????????????????
?????嵥 11. ???????
????241
????242
????243
????Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
????at WeakHashMapTest1.main(WeakHashMapTest1.java:10)
?????????????WeakHashMap ???????????????????????????檔?嵥 12 ??????????????????????
?????嵥 12. WeakHashMapTest2
????import java.util.ArrayList;
????import java.util.List;
????import java.util.WeakHashMap;
????public class WeakHashMapTest2 {
????public static void main(String[] args) throws Exception {
????List<WeakHashMap<byte[][]?? byte[][]>> maps = new ArrayList<WeakHashMap<byte[][]?? byte[][]>>();
????for (int i = 0; i < 1000; i++) {
????WeakHashMap<byte[][]?? byte[][]> d = new WeakHashMap<byte[][]?? byte[][]>();
????d.put(new byte[1000][1000]?? new byte[1000][1000]);
????maps.add(d);
????System.gc();
????System.err.println(i);
????for (int j = 0; j < i; j++) {
????System.err.println(j + " size" + maps.get(j).size());
????}
????}
????}
????}
???????н????????β???????????? ?????????????????
????????????WeakHashMap ?????????????????????????????????????????????????????????????????????????
????WeakHashMap ??????????????????? Entry<K??V>?????? WeakReference<K>???
?????? WeakHashMap$Entry<K??V>?????弰?????????????嵥 13 ?????
?????嵥 13. WeakHashMap ????
????private static class Entry<K??V> extends WeakReference<K>
????implements Map.Entry<K??V> Entry(K key?? V value?? ReferenceQueue<K> queue??int hash?? Entry<K??V> next) {
????super(key?? queue);
????this.value = value;
????this.hash = hash;
????this.next = next;
????}
?????????????????????“super(key?? queue);”????????? Key????? Key ???????????????Value ?????????ù????? this.value ??С??? System.gc() ???Key ?е? Byte ??????????????? Value ??????? (Value ????????? Entry ???Entry ??????? Map ?У?Map ?????? ArrayList ??)??
????For ???????ζ? New ????μ? WeakHashMap???? Put ????????? GC ?? WeakReference ?? Key ?е? Byte ??????????????????????? ReferenceQueue????????????????????????? WeakHashMap ????? ReferenceQueue?????? WeakReference ??? Key ????????? WeakHashMap ?У??????? value ?????????
?????? value ?????????????? ???嵥 10 ???嵥 11 ?????????????з?????????嵥 11 ?? maps.get(j).size() ?????? Value ????????????δ?????????? WeakHashMap ???????Size ?????????? expungeStaleEntries ???????÷????? JVM ??????? Entry(Quene ??) ???б????????? Entry ?? Value ????????????檔????Ч???? Key ?? GC ??????????Value ?? Key ???????? WeakHashMap ???????
????WeakHashMap ????????????????????? Collections.synchronizedMap ??????????????? WeakHashMap?? ????????????洢????????????????????????????????????????????????????????????????????????????????ü??????????????????????WeakHashMap ?е????????????????????????????С??????????????????????????????????????????????????????????????????????? WeakHashMap ????????????????????????????????????????????????????????????ü??????????????????????????????????????
???????????????????????????????????????? WeakReferences ?У??磺m.put(key?? new WeakReference(value))?????????? get ???н????????????“collection ???????”????????????????????????????????????????????????????????????????????????? Remove ?? Add ???????????κ?????κη????????????????????? ConcurrentModificationException????????????????????????????????????????e?????????????????????????????????????
???????????????????????????????????????????????????????????????????κ?????????????
???????
??????????????????????????????????????漰??????????е?????????????? List?????????????????????????????????? LinkedList????????????????????????????? ArrayList???????????????????У????????????????????н??У???????????????Ч?????????????????????????????????????????????????????????????? Key ???????????д Equals ?? HashCode ?????????????????????????????緵?? List ???? ArrayList????????????????? ArrayList ???? LinkedList ???????????????????????????б?????
????????????????ò???????????????????????????????????????????????????????????????????????????????????????????????????