package org.simantics.db.impl; import java.lang.ref.WeakReference; public class MemWatch { public static boolean isLowOnMemory() { return isLowOnMemory; } private static boolean DBBUG = false; private static boolean isLowOnMemory = false; @SuppressWarnings("unused") private static MemWatch memWatch = new MemWatch(); private Runtime runtime = Runtime.getRuntime(); @SuppressWarnings("unused") private WeakReference ref = new WeakReference(new Watch()); class Watch { @Override protected void finalize() throws Throwable { long fre = runtime.freeMemory(); long tot = runtime.totalMemory(); long max = runtime.maxMemory(); if (tot < max || fre > max / 20) isLowOnMemory = false; else isLowOnMemory = true; if (DBBUG) System.err.println("Garbage was collected using " + Thread.currentThread().getName() + " fre=" + fre + " tot=" + tot + " max=" + max + " low=" + isLowOnMemory); ref = new WeakReference(new Watch()); super.finalize(); } } }