Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: OutOfMemoryError Related JVM Arguments

  1. #1
    Member
    Join Date
    Nov 2017
    Location
    USA
    Posts
    147
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default OutOfMemoryError Related JVM Arguments

    JVM has provided helpful arguments to deal with OutOfMemoryError. In this article we would like to highlight those JVM arguments. It might come handy for you when you are troubleshooting OutOfMemoryError. Those JVM arguments are:

    1. -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath
    2. -XX:OnOutOfMemoryError
    3. -XX:+ExitOnOutOfMemoryError
    4. -XX:+CrashOnOutOfMemoryError

    Let’s discuss these JVM arguments in detail in this article.

    1. -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath
    Heap dump is basically a snapshot of memory. It contains details about objects that present in memory, actual data that is present within those objects, references originating of those objects. Heap dump is a vital artifact to troubleshoot memory problems.

    In order to diagnose OutOfMemoryError or any memory related problem, one would have to capture heap dump right at the moment or few moments before the application starts to experience OutOfMemoryError. It’s hard to do capture heap dump at the right moment manually because we will not know when OutOfMemoryError is going to be thrown. However, capturing heap dumps can be automated by passing following JVM arguments when you launch the application in the command line:

     -XX:+HeapDumpOnOutOfMemoryError and -XX:HeapDumpPath={HEAP-DUMP-FILE-PATH}

    Example

     -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/crashes/my-heap-dump.hprof

    When you pass these two JVM arguments, heap dumps will be automatically captured and written to a specified file path, when OutOfMemoryError is thrown.

    Once heap dumps are captured, you can use tools like HeapHero, Eclipse MAT to analyze heap dumps.

    2. -XX:OnOutOfMemoryError
    You can configure JVM to invoke any script when OutOfMemoryError is thrown. Most of the time, OutOfMemoryError doesn’t crash the application. However, it’s better to restart the application, once OutOfMemoryError happens. Because OutOfMemoryError can potentially leave application in an unstable state. Requests served from an unstable application instance can lead to an erroneous result.

    Example:
     -XX:OnOutOfMemoryError=/scripts/restart-myapp.sh
    When you pass this argument, JVM will invoke “/scripts/restart-myapp.sh” script whenever OutOfMemoryError is thrown. In this script, you can write code to restart your application gracefully.

    3. -XX:+CrashOnOutOfMemoryError
    When you pass this argument JVM will exit right when it OutOfMemoryError is thrown. Besides exiting, JVM produces text and binary crash files (if core files are enabled). But personally, I wouldn’t prefer configuring this argument, because we should always aim to achieve graceful exit. Abrupt exit can/will jeopardize transactions that are in motion.

    I ran an application which generates OutOfMemoryError with this ‘-XX:+CrashOnOutOfMemoryError’ argument. I could see JVM exiting immediately when OutOfMemoryError was thrown. Below was the message in the standard output stream:

    Aborting due to java.lang.OutOfMemoryError: GC overhead limit exceeded
    # A fatal error has been detected by the Java Runtime Environment:
    #
    #  Internal Error (debug.cpp:308), pid=26064, tid=0x0000000000004f4c
    #  fatal error: OutOfMemory encountered: GC overhead limit exceeded
    #
    # JRE version: Java(TM) SE Runtime Environment (8.0_181-b13) (build 1.8.0_181-b13)
    # Java VM: Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode windows-amd64 compressed oops)
    # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
    #
    # An error report file with more information is saved as:
    # C:\workspace\tier1app-svn\trunk\buggyapp\hs_err_pid26064.log
    #
    # If you would like to submit a bug report, please visit:
    #   http://bugreport.java.com/bugreport/crash.jsp
    From the message, you could see hs_err_pid file to be generated in ‘C:\workspace\tier1app-svn\trunk\buggyapp\hs_err_pid26064.log’. hs_err_pid file contains information about the crash. You can use tools like fastThread to analyze hs_err_pid file. But most of the time information present in hs_err_pid is very basic. It’s not sufficient enough to troubleshoot OutOfMemoryError.

    4. -XX:+ExitOnOutOfMemoryError
    When you pass this argument, JVM will exit right when OutOfMemoryError is thrown. You may pass this argument if you would like to terminate the application. But personally, I wouldn’t prefer configuring this argument, because we should always aim to achieve a graceful exit. Abrupt exit can/will jeopardize transactions that are in motion.

    I ran the same memory leak program with this ‘-XX:+ExitOnOutOfMemoryError’ JVM argument. Unlike ‘-XX:+CrashOnOutOfMemoryError’, this JVM argument did not generate any text/binary file. JVM just exited.

  2. #2
    Junior Member
    Join Date
    Oct 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: OutOfMemoryError Related JVM Arguments

    Hello, thanks for sharing this information with us.

Similar Threads

  1. Automating – OutOfMemoryError Troubleshooting
    By Ram Lakshmanan in forum Java Programming Tutorials
    Replies: 0
    Last Post: June 17th, 2019, 12:13 AM
  2. How to diagnose outofmemoryerror in Android?
    By Ram Lakshmanan in forum Java Programming Tutorials
    Replies: 0
    Last Post: June 20th, 2018, 12:51 AM
  3. Java Class Excercise (Related to Strings + Cmd Line Arguments)
    By Wwong3333 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 19th, 2012, 09:22 PM
  4. OutOfMemoryError caused in a loop at new BufferedImage
    By mame in forum AWT / Java Swing
    Replies: 1
    Last Post: January 28th, 2011, 06:37 PM
  5. OutOfMemoryError (Java heap space)
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2009, 11:56 AM