Skip to content

Commit

Permalink
netty直接内存监控
Browse files Browse the repository at this point in the history
  • Loading branch information
liuganghuan committed Jul 18, 2023
1 parent ab8bcb3 commit e74c2c3
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/test/java/MemoryMonitorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import io.netty.util.internal.PlatformDependent;

import java.lang.management.BufferPoolMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class MemoryMonitorTest {
public static void main(String[] args) {
Map<String, Long> map = new TreeMap<>();
map.put("direct_netty", PlatformDependent.usedDirectMemory());
map.put("heap", memoryMXBean.getHeapMemoryUsage().getUsed());
map.put("non_heap", memoryMXBean.getNonHeapMemoryUsage().getUsed());
for (BufferPoolMXBean bufferPool : bufferPoolMXBeans) {
map.put("buffer_pool_" + fixName(bufferPool.getName()), bufferPool.getMemoryUsed());
}
for (Map.Entry<String, Long> entry : map.entrySet()) {
System.out.println(String.format("%18s %s",entry.getKey(),entry.getValue()));
}
}

private static String fixName(String name) {
return name.replaceAll(" ", "_").replaceAll("'", "").replaceAll("-", "_");
}

private static MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
private static List<BufferPoolMXBean> bufferPoolMXBeans = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
}

0 comments on commit e74c2c3

Please sign in to comment.