Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
MingzhenHan committed Oct 15, 2024
1 parent 310c8d7 commit 8bf6404
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ server.role=master
# slow query log
log.slow_query_threshold=1000

# jvm memory usage monitor
# jvm(in-heap) memory usage monitor, set 1 to disable it
memory_monitor.threshold=0.85
memory_monitor.period=2000
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public HugeGraphServer(String gremlinServerConf, String restServerConf)
System.setSecurityManager(securityManager);
}

// Start Memory Monitor Task
// Start (In-Heap) Memory Monitor
this.memoryMonitor = new MemoryMonitor(restServerConf);
this.memoryMonitor.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ public MemoryMonitor(String restServerConf) {
}

private void runMemoryDetect() {
double memoryUsagePercentage = getMemoryUsagePercentage();
double memoryUsagePercentage = getMemoryUsageRatio();

if (memoryUsagePercentage > MEMORY_MONITOR_THRESHOLD) {
LOG.warn("JVM memory usage is '{}', exceeding the threshold of '{}'.",
memoryUsagePercentage, MEMORY_MONITOR_THRESHOLD);
System.gc();
LOG.warn("Trigger System.gc()");

double doubleCheckUsage = getMemoryUsagePercentage();
double doubleCheckUsage = getMemoryUsageRatio();
if (doubleCheckUsage > MEMORY_MONITOR_THRESHOLD) {
LOG.warn("JVM memory usage is '{}', exceeding the threshold of '{}'.",
doubleCheckUsage, MEMORY_MONITOR_THRESHOLD);
Expand All @@ -64,9 +64,8 @@ private void runMemoryDetect() {
}
}

private double getMemoryUsagePercentage() {
MemoryUsage heapMemoryUsage =
ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
private double getMemoryUsageRatio() {
MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
return (double) heapMemoryUsage.getUsed() / heapMemoryUsage.getMax();
}

Expand All @@ -79,14 +78,12 @@ private Thread getHighestMemoryThread() {
Thread[] threads = new Thread[Thread.activeCount()];
Thread.enumerate(threads);
for (Thread thread : threads) {
if (thread.getState() != Thread.State.RUNNABLE ||
thread.getName() == null ||
if (thread.getState() != Thread.State.RUNNABLE || thread.getName() == null ||
!thread.getName().startsWith("grizzly-http-server-")) {
continue;
}

long threadMemory =
threadMXBean.getThreadAllocatedBytes(thread.getId());
long threadMemory = threadMXBean.getThreadAllocatedBytes(thread.getId());
if (threadMemory > highestMemory) {
highestMemory = threadMemory;
highestThread = thread;
Expand All @@ -99,14 +96,13 @@ private void interruptHighestMemoryThread() {
Thread targetThread = getHighestMemoryThread();
if (targetThread != null) {
targetThread.interrupt();
LOG.warn("Send interrupt to '{}' thread",
targetThread.getName());
LOG.warn("Send interrupt to '{}' thread", targetThread.getName());
}
}

public void start() {
if (MEMORY_MONITOR_THRESHOLD >= 1.0) {
LOG.info("Invalid parameter, MEMORY_MONITOR_THRESHOLD should less than 1.0.");
LOG.info("Invalid parameter, MEMORY_MONITOR_THRESHOLD should 1.0.");
return;
}
this.scheduler.scheduleAtFixedRate(this::runMemoryDetect, 0, MEMORY_MONITOR_DETECT_PERIOD,
Expand All @@ -119,6 +115,6 @@ public void stop() {
return;
}
this.scheduler.shutdownNow();
LOG.info("Memory monitoring stoped.");
LOG.info("Memory monitoring stopped.");
}
}

0 comments on commit 8bf6404

Please sign in to comment.