import java.util.ArrayList;
import java.util.List;
public class GenarateHeapDump {
public static void main(String[] args) {
List<byte[]> list = new ArrayList<>();
int index = 1;
while (true) {
// 1MB each loop, 1 x 1024 x 1024 = 1048576
byte[] b = new byte[1048576];
list.add(b);
Runtime rt = Runtime.getRuntime();
System.out.printf("[%d] free memory: %s%n", index++, rt.freeMemory());
}
}
}
- Compile the program
javac GenerateHeapDump.java
- Create a directory to store heap dump files
I prefer to do it this way to keep my files isolated and avoid confusion.
mkdir dumpFiles
- Run the program
java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/username/dumpFiles Solution
The above command will generate a heap dump file in the directory /home/username/dumpFiles
which will have a name java_pid<pid>.hprof