Open microacup opened 10 years ago
利用SoftReference和ReferenceQueue来搭配从而达到避免oom的功能。
/** * 继承map是为了在UIManager中 自由切换 map 和softregerencemap */ public class SoftReferenceMap<K, V> extends HashMap<K, V> { private static final long serialVersionUID = 1L; // 将 V 对象封装成软引用的对象,放置在SoftReferenceMap里面 private HashMap<K, SoftValue<K, V>> temp; //用来存储键入了系统回收队列中的对象, 这里对应的是V private ReferenceQueue<V> queue; public SoftReferenceMap() { super(); this.temp = new HashMap<K, SoftValue<K, V>>(); queue = new ReferenceQueue<V>(); } @Override public boolean containsKey(Object key) { // 清空正在被系统回收队列, 否则可能拿到的SoftReference中可能已经没有了 key对应的对象了 clearMap(); return temp.containsKey(key); } @Override public V get(Object key) { clearMap(); SoftValue<K, V> softValue = temp.get(key); if (softValue != null) { return softValue.get(); } return null; } @Override public V put(K key, V value) { SoftValue<K, V> softReference = new SoftValue<K, V>(key, value, queue); temp.put(key, softReference); return null; } // 从temp中清空 已经键入了回收队列的 对象(V) 对应的SoftReference @SuppressWarnings("unchecked") private void clearMap() { //从quene中拿到当前已经加入回收队列的最后一个V SoftValue<K, V> softReference = (SoftValue<K, V>) queue.poll(); while (softReference != null) { temp.remove(softReference.key);//拿到这个V对应的key 把他从temp的map中删除 softReference = (SoftValue<K, V>) queue.poll(); //循环把所有的在回收队列中的值从temp中删除 } } //实现一个带键值对的SoftReference, 用来保存一个key供 temp 快速删除 对应的SoftReference @SuppressWarnings("hiding") private class SoftValue<K, V> extends SoftReference<V> { private K key; public SoftValue(K k, V v, ReferenceQueue<? super V> q) { super(v, q);//把SofrReference注册到Queue中, 系统会在适当时机把 V 加入到回收队列中. this.key = k; } } }
代码2:Netty中的代码
package io.netty.handler.codec.serialization; import java.lang.ref.Reference; import java.lang.ref.SoftReference; import java.util.Map; final class SoftReferenceMap<K, V> extends ReferenceMap<K, V> { public SoftReferenceMap(Map<K, Reference<V>> delegate) { super(delegate); } @Override Reference<V> fold(V value) { return new SoftReference<V>(value); } }
package io.netty.handler.codec.serialization;
import java.lang.ref.Reference; import java.util.Collection; import java.util.Map; import java.util.Set;
abstract class ReferenceMap<K, V> implements Map<K, V> {
private final Map<K, Reference<V>> delegate; protected ReferenceMap(Map<K, Reference<V>> delegate) { this.delegate = delegate; } abstract Reference<V> fold(V value); private V unfold(Reference<V> ref) { if (ref == null) { return null; } return ref.get(); } @Override public int size() { return delegate.size(); } @Override public boolean isEmpty() { return delegate.isEmpty(); } @Override public boolean containsKey(Object key) { return delegate.containsKey(key); } @Override public boolean containsValue(Object value) { throw new UnsupportedOperationException(); } @Override public V get(Object key) { return unfold(delegate.get(key)); } @Override public V put(K key, V value) { return unfold(delegate.put(key, fold(value))); } @Override public V remove(Object key) { return unfold(delegate.remove(key)); } @Override public void putAll(Map<? extends K, ? extends V> m) { for (Entry<? extends K, ? extends V> entry : m.entrySet()) { delegate.put(entry.getKey(), fold(entry.getValue())); } } @Override public void clear() { delegate.clear(); } @Override public Set<K> keySet() { return delegate.keySet(); } @Override public Collection<V> values() { throw new UnsupportedOperationException(); } @Override public Set<Entry<K, V>> entrySet() { throw new UnsupportedOperationException(); }
}
参考地址: > http://my.oschina.net/sfshine/blog/219563 > http://anonsvn.jboss.org/repos/jbosscache/experimental/jsr166/src/jsr166y/ConcurrentReferenceHashMap.java > https://github.com/netty/netty/blob/5725e804bcfbd0c76e0f11e7911a76970eb06c8c/codec/src/main/java/io/netty/handler/codec/serialization/SoftReferenceMap.java
利用SoftReference和ReferenceQueue来搭配从而达到避免oom的功能。
代码2:Netty中的代码
package io.netty.handler.codec.serialization;
import java.lang.ref.Reference; import java.util.Collection; import java.util.Map; import java.util.Set;
abstract class ReferenceMap<K, V> implements Map<K, V> {
}