Skip to content

Commit

Permalink
Merge pull request #2884 from HenrikJannsen/make-MemoryReport-singleton
Browse files Browse the repository at this point in the history
Make MemoryReport singleton and pin it to a field so that it does not…
  • Loading branch information
HenrikJannsen authored Sep 30, 2024
2 parents 99d633b + d0a754e commit 638b5c0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -127,6 +129,7 @@ public Bisq1BridgeService(Config config,
httpService = new Bisq1BridgeHttpService(httpServiceConfig, networkService);

persistence = persistenceService.getOrCreatePersistence(this, DbSubDirectory.PRIVATE, persistableStore);
memoryReport = MemoryReport.getINSTANCE();
}


Expand Down Expand Up @@ -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)
Expand All @@ -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");
}

Expand Down
19 changes: 13 additions & 6 deletions common/src/main/java/bisq/common/platform/MemoryReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit 638b5c0

Please sign in to comment.