Catching java.lang.OutOfMemoryError?

前端 未结 14 1452
Happy的楠姐
Happy的楠姐 2020-11-22 06:01

Documentation for java.lang.Error says:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application

相关标签:
14条回答
  • 2020-11-22 06:48

    For the question 2 I already see the solution I would suggest, by BalusC.

    1. Is there any real word scenarios when catching java.lang.OutOfMemoryError may be a good idea?

    I think I just came across a good example. When awt application is dispatching messages the uncatched OutOfMemoryError is displayed on stderr and the processing of the current message is stopped. But the application keeps running! User may still issue other commands unaware of the serious problems happening behind the scene. Especially when he cannot or does not observe the standard error. So catching oom exception and providing (or at least suggesting) application restart is something desired.

    0 讨论(0)
  • 2020-11-22 06:59

    You can catch anything under Throwable, generally speaking you should only catch subclasses of Exception excluding RuntimeException (though a large portion of developers also catch RuntimeException... but that was never the intent of the language designers).

    If you were to catch OutOfMemoryError what on earth would you do? The VM is out of memory, basically all you can do is exit. You probably cannot even open a dialog box to tell them you are out of memory since that would take memory :-)

    The VM throws an OutOfMemoryError when it is truly out of memory (indeed all Errors should indicate unrecoverable situations) and there should really be nothing you can do to deal with it.

    The things to do are find out why you are running out of memory (use a profiler, like the one in NetBeans) and make sure you don't have memory leaks. If you don't have memory leaks then increase the memory that you allocate to the VM.

    0 讨论(0)
  • 2020-11-22 07:00

    Yes, the real question is "what are you going to do in the exception handler?" For almost anything useful, you'll allocate more memory. If you'd like to do some diagnostic work when an OutOfMemoryError occurs, you can use the -XX:OnOutOfMemoryError=<cmd> hook supplied by the HotSpot VM. It will execute your command(s) when an OutOfMemoryError occurs, and you can do something useful outside of Java's heap. You really want to keep the application from running out of memory in the first place, so figuring out why it happens is the first step. Then you can increase the heap size of the MaxPermSize as appropriate. Here are some other useful HotSpot hooks:

    -XX:+PrintCommandLineFlags
    -XX:+PrintConcurrentLocks
    -XX:+PrintClassHistogram
    

    See the full list here

    0 讨论(0)
  • 2020-11-22 07:01

    There are definitely scenarios where catching an OOME makes sense. IDEA catches them and pops up a dialog to let you change the startup memory settings (and then exits when you are done). An application server might catch and report them. The key to doing this is to do it at a high level on the dispatch so that you have a reasonable chance of having a bunch of resources freed up at the point where you are catching the exception.

    Besides the IDEA scenario above, in general the catching should be of Throwable, not just OOM specifically, and should be done in a context where at least the thread will be terminated shortly.

    Of course most times memory is starved and the situation is not recoverable, but there are ways that it makes sense.

    0 讨论(0)
  • 2020-11-22 07:03

    I just have a scenario where catching an OutOfMemoryError seems to make sense and seems to work.

    Scenario: in an Android App, I want to display multiple bitmaps in highest possible resolution, and I want to be able to zoom them fluently.

    Because of fluent zooming, I want to have the bitmaps in memory. However, Android has limitations in memory which are device dependent and which are hard to control.

    In this situation, there may be OutOfMemoryError while reading the bitmap. Here, it helps if I catch it and then continue with lower resolution.

    0 讨论(0)
  • 2020-11-22 07:05

    I have an application that needs to recover from OutOfMemoryError failures, and in single-threaded programs it always works, but sometimes doesn't in multi-threaded programs. The application is an automated Java testing tool that executes generated test sequences to the maximum possible depth on test classes. Now, the UI must be stable, but the test engine can run out of memory while growing the tree of test cases. I handle this by the following kind of code idiom in the test engine:

    boolean isOutOfMemory = false;  // flag used for reporting
    try {
       SomeType largeVar;
       // Main loop that allocates more and more to largeVar
       // may terminate OK, or raise OutOfMemoryError
    }
    catch (OutOfMemoryError ex) {
       // largeVar is now out of scope, so is garbage
       System.gc();                // clean up largeVar data
       isOutOfMemory = true;       // flag available for use
    }
    // program tests flag to report recovery
    

    This works every time in single-threaded applications. But I recently put my test engine into a separate worker-thread from the UI. Now, the out of memory may occur arbitrarily in either thread, and it is not clear to me how to catch it.

    For example, I had the OOME occur while the frames of an animated GIF in my UI were being cycled by a proprietary thread that is created behind-the-scenes by a Swing class that is out of my control. I had thought that I had allocated all the resources needed in advance, but clearly the animator is allocating memory every time it fetches the next image. If anyone has an idea about how to handle OOMEs raised in any thread, I would love to hear.

    0 讨论(0)
提交回复
热议问题