简体   繁体   中英

Is it wise to catch OutofMemoryError in run() of thread?

I suspect in my application that an outofmemoryerror is causing aa run() to exit, however because there were no logs, the error wasn't visible.

What should i do in this case?

You should run your JVM with this flag: -XX:+HeapDumpOnOutOfMemoryError to see whats happening inside the JVM while the OOM happens. This will write a HPROF file, which you can analyze with a profiler, Eclipse MAT is a good one for this. Use -XX:HeapDumpPath=/tmp to configure the path where to write the HPROF to.

I do not see a reason as why you can not do that as parent and child threads all are spawned in the same JVM and they share the same heap memory. There are rare requirements to catch OOM errors though as explained here

I don't know about others, but never (IMHO) catch or throws anything that extends Error . The followig statement from the javadoc states:

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur .

An Error is printed with System.err and if you want to avoid OutofMemoryException , increase your heapsace instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM