X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2FMemWatch.java;fp=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2FMemWatch.java;h=2683e7f46d9b81f28bfbe277e169c9b999833f32;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 index 000000000..2683e7f46 --- /dev/null +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/MemWatch.java @@ -0,0 +1,34 @@ +package org.simantics.db.impl; + +import java.lang.ref.WeakReference; + +public class MemWatch { + public static boolean isLowOnMemory() { + return isLowOnMemory; + } + private static boolean DBBUG = false; + private static boolean isLowOnMemory = false; + @SuppressWarnings("unused") + private static MemWatch memWatch = new MemWatch(); + private Runtime runtime = Runtime.getRuntime(); + @SuppressWarnings("unused") + private WeakReference ref = new WeakReference(new Watch()); + class Watch { + @Override + protected void finalize() throws Throwable { + long fre = runtime.freeMemory(); + long tot = runtime.totalMemory(); + long max = runtime.maxMemory(); + if (tot < max || fre > max / 20) + isLowOnMemory = false; + else + isLowOnMemory = true; + if (DBBUG) + System.err.println("Garbage was collected using " + Thread.currentThread().getName() + + " fre=" + fre + " tot=" + tot + " max=" + max + + " low=" + isLowOnMemory); + ref = new WeakReference(new Watch()); + super.finalize(); + } + } +}