LRU LinkedHashMap that limits size based on available memory

Posted by sanity on Stack Overflow See other posts from Stack Overflow or by sanity
Published on 2009-10-29T20:13:22Z Indexed on 2010/03/14 16:15 UTC
Read the original article Hit count: 153

I want to create a LinkedHashMap which will limit its size based on available memory (ie. when freeMemory + (maxMemory - allocatedMemory) gets below a certain threshold). This will be used as a form of cache, probably using "least recently used" as a caching strategy.

My concern though is that allocatedMemory also includes (I assume) un-garbage collected data, and thus will over-estimate the amount of used memory. I'm concerned about the unintended consequences this might have.

For example, the LinkedHashMap may keep deleting items because it thinks there isn't enough free memory, but the free memory doesn't increase because these deleted items aren't being garbage collected immediately.

Does anyone have any experience with this type of thing? Is my concern warranted? If so, can anyone suggest a good approach?

I should add that I also want to be able to "lock" the cache, basically saying "ok, from now on don't delete anything because of memory usage issues".

© Stack Overflow or respective owner

Related posts about java

Related posts about memory-usage