• WeakHashMap 源码解析


    目录

    一. 前言

    二. 源码解析

    2.1. 类结构

    2.2. 成员变量

    2.3. 构造方法

    2.4. Entry

    2.5. 添加元素

    2.6. 扩容

    2.7. 删除元素

    2.8. 获取元素


    一. 前言

        WeakHashMap,从名字可以看出它是某种 Map。它的特殊之处在于 WeakHashMap 里的entry可能会被GC自动删除,即使程序员没有调用remove()或者clear()方法。

        更直观的说,当使用 WeakHashMap 时,即使没有显示的添加或删除任何元素,也可能发生如下情况:
    1. 调用两次size()方法返回不同的值;
    2. 两次调用isEmpty()方法,第一次返回false,第二次返回true;
    3. 两次调用containsKey()方法,第一次返回true,第二次返回false,尽管两次使用的是同一个key;
    4. 两次调用get()方法,第一次返回一个value,第二次返回null,尽管两次使用的是同一个对象。

        遇到这么奇葩的现象,你是不是觉得使用者一定会疯掉?其实不然,WeakHashMap 的这个特点特别适用于需要缓存的场景。在缓存场景下,由于内存是有限的,不能缓存所有对象;对象缓存命中可以提高系统效率,但缓存MISS也不会造成错误,因为可以通过计算重新得到。

        要明白 WeakHashMap 的工作原理,还需要引入一个概念:弱引用(WeakReference)。我们都知道Java中的内存是通过GC自动管理的,GC会在程序运行过程中自动判断哪些对象是可以被回收的,并在合适的时机进行内存释放。GC判断某个对象是否可被回收的依据是,是否有有效的引用指向该对象。如果没有有效引用指向该对象(基本意味着不存在访问该对象的方式),那么该对象就是可回收的。这里的有效引用并不包括弱引用。也就是说,虽然弱引用可以用来访问对象,但进行垃圾回收时弱引用并不会被考虑在内,仅有弱引用指向的对象仍然会被GC回收。

        WeakHashMap 内部是通过弱引用来管理entry的,弱引用的特性对应到 WeakHashMap 上意味着什么呢?将一对key, value放入到 WeakHashMap 里并不能避免该key值被GC回收,除非在 WeakHashMap 之外还有对该key的强引用。

    关于强引用,弱引用等概念请参见《Java 四种引用类型》

    二. 源码解析

        如果你看过前几篇关于 Map 和 Set 的讲解,一定会问:既然有 WeakHashMap,是否有 WeekHashSet 呢?答案是没有。不过Java Collections工具类给出了解决方案,Collections.newSetFromMap(Map map) 方法可以将任何 Map包装成一个Set。通过如下方式可以快速得到一个 WeakHashSet:

    1. // 将WeakHashMap包装成一个Set
    2. Set weakHashSet = Collections.newSetFromMap(
    3. new WeakHashMap());
    4. // Collections.newSetFromMap()用于将任何Map包装成一个Set
    5. public static Set newSetFromMap(Map map) {
    6. return new SetFromMap<>(map);
    7. }
    8. private static class SetFromMap extends AbstractSet
    9. implements Set, Serializable
    10. {
    11. private final Map m; // The backing map
    12. private transient Set s; // Its keySet
    13. SetFromMap(Map map) {
    14. if (!map.isEmpty())
    15. throw new IllegalArgumentException("Map is non-empty");
    16. m = map;
    17. s = map.keySet();
    18. }
    19. public void clear() { m.clear(); }
    20. public int size() { return m.size(); }
    21. public boolean isEmpty() { return m.isEmpty(); }
    22. public boolean contains(Object o) { return m.containsKey(o); }
    23. public boolean remove(Object o) { return m.remove(o) != null; }
    24. public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; }
    25. public Iterator iterator() { return s.iterator(); }
    26. public Object[] toArray() { return s.toArray(); }
    27. public T[] toArray(T[] a) { return s.toArray(a); }
    28. public String toString() { return s.toString(); }
    29. public int hashCode() { return s.hashCode(); }
    30. public boolean equals(Object o) { return o == this || s.equals(o); }
    31. public boolean containsAll(Collection c) {return s.containsAll(c);}
    32. public boolean removeAll(Collection c) {return s.removeAll(c);}
    33. public boolean retainAll(Collection c) {return s.retainAll(c);}
    34. // addAll is the only inherited implementation
    35. // Override default methods in Collection
    36. @Override
    37. public void forEach(Consumersuper E> action) {
    38. s.forEach(action);
    39. }
    40. @Override
    41. public boolean removeIf(Predicatesuper E> filter) {
    42. return s.removeIf(filter);
    43. }
    44. @Override
    45. public Spliterator spliterator() {return s.spliterator();}
    46. @Override
    47. public Stream stream() {return s.stream();}
    48. @Override
    49. public Stream parallelStream() {return s.parallelStream();}
    50. private static final long serialVersionUID = 2454657854757543876L;
    51. private void readObject(java.io.ObjectInputStream stream)
    52. throws IOException, ClassNotFoundException
    53. {
    54. stream.defaultReadObject();
    55. s = m.keySet();
    56. }
    57. }
    58. 2.1. 类结构

      从图中可以看出:WeakHashMap继承于AbstractMap,并且实现了Map接口。

      2.2. 成员变量

      1. // 默认的初始容量是16,必须是2的幂。
      2. private static final int DEFAULT_INITIAL_CAPACITY = 16;
      3. // 最大容量(必须是2的幂且小于2的30次方,传入容量过大将被这个值替换)
      4. private static final int MAXIMUM_CAPACITY = 1 << 30;
      5. // 默认加载因子
      6. private static final float DEFAULT_LOAD_FACTOR = 0.75f;
      7. // 存储数据的Entry数组,长度是2的幂。
      8. // WeakHashMap是采用拉链法实现的,每一个Entry本质上是一个单向链表
      9. private Entry[] table;
      10. // WeakHashMap的大小,它是WeakHashMap保存的键值对的数量
      11. private int size;
      12. // WeakHashMap的阈值,用于判断是否需要调整WeakHashMap的容量(threshold = 容量*加载因子)
      13. private int threshold;
      14. // 加载因子实际大小
      15. private final float loadFactor;
      16. // queue保存的是“已被GC清除”的“弱引用的键”。
      17. // 弱引用和ReferenceQueue 是联合使用的:如果弱引用所引用的对象被垃圾回收,Java虚拟机就会把这个弱引用加入到与之关联的引用队列中
      18. private final ReferenceQueue queue = new ReferenceQueue<>();
      19. // WeakHashMap被改变的次数
      20. int modCount;
      21. 2.3. 构造方法

        1. // 指定“容量大小”和“加载因子”的构造函数
        2. public WeakHashMap(int initialCapacity, float loadFactor) {
        3. if (initialCapacity < 0)
        4. throw new IllegalArgumentException("Illegal Initial Capacity: "+
        5. initialCapacity);
        6. // WeakHashMap的最大容量只能是MAXIMUM_CAPACITY
        7. if (initialCapacity > MAXIMUM_CAPACITY)
        8. initialCapacity = MAXIMUM_CAPACITY;
        9. if (loadFactor <= 0 || Float.isNaN(loadFactor))
        10. throw new IllegalArgumentException("Illegal Load factor: "+
        11. loadFactor);
        12. // 找出“大于initialCapacity”的最小的2的幂
        13. int capacity = 1;
        14. while (capacity < initialCapacity)
        15. capacity <<= 1;
        16. // 创建Entry数组,用来保存数据
        17. table = newTable(capacity);
        18. // 设置“加载因子”
        19. this.loadFactor = loadFactor;
        20. // 设置“WeakHashMap阈值”,当WeakHashMap中存储数据的数量达到threshold时,就需要将WeakHashMap的容量加倍。
        21. threshold = (int)(capacity * loadFactor);
        22. }
        23. // 指定“容量大小”的构造函数
        24. public WeakHashMap(int initialCapacity) {
        25. this(initialCapacity, DEFAULT_LOAD_FACTOR);
        26. }
        27. // 默认构造函数。
        28. public WeakHashMap() {
        29. this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
        30. }
        31. // 包含“子Map”的构造函数
        32. public WeakHashMap(Map m) {
        33. this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
        34. DEFAULT_INITIAL_CAPACITY),
        35. DEFAULT_LOAD_FACTOR);
        36. // 将m中的全部元素逐个添加到WeakHashMap中
        37. putAll(m);
        38. }
        39. private Entry[] newTable(int n) {
        40. return (Entry[]) new Entry[n];
        41. }

        2.4. Entry

        1. private static class Entry extends WeakReference implements Map.Entry {
        2. V value;
        3. final int hash;
        4. Entry next;
        5. /**
        6. * Creates new entry.
        7. */
        8. Entry(Object key, V value,
        9. ReferenceQueue queue,
        10. int hash, Entry next) {
        11. super(key, queue);
        12. this.value = value;
        13. this.hash = hash;
        14. this.next = next;
        15. }
        16. @SuppressWarnings("unchecked")
        17. public K getKey() {
        18. return (K) WeakHashMap.unmaskNull(get());
        19. }
        20. public V getValue() {
        21. return value;
        22. }
        23. public V setValue(V newValue) {
        24. V oldValue = value;
        25. value = newValue;
        26. return oldValue;
        27. }
        28. public boolean equals(Object o) {
        29. if (!(o instanceof Map.Entry))
        30. return false;
        31. Map.Entry e = (Map.Entry)o;
        32. K k1 = getKey();
        33. Object k2 = e.getKey();
        34. if (k1 == k2 || (k1 != null && k1.equals(k2))) {
        35. V v1 = getValue();
        36. Object v2 = e.getValue();
        37. if (v1 == v2 || (v1 != null && v1.equals(v2)))
        38. return true;
        39. }
        40. return false;
        41. }
        42. public int hashCode() {
        43. K k = getKey();
        44. V v = getValue();
        45. return Objects.hashCode(k) ^ Objects.hashCode(v);
        46. }
        47. public String toString() {
        48. return getKey() + "=" + getValue();
        49. }
        50. }
        51. 可以看到Entry是继承WeakReference的,我们结合WeakReference再看一下:

          1. public class WeakReference extends Reference {
          2. /**
          3. * Creates a new weak reference that refers to the given object. The new
          4. * reference is not registered with any queue.
          5. * 创建一个新的弱应用给传入的对象,这个新的引用不注册任何队列
          6. *
          7. * @param referent object the new weak reference will refer to
          8. */
          9. public WeakReference(T referent) {
          10. super(referent);
          11. }
          12. /**
          13. * Creates a new weak reference that refers to the given object and is
          14. * registered with the given queue.
          15. * 创建一个新的弱应用给传入的对象,这个新的引用注册给一个给定的队列
          16. *
          17. * @param referent object the new weak reference will refer to
          18. * @param q the queue with which the reference is to be registered,
          19. * or null if registration is not required
          20. */
          21. public WeakReference(T referent, ReferenceQueuesuper T> q) {
          22. super(referent, q);
          23. }
          24. }

          我们发现在WeakHashMap中把key注册给了WeakReference,也就是说在WeakHashMap中key是一个弱引用。

          2.5. 添加元素

          1. public V put(K key, V value) {
          2. // 如果key是null则给定一个空的对象进行修饰
          3. Object k = maskNull(key);
          4. // 计算key的hash
          5. int h = hash(k);
          6. // 获取table
          7. Entry[] tab = getTable();
          8. // 根据hash找到数组下标
          9. int i = indexFor(h, tab.length);
          10. // 找到链表中元素位置
          11. for (Entry e = tab[i]; e != null; e = e.next) {
          12. if (h == e.hash && eq(k, e.get())) {
          13. V oldValue = e.value;
          14. if (value != oldValue)
          15. e.value = value;
          16. return oldValue;
          17. }
          18. }
          19. modCount++;
          20. Entry e = tab[i];
          21. tab[i] = new Entry<>(k, value, queue, h, e);
          22. if (++size >= threshold) // 是否达到阀值达到阀值就扩容
          23. resize(tab.length * 2);
          24. return null;
          25. }
          26. private Entry[] getTable() {
          27. expungeStaleEntries();
          28. return table;
          29. }
          30. private void expungeStaleEntries() {
          31. // 从 ReferenceQueue中拉取元素
          32. for (Object x; (x = queue.poll()) != null; ) {
          33. synchronized (queue) {
          34. @SuppressWarnings("unchecked")
          35. Entry e = (Entry) x;
          36. int i = indexFor(e.hash, table.length);
          37. Entry prev = table[i];
          38. Entry p = prev;
          39. while (p != null) {
          40. Entry next = p.next;
          41. if (p == e) {
          42. if (prev == e)
          43. table[i] = next;
          44. else
          45. prev.next = next;
          46. // Must not null out e.next;
          47. // stale entries may be in use by a HashIterator
          48. // 拿到entry的值赋值为null帮助GC
          49. e.value = null; // Help GC
          50. size--;
          51. break;
          52. }
          53. prev = p;
          54. p = next;
          55. }
          56. }
          57. }
          58. }

          expungeStaleEntries 就是WeakHashMap的核心了,它承担着Map中死对象的清理工作。原理就是依赖WeakReference和ReferenceQueue的特性。在每个WeakHashMap都有个ReferenceQueue queue,在Entry初始化的时候也会将queue传给WeakReference,这样当某个key可以失去所有强引用之后,其key对应的WeakReference对象会被放到queue里,有了queue就知道需要清理哪些Entry了。

          这里也是整个WeakHashMap里唯一加了同步的地方。除了上文说的到resize中调用了expungeStaleEntries(),size()中也调用了这个清理方法。另外 getTable()也调了,这就意味着几乎所有其他方法都间接调用了清理。

          1. // 将"m"的全部元素都添加到WeakHashMap中
          2. public void putAll(Map m) {
          3. int numKeysToBeAdded = m.size();
          4. if (numKeysToBeAdded == 0)
          5. return;
          6. // 计算容量是否足够,
          7. // 若“当前实际容量 < 需要的容量”,则将容量x2。
          8. if (numKeysToBeAdded > threshold) {
          9. int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
          10. if (targetCapacity > MAXIMUM_CAPACITY)
          11. targetCapacity = MAXIMUM_CAPACITY;
          12. int newCapacity = table.length;
          13. while (newCapacity < targetCapacity)
          14. newCapacity <<= 1;
          15. if (newCapacity > table.length)
          16. resize(newCapacity);
          17. }
          18. // 将“m”中的元素逐个添加到WeakHashMap中。
          19. for (Map.Entryextends K, ? extends V> e : m.entrySet())
          20. put(e.getKey(), e.getValue());
          21. }

          2.6. 扩容

          1. // 重新调整WeakHashMap的大小,newCapacity是调整后的单位
          2. void resize(int newCapacity) {
          3. Entry[K, V] oldTable = getTable();
          4. int oldCapacity = oldTable.length;
          5. if (oldCapacity == MAXIMUM_CAPACITY) {
          6. threshold = Integer.MAX_VALUE;
          7. return;
          8. }
          9. // 新建一个newTable,将“旧的table”的全部元素添加到“新的newTable”中,
          10. // 然后,将“新的newTable”赋值给“旧的table”。
          11. Entry[K, V] newTable = new Table(newCapacity);
          12. transfer(oldTable, newTable);
          13. table = newTable;
          14. if (size >= threshold / 2) {
          15. threshold = (int)(newCapacity * loadFactor);
          16. } else {
          17. // 删除table中“已被GC回收的key对应的键值对”
          18. expungeStaleEntries();
          19. transfer(newTable, oldTable);
          20. table = oldTable;
          21. }
          22. }
          23. // 将WeakHashMap中的全部元素都添加到newTable中
          24. private void transfer(Entry[] src, Entry[] dest) {
          25. for (int j = 0; j < src.length; ++j) {
          26. Entry e = src[j];
          27. src[j] = null;
          28. while (e != null) {
          29. Entry next = e.next;
          30. Object key = e.get();
          31. if (key == null) {
          32. e.next = null; // Help GC
          33. e.value = null; // " "
          34. size--;
          35. } else {
          36. int i = indexFor(e.hash, dest.length);
          37. e.next = dest[i];
          38. dest[i] = e;
          39. }
          40. e = next;
          41. }
          42. }
          43. }

          2.7. 删除元素

          1. // 删除“键为key”元素
          2. public V remove(Object key) {
          3. Object k = maskNull(key);
          4. // 获取哈希值。
          5. int h = hash(k);
          6. Entry[] tab = getTable();
          7. int i = indexFor(h, tab.length);
          8. Entry prev = tab[i];
          9. Entry e = prev;
          10. // 删除链表中“键为key”的元素
          11. // 本质是“删除单向链表中的节点”
          12. while (e != null) {
          13. Entry next = e.next;
          14. if (h == e.hash && eq(k, e.get())) {
          15. modCount++;
          16. size--;
          17. if (prev == e)
          18. tab[i] = next;
          19. else
          20. prev.next = next;
          21. return e.value;
          22. }
          23. prev = e;
          24. e = next;
          25. }
          26. return null;
          27. }
          28. // 删除“键值对”
          29. boolean removeMapping(Object o) {
          30. if (!(o instanceof Map.Entry))
          31. return false;
          32. Entry[] tab = getTable();
          33. Map.Entry entry = (Map.Entry)o;
          34. Object k = maskNull(entry.getKey());
          35. int h = hash(k);
          36. int i = indexFor(h, tab.length);
          37. Entry prev = tab[i];
          38. Entry e = prev;
          39. // 删除链表中的“键值对e”
          40. // 本质是“删除单向链表中的节点”
          41. while (e != null) {
          42. Entry next = e.next;
          43. if (h == e.hash && e.equals(entry)) {
          44. modCount++;
          45. size--;
          46. if (prev == e)
          47. tab[i] = next;
          48. else
          49. prev.next = next;
          50. return e;
          51. }
          52. prev = e;
          53. e = next;
          54. }
          55. return false;
          56. }
          57. // 清空WeakHashMap,将所有的元素设为null
          58. public void clear() {
          59. while (queue.poll() != null)
          60. ;
          61. modCount++;
          62. Arrays.fill(table, null);
          63. size = 0;
          64. while (queue.poll() != null)
          65. ;
          66. }

          2.8. 获取元素

          1. // 获取key对应的value
          2. public V get(Object key) {
          3. Object k = maskNull(key);
          4. // 获取key的hash值。
          5. int h = hash(k);
          6. Entry[] tab = getTable();
          7. int index = indexFor(h, tab.length);
          8. Entry e = tab[index];
          9. // 在“该hash值对应的链表”上查找“键值等于key”的元素
          10. while (e != null) {
          11. if (e.hash == h && eq(k, e.get()))
          12. return e.value;
          13. e = e.next;
          14. }
          15. return null;
          16. }
          17. // WeakHashMap是否包含key
          18. public boolean containsKey(Object key) {
          19. return getEntry(key) != null;
          20. }
          21. // 返回“键为key”的键值对
          22. Entry getEntry(Object key) {
          23. Object k = maskNull(key);
          24. int h = hash(k);
          25. Entry[] tab = getTable();
          26. int index = indexFor(h, tab.length);
          27. Entry e = tab[index];
          28. while (e != null && !(e.hash == h && eq(k, e.get())))
          29. e = e.next;
          30. return e;
          31. }

        52. 相关阅读:
          qnx sh: rm: Arg list too long
          写了这么久Java项目,是否还记得你的第一行Java代码
          【精选】矩阵加速
          centos 安装和卸载 webmin
          HTML5 基础
          vue3的单组件的编写(三)【响应式 API 之 toRef 与 toRefs】
          史上最全SpringCloud微服务笔记,掌握已超过80%Java面试者
          普冉PY32系列(五) 使用JLink RTT代替串口输出日志
          第三套.py
          dockerfile制作-pytoch+深度学习环境版
        53. 原文地址:https://blog.csdn.net/mrluo735/article/details/134048651