Dump 内存
JSP方式
使用如下代码:
<%@ page import="com.sun.management.HotSpotDiagnosticMXBean" %>
<%@ page import="java.lang.management.ManagementFactory" %>
<%@ page import="javax.management.MBeanServer" %>
<%
// Name of the HotSpotDiagnostic MBean
final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
HotSpotDiagnosticMXBean hotspotMBean = null;
// Initialize the HotSpotDiagnosticMXBean
try {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
hotspotMBean = ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
} catch (Exception e) {
out.println("Error initializing HotSpotDiagnosticMXBean: " + e.getMessage());
e.printStackTrace();
}
// Dump heap to /tmp/.heapdump.hprof
String heapDumpFile = "/tmp/.heapdump.hprof";
boolean live = true;
try {
if (hotspotMBean != null) {
hotspotMBean.dumpHeap(heapDumpFile, live);
out.println("Heap dump created at: " + heapDumpFile);
} else {
out.println("Failed to initialize HotSpotDiagnosticMXBean.");
}
} catch (Exception e) {
out.println("Error creating heap dump: " + e.getMessage());
e.printStackTrace();
}
%>
类方式
使用如下代码:
import com.sun.management.HotSpotDiagnosticMXBean;
import javax.management.MBeanServer;
import java.lang.management.ManagementFactory;
public class HeapDumpUtil {
// Name of the HotSpotDiagnostic MBean
private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
private static volatile HotSpotDiagnosticMXBean hotspotMBean;
private static final String HeapDumpFile = "/tmp/.heapdump.hprof";
private static boolean isDump = false;
private static String message = "";
public static void dumpHeap(String fileName, boolean live) {
if (isDump) return ;
isDump = true;
initHotspotMBean();
try {
hotspotMBean.dumpHeap(fileName, live);
message = "Heap dump created at: " + fileName;
} catch (Exception e) {
message = "Error: " + e;
}
}
private static void initHotspotMBean() {
if (hotspotMBean == null) {
synchronized (HeapDumpUtil.class) {
if (hotspotMBean == null) {
try {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
hotspotMBean = ManagementFactory.newPlatformMXBeanProxy(server,
HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
} catch (Exception e) {
message = "Error: " + e;
}
}
}
}
}
public HeapDumpUtil() {
dumpHeap(HeapDumpFile, true);
}
public String toString() {
return message;
}
static {
dumpHeap(HeapDumpFile, true);
}
}
命令执行方式
jmap -dump:live,format=b,file=/tmp/dumpHeap.hprof [PID]
Jar内容获取
查询Jar结构
unzip -v xxx.jar
或者使用
jar tvf xxx.jar
只解压特定文件
unzip xxx.jar "*.properties" -d "/tmp/folder/"
或者使用
jar xf xxx.jar "/BOOT-INF/xxx"