内存映象文件导出的2种方式
- 内存溢出自动导出
- -XX:+HeapDumpOnOutOfMemoryError
- -XX:HeapDumpPath=./
- 使用jmap命令手动导出
内存映象文件导出示例
自动导出
@RestController
public class DemoController {
List<User> userList = new ArrayList<>();
List<Class<?>> classList = new ArrayList<>();
/**
* -Xmx32M -Xms32M
* -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./
* @return
*/
@GetMapping("/heap")
public String heap() {
int i = 0;
while (true) {
userList.add(new User(i++, UUID.randomUUID().toString()));
}
}
}
输出:
java.lang.OutOfMemoryError: Java heap space
Dumping heap to ./\java_pid17776.hprof ...
Heap dump file created [44356196 bytes in 0.272 secs]
Exception in thread "http-nio-8088-exec-1" java.lang.OutOfMemoryError: Java heap space
jmap手动导出
PS C:\Users\LiXinlei\Desktop> jmap -dump:format=b,file=heap.hprof 17452
Dumping heap to C:\Users\LiXinlei\Desktop\heap.hprof ...
Heap dump file created