Thread Dump Analyzer in Java
A Java Thread Dump Analyzer is a tool used to analyze the thread dump generated by a Java Virtual Machine (JVM) and to identify potential issues such as deadlocks, thread contention, high CPU utilization, and excessive blocking time.
Requirement :
Download thread dump analyzer : https://www.eclipse.org/mat/downloads.php
Simple java application to simulate heap : https://github.com/wahyueko22/rest-hallo-world/blob/master/src/main/java/com/hallo/restworld/RestControllerWolrd.java
Command :
java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:/tmp/ -jar -Xmx128m -Xms128m target/restworld-0.0.1-SNAPSHOT.jar
In Java, `+HeapDumpOnOutOfMemoryError` and `-HeapDumpOnOutOfMemoryError` are two command-line options that control whether or not the JVM should generate a heap dump when it encounters an out-of-memory error.
The `+HeapDumpOnOutOfMemoryError` option tells the JVM to generate a heap dump file in the event of an out-of-memory error. This option is available in JVMs running on Windows, macOS, and Linux operating systems.
On the other hand, the `-HeapDumpOnOutOfMemoryError` option also tells the JVM to generate a heap dump file when an out-of-memory error occurs, but it is only available in JVMs running on Linux and Solaris operating systems.
The code that causing heap out of memory:
ArrayList<RestControllerWolrd> list = new ArrayList<>();
try {
while (true) {
list.add(new RestControllerWolrd());
}
} catch (OutOfMemoryError e) {
System.err.println("Heap out of memory error occurred.");
}
download the memory anlyer from https://www.eclipse.org/mat/downloads.php
Apply HeapDumpOnOutOfMemoryError on wildfly
WildFly is a Java application server that can be configured to run in standalone mode or in domain mode.
In standalone mode, WildFly runs as a single server instance that hosts all of your applications. The standalone.conf file contains the configuration settings for the standalone mode.
In domain mode, WildFly runs as a managed domain that can host multiple server instances across different physical machines. The domain.conf file contains the configuration settings for the domain mode.
Both the standalone.conf and domain.conf files contain the JAVA_OPTS variable, which is used to set the command-line arguments for the JVM when starting WildFly. You can use this variable to set JVM options such as the -XX:HeapDumpPath and -XX:-HeapDumpOnOutOfMemoryError options that control heap dumps.
However, the standalone.conf and domain.conf files have different purposes, so you should be careful when making changes to these files. Changes made to the standalone.conf file only affect the standalone mode, while changes made to the domain.conf file only affect the domain mode.
If you need to make changes that affect both the standalone and domain modes, you can create a new file (such as common.conf) that contains the shared configuration settings and include it in both the standalone.conf and domain.conf files.
You can set both the -XX:HeapDumpPath and -XX:-HeapDumpOnOutOfMemoryError JVM options on WildFly by adding them to the JAVA_OPTS environment variable. Here are the steps to do so:
1. Open the WildFly configuration file (standalone.conf or domain.conf) in a text editor.
2. Search for the JAVA_OPTS variable in the file. This variable is used to set the command-line arguments for the JVM when starting WildFly.
3. Add the -XX:HeapDumpPath option to the JAVA_OPTS variable, followed by the path where you want to save the heap dump file. For example, if you want to save the heap dump file in the /tmp, you can add the following line to the JAVA_OPTS variable:
JAVA_OPTS=”$JAVA_OPTS -XX:HeapDumpPath=/tmp/”
4. Add the -XX:-HeapDumpOnOutOfMemoryError option to the JAVA_OPTS variable to enable heap dump on OutOfMemoryError. You can add the following line to the JAVA_OPTS variable:
JAVA_OPTS=”$JAVA_OPTS -XX:-HeapDumpOnOutOfMemoryError”
5. Save the configuration file and restart WildFly for the changes to take effect.
When WildFly starts up, it will use the JAVA_OPTS environment variable to set the -XX:HeapDumpPath and -XX:HeapDumpOnOutOfMemoryError options for the JVM. If a heap dump is triggered due to an OutOfMemoryError, the heap dump file will be saved in the directory specified by the -XX:HeapDumpPath option.
Github:
https://github.com/wahyueko22/rest-hallo-world
Source :