在Debian系統上排查Java內存泄漏可以按照以下步驟進行:
Runtime runtime = Runtime.getRuntime();
long totalMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
long maxMemory = runtime.maxMemory();
System.out.println("Total Memory: " + totalMemory);
System.out.println("Free Memory: " + freeMemory);
System.out.println("Max Memory: " + maxMemory);
top或htop查看Java進程的內存使用情況。jmap 命令生成堆轉儲:jmap -dump:live,format=b,file=heapdump.hprof <pid>
其中 <pid> 是Java進程的進程ID。Eclipse MAT (Memory Analyzer Tool):
VisualVM:
Arthas:
wget https://alibaba.github.io/arthas/arthas-1.7.3.tar.gz && tar -zxvf arthas-1.7.3.tar.gz./arthas -p <pid> 命令連接到Java進程。watch 命令監控對象創建,使用 trace 命令追蹤方法調用。java -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/path/to/gc.log YourMainClass
GCViewer 分析GC日志。java -Xmx4g -Xms4g -XX:MaxMetaspaceSize=21m YourMainClass
jstat 監控GC狀態:jstat -gcutil <pid> 1000 5
jstack 生成Java進程的線程轉儲文件,分析線程的狀態和調用棧情況。jconsole 進行可視化的監控和管理。