I have the following situation: there are a couple of machines forming a cluster. Clients can load data-sets and we need to select the node on which the dataset will be loaded and refuse to load / avoid an OOM error if there is no one machine which could fit the dataset.
What we do currently: we now the
memory to be used
entry count * empirical factor
empirical factor can be calculated as build step and placed in a properties file.
freeMemory() is almost always less than the amount which would be free after a GC, you can check it to see if it is available and call a
System.gc() if the
maxMemory() indicates there might be plenty.
System.gc() in production only makes in very rare situations and in general it often incorrectly used resulting in a reduction in performance and obscuring the real problem.
I would avoid triggering an OOME unless you are running is a JVM you can restart as required.