From d0a754ed1f45da4ac843a82c73c14ad523bb3dc6 Mon Sep 17 00:00:00 2001 From: HenrikJannsen Date: Mon, 30 Sep 2024 18:05:15 +0700 Subject: [PATCH] Make MemoryReport singleton and pin it to a field so that it does not get GC'ed --- .../bisq/application/ApplicationService.java | 6 ++++-- .../bisq1_bridge/Bisq1BridgeService.java | 9 ++++++--- .../bisq/common/platform/MemoryReport.java | 19 +++++++++++++------ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/bisq/application/ApplicationService.java b/application/src/main/java/bisq/application/ApplicationService.java index 259eb78bdf..7bbf3a0f80 100644 --- a/application/src/main/java/bisq/application/ApplicationService.java +++ b/application/src/main/java/bisq/application/ApplicationService.java @@ -124,7 +124,8 @@ public Config(Path baseDir, protected final Config config; @Getter protected final PersistenceService persistenceService; - + @SuppressWarnings("FieldCanBeLocal") // Pin it so that it does not get GC'ed + private final MemoryReport memoryReport; private FileLock instanceLock; public ApplicationService(String configFileName, String[] args) { @@ -170,7 +171,8 @@ public ApplicationService(String configFileName, String[] args) { log.info("Using custom config file"); } - MemoryReport.printPeriodically(config.getMemoryReportIntervalSec(), config.isIncludeThreadListInMemoryReport()); + memoryReport = MemoryReport.getINSTANCE(); + memoryReport.printPeriodically(config.getMemoryReportIntervalSec(), config.isIncludeThreadListInMemoryReport()); DevMode.setDevMode(config.isDevMode()); diff --git a/apps/oracle-node-app/src/main/java/bisq/oracle_node/bisq1_bridge/Bisq1BridgeService.java b/apps/oracle-node-app/src/main/java/bisq/oracle_node/bisq1_bridge/Bisq1BridgeService.java index a380876df5..9559bc9eac 100644 --- a/apps/oracle-node-app/src/main/java/bisq/oracle_node/bisq1_bridge/Bisq1BridgeService.java +++ b/apps/oracle-node-app/src/main/java/bisq/oracle_node/bisq1_bridge/Bisq1BridgeService.java @@ -107,6 +107,8 @@ public static Bisq1BridgeService.Config from(com.typesafe.config.Config config) @Nullable private Scheduler periodicRequestDoaDataScheduler, initialDelayScheduler; + @SuppressWarnings("FieldCanBeLocal") // Pin it so that it does not get GC'ed + private final MemoryReport memoryReport; public Bisq1BridgeService(Config config, NetworkService networkService, @@ -127,6 +129,7 @@ public Bisq1BridgeService(Config config, httpService = new Bisq1BridgeHttpService(httpServiceConfig, networkService); persistence = persistenceService.getOrCreatePersistence(this, DbSubDirectory.PRIVATE, persistableStore); + memoryReport = MemoryReport.getINSTANCE(); } @@ -219,11 +222,11 @@ public void onAuthorizedDataAdded(AuthorizedData authorizedData) { private void initialRepublish() { log.info("Start republishAuthorizedBondedRoles"); republishAuthorizedBondedRoles(); - MemoryReport.logReport(); + memoryReport.logReport(); log.info("Completed republishAuthorizedBondedRoles"); log.info("Start request and publish DaoData"); requestDaoData().join(); // takes about 6 minutes for 500 items - MemoryReport.logReport(); + memoryReport.logReport(); log.info("Completed request and publish DaoData"); periodicRequestDoaDataScheduler = Scheduler.run(this::periodicRepublish) .host(this) @@ -234,7 +237,7 @@ private void initialRepublish() { private void periodicRepublish() { log.info("periodicRequestDoaDataScheduler: Start requestDoaData"); requestDaoData().join(); - MemoryReport.logReport(); + memoryReport.logReport(); log.info("periodicRequestDoaDataScheduler: Completed requestDoaData"); } diff --git a/common/src/main/java/bisq/common/platform/MemoryReport.java b/common/src/main/java/bisq/common/platform/MemoryReport.java index 1ea3663e3e..57b7e0bcd9 100644 --- a/common/src/main/java/bisq/common/platform/MemoryReport.java +++ b/common/src/main/java/bisq/common/platform/MemoryReport.java @@ -22,6 +22,7 @@ import bisq.common.threading.ThreadProfiler; import bisq.common.timer.Scheduler; import bisq.common.util.StringUtils; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import java.util.Comparator; @@ -30,21 +31,27 @@ @Slf4j public class MemoryReport { - private static Scheduler scheduler; - private static boolean includeThreadListInMemoryReport; + @Getter + private static final MemoryReport INSTANCE = new MemoryReport(); - public static void printPeriodically(int memoryReportIntervalSec, boolean includeThreadListInMemoryReport) { - MemoryReport.includeThreadListInMemoryReport = includeThreadListInMemoryReport; + private Scheduler scheduler; + private boolean includeThreadListInMemoryReport; + + public MemoryReport() { + } + + public void printPeriodically(int memoryReportIntervalSec, boolean includeThreadListInMemoryReport) { + this.includeThreadListInMemoryReport = includeThreadListInMemoryReport; if (scheduler != null) { scheduler.stop(); } - scheduler = Scheduler.run(MemoryReport::logReport) + scheduler = Scheduler.run(this::logReport) .host(MemoryReport.class) .runnableName("logReport") .periodically(30, memoryReportIntervalSec, TimeUnit.SECONDS); } - public static void logReport() { + public void logReport() { Runtime runtime = Runtime.getRuntime(); long free = runtime.freeMemory(); long total = runtime.totalMemory();