X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2FQueryMemoryWatcher.java;fp=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2FQueryMemoryWatcher.java;h=178177a1f2bdfb348c15c999b6e45afe185e4821;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/QueryMemoryWatcher.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/QueryMemoryWatcher.java new file mode 100644 index 000000000..178177a1f --- /dev/null +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/QueryMemoryWatcher.java @@ -0,0 +1,46 @@ +package org.simantics.db.common; + +import org.simantics.db.ReadGraph; +import org.simantics.db.service.QueryControl; + +public class QueryMemoryWatcher { + + final ReadGraph graph; + final QueryControl support; + final int stopTime; + final long baseline; + final long highLimit; + final long lowLimit; + boolean collecting = false; + + public QueryMemoryWatcher(ReadGraph graph, int allowedQueries) { + this(graph, allowedQueries, 0.5, 10); + } + + public QueryMemoryWatcher(ReadGraph graph, int allowedQueries, double ratio, int stopTime) { + this.support = graph.getService(QueryControl.class); + baseline = support.count(); + highLimit = baseline + allowedQueries; + lowLimit = baseline + (int)(allowedQueries*ratio); + this.graph = graph; + this.stopTime = 10; + //System.err.println("QueryMemoryWatcher started with " + baseline + " queries."); + } + + public void maintain() { + long current = support.count(); + if(collecting) { + if(current > lowLimit) support.gc(graph, stopTime); + else { +// System.err.println("QueryMemoryWatcher stops collecting"); + collecting = false; + } + } else { + if(current > highLimit) { +// System.err.println("QueryMemoryWatcher starts collecting"); + collecting = true; + } + } + } + +}