// serverMemory.java
// -------------------------------------------
// (C) 2005 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
// first published 22.09.2005 on http://yacy.net
//
// $LastChangedDate$
// $LastChangedRevision$
// $LastChangedBy$
//
// LICENSE
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package de.anomic.server;
import de.anomic.server.logging.serverLog;
import de.anomic.tools.yFormatter;
/**
* Use this to get information about memory usage or try to free some memory
*/
public class serverMemory {
private static final Runtime runtime = Runtime.getRuntime();
private static final serverLog log = new serverLog("MEMORY");
private static final long[] gcs = new long[5];
private static int gcs_pos = 0;
private static long lastGC;
/**
* Runs the garbage collector if last garbage collection is more than last millis ago
* @param last time which must be passed since lased gc
* @param info additional info for log
*/
public final synchronized static void gc(int last, String info) { // thq
long elapsed = System.currentTimeMillis() - lastGC;
if (elapsed > last) {
long free = free();
System.gc();
lastGC = System.currentTimeMillis();
if (log.isFine()) log.logInfo("[gc] before: " + bytesToString(free) + ", after: " + bytesToString(free()) + ", call: " + info);
} else if (log.isFine()) {
if (log.isFinest()) log.logFinest("[gc] no execute, last run: " + (elapsed / 1000) + " seconds ago, call: " + info);
}
}
/**
* Tries to free count bytes
* @param count bytes
* @return the amount of freed bytes by a forced GC this method performes
*/
private static long runGC(final boolean count) {
final long memBefore = available();
gc(1000, "serverMemory.runGC(...)");
final long freed = available() - memBefore;
if (count) {
gcs[gcs_pos] = freed;
gcs_pos = (gcs_pos + 1) % gcs.length;
}
return freed;
}
/**
* This method calculates the average amount of bytes freed by the last GCs forced by this class
* @return the average amount of freed bytes of the last forced GCs or 0
if no
* GC has been run yet
*/
public static long getAverageGCFree() {
long x = 0;
int y = 0;
for (int i=0; i
* 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