java内存溢出时自动生成dump文件

java内存溢出时自动生成dump文件

摘要

java内存溢出时自动生成dump文件

模拟内存溢出代码

package testmain;

import java.util.ArrayList;

public class App {

    public static void main(String[] args) throws Exception {

        ArrayList<String> a = new ArrayList<String>();
        while(true) {
            a.add("asdfasdjf;lasdkjfl;SJF;LAJFDL;ASJDFL;ASJDFL;AJSDFL;JSLFJSDL;FJS;LFJA;LSDKJ;ksdjflsjdf;lasjdlfkj");
        }

    }
}

java启动参数

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d://dump

注意文件夹要事先准备好

备注pom打包插件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>testmain</groupId>
  <artifactId>testmain</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testmain</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>


  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <plugins>

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <appendAssemblyId>false</appendAssemblyId>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>testmain.App</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>assembly</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>utf8</encoding>
        </configuration>
      </plugin>


    </plugins>
  </build>


</project>

打包命令是 mvn package

打开堆快照文件

这里使用 jvisualvm.exe
位置 “C:\Program Files\Java\jdk1.8.0_91\bin\jvisualvm.exe”
双击打开,就可以载入java_pid4396.hprof 文件了

linux下错误备注

HeapDumpPath可以指定一个文件或者文件夹
如果是文件夹必须/结尾且文件夹必须已经存在.
如果是文件,文件必须不存在且所在文件夹必须已经存在.

root@aliyun:~/test# java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="/root/dump/"  -jar  testmain-0.0.1-SNAPSHOT.jar
java.lang.OutOfMemoryError: Java heap space
Dumping heap to /root/dump/ ...
Unable to create /root/dump/: 是一个目录
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3210)
    at java.util.Arrays.copyOf(Arrays.java:3181)
    at java.util.ArrayList.grow(ArrayList.java:261)
    at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235)
    at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
    at java.util.ArrayList.add(ArrayList.java:458)
    at testmain.App.main(App.java:11)