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