We have a collection of objects which grows quite large over time. We have implemented a caching strategy to help alleviate this, however we are still running out of Heap Space at run time - if enough memory isn't allocated at startup.
Is there a standard mechanism to reduce the size of this cache at runtime to remove these OutOFMemory errors? This way if our process is started with a smaller slice of memory than normal we can hopefully avoid the server dying.
I realize that this is an Error type and thus shouldn't be caught / processed as it's normally indicative of more serious issue.
Is it as simple as having something as follows:
private static final long RECOMMENDED_MEMORY = 1073741824L; //Example 1 Gig
private static int recommendedCacheSize = 100; //Example of 100 items
long heapSize = Runtime.getRuntime().totalMemory();
double size = Math.floor((double) heapSize/RECOMMENDED_MEMORY * recommendedCacheSize);
recommendedCacheSize = ++size;