java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > HashMap弱引用

Java中的HashMap弱引用之WeakHashMap详解

作者:ywb201314

这篇文章主要介绍了Java中的HashMap弱引用之WeakHashMap详解,当内存空间不足,Java虚拟机宁愿抛出OutOfMemoryError错误,使程序异常终止,也不会靠随意回收具有强引用的对象来解决内存不足的问题,需要的朋友可以参考下

Java 引用的相关知识

1. 强引用

Object o = new Object();

2. 软引用

public class SoftReference<T> extends Reference<T> {...} 

3. 弱引用

public class WeakReference<T> extends Reference<T> {...} 

WeakHashMap 的认识:

1. 类定义

public class WeakHashMap<K,V>
    extends AbstractMap<K,V>
    implements Map<K,V>

2. 重要的全局变量

/**
  * The default initial capacity -- MUST be a power of two.
  * 默认容量,必须是2次幂
  */
private static final int DEFAULT_INITIAL_CAPACITY = 16;
/**
  * The maximum capacity, used if a higher value is implicitly specified by either of the
  * constructors with arguments. MUST be a power of two <= 1<<30.
  * 最大容量,必须为2次幂且 <= 1<<30
  */
private static final int MAXIMUM_CAPACITY = 1 << 30;
/**
  * The load factor used when none specified in constructor.
  * 负载因子
  */
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
  * The table, resized as necessary. Length MUST Always be a power of two.
  * 容量必须为2次幂的数组
  */
Entry<K,V>[] table;
/**
  * The number of key-value mappings contained in this weak hash map.
  * 拥有键值对的数量
  */
private int size;
/**
  * The next size value at which to resize (capacity * load factor).
  * 阈值 -- 扩容判断依据
  */
private int threshold;
/**
  * The load factor for the hash table.
  */
private final float loadFactor;
/**
  * Reference queue for cleared WeakEntries
  * 引用队列,用于存储已被GC清除的WeakEntries
  */
private final ReferenceQueue<Object> queue = new ReferenceQueue<>();

3. 构造器

// 默认构造函数
WeakHashMap()
// 指定"容量大小"的构造函数
WeakHashMap(int capacity)
// 指定"容量大小"和"负载因子"的构造函数
WeakHashMap(int capacity, float loadFactor)
// 包含"子Map"的构造函数
WeakHashMap(Map<? extends K, ? extends V> map)

实现跟JDK1.7版本HashMap的实现一致,具体请参见笔者的HashMap - 基于哈希表和 Map 接口的键值对利器 (JDK 1.7)

4. Entry

/**
  * The entries in this hash table extend WeakReference, using its main ref field as the key.
  * 该Enty继承WeakReference,从而具备弱引用的特性
  */
private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> {
    V value;
    int hash;
    Entry<K,V> next;//链表
    /**
      * Creates new entry.
      */
    Entry(Object key, V value,
            ReferenceQueue<Object> queue,
            int hash, Entry<K,V> next) {
        super(key, queue);
        this.value = value;
        this.hash  = hash;
        this.next  = next;
    }
    ....
}  

WeakHashMap 的重要方法

常规 put(), get(), remove() 等不详细研究,来看下比较关键的expungeStaleEntries()

/**
  * Expunges stale entries from the table. -- 删除过时的entry
  * 该方法是实现弱键回收的最关键方法,也是区分HashMap的根本方法
  * 核心:移除table和queue的并集元素(queue中存储是已被GC的key,注意是key!!)
  * 效果:key在GC的时候被清除,value在key清除后访问WeakHashMap被清除
  */
private void expungeStaleEntries() {
    //从队列中出队遍历
    //poll 移除并返问队列头部的元素;如果队列为空,则返回null
    for (Object x; (x = queue.poll()) != null; ) {
        synchronized (queue) {
            //值得一提的是WeakHashMap是非线程安全的,这里却同步了一下
            //大神原本的用意是保证在多线程时能不破坏数据结构,但JavaDoc已经强调这类非安全,如下文
            //http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6425537
            @SuppressWarnings("unchecked")
                Entry<K,V> e = (Entry<K,V>) x;
            //找到该队列中的元素在数组中的位置,并移除该元素(table和queue都需要移除)    
            int i = indexFor(e.hash, table.length);
            Entry<K,V> prev = table[i];
            Entry<K,V> p = prev;
            while (p != null) {
                Entry<K,V> next = p.next;
                if (p == e) {
                    if (prev == e)
                        table[i] = next;
                    else
                        prev.next = next;
                    // Must not null out e.next;
                    // stale entries may be in use by a HashIterator
                    e.value = null; // Help GC 移除value
                    size--;
                    break;
                }
                prev = p;
                p = next;
            }
        }
    }
}

到此这篇关于Java中的HashMap弱引用之WeakHashMap详解的文章就介绍到这了,更多相关HashMap弱引用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文