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