1 package org.simantics.db.impl;
\r
3 import java.lang.ref.WeakReference;
\r
5 public class MemWatch {
\r
6 public static boolean isLowOnMemory() {
\r
7 return isLowOnMemory;
\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
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
25 isLowOnMemory = true;
\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