0

I had run a jvm process, one day it encountered the outofmemory exception. In the log I have found many outofmemory errors::

-bash-3.00# grep OutOfMemoryError AdminServer.log00399
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
####<2013-6-28 05:09:27 pm CST> <Error> <Server> <kfmsapp2> <AdminServer> <DynamicListenThread[Default[3]]> <<WLS Kernel>> <> <>

<1372410567863>

It also generated a heapdump file, I found it was a big object from a thread after analysing by using mat, I just know it's a hibernate common object org.hibernate.engine.PersistenceContext, but I don't know where it come from.

I want to write a small program, and it will generate a threaddump file automatically when the outofmemory error occured.

My question is: what time of the program executing is best to take a thread dump exactly? Is it on the first outofmemory error appear in the log file?

1ac0
  • 2,875
  • 3
  • 33
  • 47
tdy218
  • 53
  • 1
  • 7

1 Answers1

0

I'd probably try to use an UncaughtExceptionHandler:

public static void main(String[] args) {
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
            // print it ...
        }
    });

            // ... continuing
}

For printing, see here or here.

Community
  • 1
  • 1
Xie
  • 374
  • 1
  • 8