]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/MemWatch.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / MemWatch.java
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/MemWatch.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/MemWatch.java
new file mode 100644 (file)
index 0000000..2683e7f
--- /dev/null
@@ -0,0 +1,34 @@
+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