]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.db.impl;\r
2 \r
3 import java.lang.ref.WeakReference;\r
4 \r
5 public class MemWatch {\r
6     public static boolean isLowOnMemory() {\r
7         return isLowOnMemory;\r
8     }\r
9     private static boolean DBBUG = false;\r
10     private static boolean isLowOnMemory = false;\r
11     @SuppressWarnings("unused")\r
12     private static MemWatch memWatch = new MemWatch();\r
13     private Runtime runtime = Runtime.getRuntime();\r
14     @SuppressWarnings("unused")\r
15     private WeakReference<Watch> ref = new WeakReference<Watch>(new Watch());\r
16     class Watch {\r
17         @Override\r
18         protected void finalize() throws Throwable {\r
19             long fre = runtime.freeMemory();\r
20             long tot = runtime.totalMemory();\r
21             long max = runtime.maxMemory();\r
22             if (tot < max || fre > max / 20) \r
23                 isLowOnMemory = false;\r
24             else\r
25                 isLowOnMemory = true;\r
26             if (DBBUG)\r
27               System.err.println("Garbage was collected using " + Thread.currentThread().getName()\r
28               + " fre=" + fre + " tot=" + tot + " max=" + max\r
29               + " low=" + isLowOnMemory);\r
30             ref = new WeakReference<Watch>(new Watch());\r
31             super.finalize();\r
32         }\r
33     }\r
34 }\r