Skip to content

Commit

Permalink
[type:refactor] optimize the performance of MemoryLimitCalculator#max…
Browse files Browse the repository at this point in the history
…Available (#3316)

* [type:refactor] optimize the performance of MemoryLimitCalculator#maxAvailable

* fix bug

* fix bug
  • Loading branch information
loongs-zhang authored Apr 25, 2022
1 parent 86302d4 commit 55cacb0
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* {@link javax.management.MXBean} technology is used to calculate the memory
Expand All @@ -32,14 +35,30 @@ public class MemoryLimitCalculator {

private static final MemoryMXBean MX_BEAN = ManagementFactory.getMemoryMXBean();

private static volatile long maxAvailable;

private static final ScheduledExecutorService SCHEDULER = Executors.newSingleThreadScheduledExecutor();

static {
// immediately refresh when this class is loaded to prevent maxAvailable from being 0
refresh();
// check every 50 ms to improve performance
SCHEDULER.scheduleWithFixedDelay(MemoryLimitCalculator::refresh, 50, 50, TimeUnit.MILLISECONDS);
Runtime.getRuntime().addShutdownHook(new Thread(SCHEDULER::shutdown));
}

private static void refresh() {
final MemoryUsage usage = MX_BEAN.getHeapMemoryUsage();
maxAvailable = usage.getCommitted();
}

/**
* Get the maximum available memory of the current JVM.
*
* @return maximum available memory
*/
public static long maxAvailable() {
final MemoryUsage usage = MX_BEAN.getHeapMemoryUsage();
return usage.getCommitted();
return maxAvailable;
}

/**
Expand Down

0 comments on commit 55cacb0

Please sign in to comment.