yannianmu created LUCENE-6211:
---------------------------------
Summary: WeakHashMap may be cause a memory leak problem.
Key: LUCENE-6211
URL: https://issues.apache.org/jira/browse/LUCENE-6211
Project: Lucene - Core
Issue Type: Bug
Components: core/search
Affects Versions: 3.5
Reporter: yannianmu
WeakHashMap may be cause a memory leak problem.
we use SoftReference instad of it like this;
public static class SoftLinkMap{
private static int SORT_CACHE_SIZE=1024;
private static float LOADFACTOR=0.75f;
final Map<Object,SoftReference<Map<Entry,Object>>> readerCache_lru=new
LinkedHashMap<Object,SoftReference<Map<Entry,Object>>>((int)
Math.ceil(SORT_CACHE_SIZE / LOADFACTOR) + 1, LOADFACTOR, true) {
@Override
protected boolean
removeEldestEntry(Map.Entry<Object,SoftReference<Map<Entry,Object>>> eldest) {
return size() > SORT_CACHE_SIZE;
}
};
public void remove(Object key)
{
readerCache_lru.remove(key);
}
public Map<Entry,Object> get(Object key)
{
SoftReference<Map<Entry,Object>> w = readerCache_lru.get(key);
if(w==null)
{
return null;
}
return w.get();
}
public void put(Object key,Map<Entry,Object> value)
{
readerCache_lru.put(key, new SoftReference<Map<Entry,Object>>(value));
}
public Set<java.util.Map.Entry<Object, Map<Entry, Object>>> entrySet()
{
HashMap<Object,Map<Entry,Object>> rtn=new HashMap<Object,
Map<Entry,Object>>();
for(java.util.Map.Entry<Object,SoftReference<Map<Entry,Object>>>
e:readerCache_lru.entrySet())
{
Map<Entry,Object> v=e.getValue().get();
if(v!=null)
{
rtn.put(e.getKey(), v);
}
}
return rtn.entrySet();
}
}
final SoftLinkMap readerCache=new SoftLinkMap();
// final Map<Object,Map<Entry,Object>> readerCache = new
WeakHashMap<Object,Map<Entry,Object>>();
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]