ibatis-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alan Charley (JIRA)" <ibatis-...@incubator.apache.org>
Subject [jira] Created: (IBATIS-562) Memory cache using WEAK references can lead to memory leak
Date Mon, 15 Dec 2008 15:47:44 GMT
Memory cache using WEAK references can lead to memory leak
----------------------------------------------------------

                 Key: IBATIS-562
                 URL: https://issues.apache.org/jira/browse/IBATIS-562
             Project: iBatis for Java
          Issue Type: Bug
          Components: SQL Maps
    Affects Versions: 2.3.4
         Environment: Hp NonStop H06.14 , NonStop Java 1.5.0_02  Ibatis build 2.3.4.726 
            Reporter: Alan Charley


Caching objects in memory using WEAK references will lead to memory leaks because GC can occur
on object, yet cache memory still has reference to object.  Since the key is present in the
hashmap but the object being cached has been destroyed, value returns as null indicating a
cache miss. Then the object is cached again, and again, and again. This causes the haspmap
to grow until JVM crashes with out of memory.  

Code snippet below from MemoryCacheController.java 

 public Object getObject(CacheModel cacheModel, Object key) {
    Object value = null;
    Object ref = cache.get(key);
    if (ref != null) {
      if (ref instanceof StrongReference) {
        value = ((StrongReference) ref).get();
      } else if (ref instanceof SoftReference) {
        value = ((SoftReference) ref).get();
      } else if (ref instanceof WeakReference) {
        value = ((WeakReference) ref).get();
      } 
    }
    /*  AJC added this code to prevent memory leak if object key is in cached list but value
had been removed */
    if (value == null) {
    	if (ref != null) {
       	   cache.remove(key);
    	}   
    }
   /* end of code added */

    return value;
  }



-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message