3

I am trying to analyze an application that again and again dies with a heap out-of-memory.

The application is started with:

export JAVA_OPTS="-Xms256M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/opt/our_app_here/heapdump.dmp"

Before the application dies completely I see multiple entries like this in the log:

2021-01-11 07:17:05,039 [ERROR] [User=xxxxx] [class-name-here] Exception occured: java.lang.OutOfMemoryError: Java heap space

These errors happen in a try-catch where they are obviously caught and logged. Amazingly the application continues but - not too surprisingly - crashes shortly after.

Why does the JVM not write any heap-dump in that out-of-memory situation? I thought the "HeapDumpOnOutOfMemoryError" option was meant to trigger exactly that. Such a heap-dump would be most helpful if not essential to be able to figure out where our memory leak sits.

This is using Java 8 (AdooptOpenJDK 8u252) on SLES 12 and there is definitely enough diskspace left for the heap-dump!

mmo
  • 3,897
  • 11
  • 42
  • 63
  • 2
    Just as a sanity check, the user which runs the java process does have write permissions for that path right? Also it's a `HeapDumpPath` should point to a directory, not a file – JensV Jan 11 '21 at 11:38
  • Umm... why would you try to catch an OutOfMemoryError? Doesn't that essentially hide the fact that it occured? This seems to lead to undefined behaviour. Related: https://stackoverflow.com/q/2679330/2232127 – JensV Jan 11 '21 at 11:42
  • @JensV The code is not explicitly catching OutOfMemoryError's. Rather it's a try-catch in the main-loop of the application that tries to catch all Exceptions (and logs them!) to keep the application running in case of unexpected errors, null-pointers, etc. I just happens to also catch these OoM's. Probably we should explicitly NOT catch these (or rethrow them) since catching OoM's (at least heap overflows) does indeed not make much sense. – mmo Jan 11 '21 at 13:39
  • @Jens thanks for pointing out the fact that HeapDumpPath is considered as path. I actually copied that from an example (where it also included the filename). But I'll definitely change that. That might actually be the reason why we get no heap-dump. We'll see! – mmo Jan 11 '21 at 13:41
  • 2
    It's fine to have a catch-all for exceptions usually. But it should catch `Exception` and not `Throwable` or `Error`. Semantically, `Error` is not recoverable and you might run into very weird behaviour of the JVM when catching OOM Errors and trying to recover. (This is the general case, mileage may vary if you know the details on how to recover from specific stuff). I'm not convinced that a heap-dump is performed when the error is caught since you technically attempt to recover from it. It may finally crash because the jvm is in a broken state when you catch the error repeatedly. – JensV Jan 11 '21 at 13:45
  • Agreed re. Exception vs. Throwable. BTW: I googled re. HeapDumpPath and found the Oracle Java 8 docs: -XX:HeapDumpPath=path Sets the path *and file name* for writing the heap dump provided by the heap profiler (HPROF) when the -XX:+HeapDumpOnOutOfMemoryError option is set. By default, the file is created in the current working directory, and it is named java_pidpid.hprof where pid is the identifier of the process. -XX:HeapDumpPath=./java_pid%p.hprof The following example shows how to set the heap dump file to C:/log/java/java_heapdump.log: -XX:HeapDumpPath=C:/log/java/java_heapdump.log – mmo Jan 11 '21 at 13:54
  • Fair enough, I only used https://openjdk.java.net/groups/hotspot/docs/RuntimeOverview.html as a reference since you're using openjdk – JensV Jan 11 '21 at 13:56
  • ... which brings me back to my original question: why is there no heap dump triggered/written? – mmo Jan 11 '21 at 13:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227157/discussion-between-jensv-and-mmo). – JensV Jan 11 '21 at 13:57
  • From my own testing this seems to be a configuration problem. Your export line doesn't have a closing quote. If that's a typo, this might be of help: https://stackoverflow.com/q/28327620/2232127 otherwise, we need to know where the export line is specified and how your application is started – JensV Jan 11 '21 at 14:32
  • Sorry - these missing quote stemmed from removing the trailer of the config line before appending it here. We have more options there and there definitely ARE closing quotes on the actual command line. I fixed the line in the question. – mmo Jan 11 '21 at 16:38

0 Answers0