EHC ❯ Element having a reference to a key can cause excess heap usage
-
Bug
-
Status: Open
-
2 Major
-
Resolution:
-
ehcache-core
-
-
cdennis
-
Reporter: cdennis
-
November 18, 2011
-
0
-
Watchers: 4
-
December 02, 2013
-
Description
When performing an update (a put that overwrites another mapping) on a in-heap cached value we can end up with a cache mapping holding too {{equals()}} but different key objects in the heap. Consider the following simple example:
String keyOne = new String("key");
String keyTwo = new String("key");
cache.put(new Element(keyOne, "value"));
cache.put(new Element(keyTwo, "value"));
In the first put the cache uses the {{keyOne}} reference as the key for the new mapping:
{ keyOne : Element(keyOne, "value") }
In the second put {{keyTwo}} matches against {{keyOne}} and as is the convention for maps the value is replaced but not the key:
{ keyOne : Element(keyTwo, "value") }
After the second put the cache has ended up strongly referencing both instances of the key. For usages where this happens frequently and the key objects are not dominated in size by the value objects this can lead to non-negligible extra heap usage.
Comments
Chris Dennis 2013-01-21
Fiona OShea 2013-01-22
Setting the doc flag and moving this out until cache overhaul work
In all honesty unless we get internal pressure to fix this I just don’t see the value - he’s right that it’s a pretty stupid thing to do - but it’s really an “accident” of having an ELement class. Something we can fix when we fully overhaul the Ehcache API.