WeakHashMap may be cause a memory leak problem. [LUCENE-6211]
- Dominant language
- Java
- Stars
- 3.6k
- Forks
- 1.4k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 88
Description
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> readerCache_lru=new LinkedHashMap<Object,SoftReference>((int) Math.ceil(SORT_CACHE_SIZE / LOADFACTOR) + 1, LOADFACTOR, true) {
`@Override`
protected boolean removeEldestEntry(Map.Entry<Object,SoftReference> eldest) {
return size() > SORT_CACHE_SIZE;
}
};
public void remove(Object key)
{
readerCache_lru.remove(key);
}
public Map<Entry,Object> get(Object key)
{
SoftReference<Map 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(value));
}
public Set<java.util.Map.Entry> entrySet()
{
HashMap<Object,Map rtn=new HashMap<Object, Map();
for(java.util.Map.Entry<Object,SoftReference> 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 readerCache = new WeakHashMap<Object,Map();
---
Migrated from [LUCENE-6211](https://issues.apache.org/jira/browse/LUCENE-6211) by yannianmu
Contributor guide
Research direction
Locate the readerCache WeakHashMap usage in the Lucene source and review the proposed SoftLinkMap implementation in this issue. Compare the cache's current behavior with the proposed reference and eviction behavior; the issue is complete only after the memory-leak concern and replacement approach are agreed and validated.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend, performance
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100