Improve GcUtil

Increase duration for autoReleaseMemory from 60 to 120 sec.
Improve logging.
Add print stack trace when in dev mode to show caller for debugging/tuning.
Remove inefficient GC calls (based on test runs when no reduction occurred at those calls).
This commit is contained in:
chimp1984 2021-10-26 13:56:35 +02:00
parent c559e6e044
commit 7b68686b28
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3
3 changed files with 19 additions and 9 deletions

View File

@ -501,8 +501,6 @@ public class PersistenceManager<T extends PersistableEnvelope> {
if (completeHandler != null) {
UserThread.execute(completeHandler);
}
GcUtil.maybeReleaseMemory();
}
}

View File

@ -18,6 +18,7 @@
package bisq.common.util;
import bisq.common.UserThread;
import bisq.common.app.DevEnv;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@ -47,18 +48,31 @@ public class GcUtil {
* @param trigger Threshold for free memory in MB when we invoke the garbage collector
*/
private static void autoReleaseMemory(long trigger) {
UserThread.runPeriodically(() -> maybeReleaseMemory(trigger), 60);
UserThread.runPeriodically(() -> maybeReleaseMemory(trigger), 120);
}
/**
* @param trigger Threshold for free memory in MB when we invoke the garbage collector
*/
private static void maybeReleaseMemory(long trigger) {
long totalMemory = Runtime.getRuntime().totalMemory();
if (totalMemory > trigger * 1024 * 1024) {
log.info("Invoke garbage collector. Total memory: {} {} {}", Utilities.readableFileSize(totalMemory), totalMemory, trigger * 1024 * 1024);
long ts = System.currentTimeMillis();
long preGcMemory = Runtime.getRuntime().totalMemory();
if (preGcMemory > trigger * 1024 * 1024) {
System.gc();
log.info("Total memory after gc() call: {}", Utilities.readableFileSize(Runtime.getRuntime().totalMemory()));
long postGcMemory = Runtime.getRuntime().totalMemory();
log.info("GC reduced memory by {}. Total memory before/after: {}/{}. Took {} ms.",
Utilities.readableFileSize(preGcMemory - postGcMemory),
Utilities.readableFileSize(preGcMemory),
Utilities.readableFileSize(postGcMemory),
System.currentTimeMillis() - ts);
if (DevEnv.isDevMode()) {
try {
// To see from where we got called
throw new RuntimeException("Dummy Exception for print stacktrace at maybeReleaseMemory");
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
}

View File

@ -24,7 +24,6 @@ import bisq.core.dao.state.DaoStateService;
import bisq.core.dao.state.model.blockchain.Block;
import bisq.common.app.DevEnv;
import bisq.common.util.GcUtil;
import org.bitcoinj.core.Coin;
@ -119,7 +118,6 @@ public class BlockParser {
blockHeight, duration);
}
GcUtil.maybeReleaseMemory();
return block;
}