You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ibatis.apache.org by "Larry Meadors (JIRA)" <ib...@incubator.apache.org> on 2008/12/15 17:03:44 UTC

[jira] Commented: (IBATIS-562) Memory cache using WEAK references can lead to memory leak

    [ https://issues.apache.org/jira/browse/IBATIS-562?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12656647#action_12656647 ] 

Larry Meadors commented on IBATIS-562:
--------------------------------------

I think I'd like to see more on this one before we make a change.

Let's say I run a sql statement whose cache key is "A".

It looks in the cache for "A", finds it, but it's null because it got taken out by the GC.

The query runs, and the entry is replaced by the results using the key "A" again.

So what's the value of removing "A" before replacing it?

> 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.