0
if no
* GC has been run yet
*/
public static long getAverageGCFree() {
long x = 0;
int y = 0;
for (int i=0; igciffail
is set - a Full GC is run, if gciffail
is set to
* false
and not enough memory is available, this method returns false
.
*
* @see serverMemory#request(long, boolean) for another implementation
* @param memory amount of bytes to be assured to be free
* @param gciffail if not enough memory is available, this parameter specifies whether to perform
* a Full GC to free enough RAM
* @return whether enough RAM is available
* @deprecated use {@link serverMemory#request(long, boolean)} instead to enable the collection of
* heuristics about explicit usage of Full GCs.
*/
public static boolean available(long memory, boolean gciffail) {
if (available() >= memory) return true;
if (!gciffail) return false;
gc(4000, "serverMemory.available(...)");
return (available() >= memory);
}
public static long available() {
// memory that is available including increasing total memory up to maximum
return max - runtime.totalMemory() + runtime.freeMemory();
}
/**
* Tries to free a specified amount of bytes.
*
* If the currently available memory is enough, the method returns true
without
* performing additional steps. If not, the behaviour depends on the parameter force
.
* If false
, a Full GC is only performed if former GCs indicated that a GC should
* provide enough free memory. If former GCs didn't but force
is set to true
* a Full GC is performed nevertheless.
*
* Setting the force
parameter to false doesn't necessarily mean, that no GC may be
* performed by this method, if the currently available memory doesn't suffice!
*
Be careful with this method as GCs should always be the last measure to take
* * @param size the requested amount of free memory in bytes * @param force specifies whether a GC should be run even in case former GCs didn't provide enough memory * @return whether enough memory could be freed (or is free) or not */ public static boolean request(final long size, final boolean force) { long avail = available(); if (avail >= size) return true; if (log.isFine()) { String t = new Throwable("Stack trace").getStackTrace()[1].toString(); log.logFine(t + " requested " + (size >> 10) + " KB, got " + (avail >> 10) + " KB"); } final long avg = getAverageGCFree(); if (force || avg == 0 || avg + avail >= size) { // this is only called if we expect that an allocation of