Linux-jprofiler定位OOM的问题原因
Jprofiler定位OOM的问题原因
JProfiler是一款功能强大的Java开发分析工具,它可以快速的帮助用户分析出存在的错误,软件还可对需 要的显示类进行标记,包括了内存的分配情况和信息的视图等
官网:https://www.ej-technologies.com/products/jprofiler/overview.html
范例: 安装jprofiler工具定位OOM原因和源码问题的位置
#安装OpenJDK [root@centos8 ~]#dnf -y install java-1.8.0-openjdk-devel [root@centos8 ~]#cat HeapOom.java import java.util.ArrayList; import java.util.List; public class HeapOom { public static void main(String[] args) { List<byte[]> list =new ArrayList<byte[]>(); int i = 0; boolean flag = true; while(flag){ try{ i++; list.add(new byte[1024* 1024]);//每次增加一个1M大小的数组对象 Thread.sleep(1000); }catch(Throwable e){ e.printStackTrace(); flag = false; System.out.println("count="+i);//记录运行的次数 } } } } [root@centos8 ~]#javac HeapOom.java [root@centos8 ~]#java -cp . -Xms5m -Xmx10m -XX:+HeapDumpOnOutOfMemoryErrorHeapOom java.lang.OutOfMemoryError: Java heap space Dumping heap to java_pid96271.hprof ... Heap dump file created [8134070 bytes in 0.031 secs] java.lang.OutOfMemoryError: Java heap space at HeapOom.main(HeapOom.java:12) count=8 [root@centos8 ~]#cat HeapOom2.java import java. util. Random; public class HeapOom2 { public static void main(String[] args) { String str = "I am lao wang"; while (true){ str += str + new Random().nextInt(88888888); } } } [root@centos8 ~]#javac HeapOom2.java [root@centos8 ~]#java -cp . -Xms5m -Xmx10m -XX:+HeapDumpOnOutOfMemoryErrorHeapOom2 java.lang.OutOfMemoryError: Java heap space Dumping heap to java_pid96339.hprof ... 生成此文件传送到window桌面,用Jprofiler工具打开 Heap dump file created [4925877 bytes in 0.016 secs] Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3332) at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.jav a:124) at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:674) at java.lang.StringBuilder.append(StringBuilder.java:208) at HeapOom2.main(HeapOom2.java:6)
[root@centos8 ~]#cat HeapOom.java import java.util.ArrayList; import java.util.List; public class HeapOom { public static void main(String[] args) { List<byte[]> list =new ArrayList<byte[]>(); int i = 0; boolean flag = true; while(flag){ try{ i++; list.add(new byte[1024* 1024]);//每次增加一个1M大小的数组对象 Thread.sleep(1000); }catch(Throwable e){ e.printStackTrace(); flag = false; System.out.println("count="+i);//记录运行的次数 } } } }