Is it such a bad idea to capture an OutOfMemoryError?

Possible duplicate:
Capture java.lang.OutOfMemoryError

OutOfMemoryError :

It is thrown when the Java virtual machine cannot allocate an object because it has lost memory and there can be no more garbage collector

Java says:

A bug is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most of these errors are abnormal conditions.

It sounds like a rumor:

If you are drowning, be reasonable: you should not try to swim up to keep your head above the water. Death usually arises from abnormal conditions.

Imagine a scenario in which a service is running. For some reason, another application on the same server contains a lot of memory, which leads to an unexpected OOM in your service. Is it really a bad idea to try to reduce the consumption of this auxiliary memory in order to remain accessible to the user?

Or is there something more fundamental at the JVM level that prevents the implementation of such a solution after the OOM has been dropped?

+7
source share
5 answers

The problem here is that OOM is a mistake that should not occur under normal circumstances. By catching it and trying to free memory, you are probably hiding some kind of leak or inadvertent behavior elsewhere.

If you get OOM, perhaps because you haven't configured the JVM to use more memory.

0
source

As you indicated

It is thrown when the Java virtual machine cannot allocate an object because it has lost memory and there can be no more garbage collector

At this moment you are screwed. This description implies that Java / JVM cannot get enough resources to work, and if true, executing more Java code to fix the problem will be problematic in itself.

A good analogy is that your car has run out of gas, and you want to fix it by clicking on the accelerator.

The best solution is capacity planning and make sure that

1) Your servers have enough memory to perform their tasks
2) Services running on your servers run within the specification and do not consume more than a certain amount of resources.

+3
source

It is a bad idea. First of all, because OutOfMemoryError is Error not a Exception - Error "indicates serious problems that a reasonable application should not try to catch."

Instead, analyze the possible scenarios and apply corrections based on the information provided in this PDF

-one
source

It's useless. Since the error is currently caught, you can be as short as a bunch of memory that any new creation of an object can cause a new OOME. Having such a small maneuver in a trick means that the only action you can take is to exit.

Test this snippet and tell me your output is:

  import java.util.LinkedList; import java.util.List; public class OOMErrorTest { public static void main(String[] args) { List<Long> ll = new LinkedList<Long>(); try { long l = 0; while(true){ ll.add(new Long(l++)); } } catch(OutOfMemoryError oome){ System.out.println("Error catched!!"); System.out.println("size:" +ll.size()); } System.out.println("Test finished"); } } 

But, strictly speaking, yes, it is exciting.

-one
source

You really can't recover from OOM.

There is one reason, however, to try to catch something - what the "catch" fraction told me when we did just that in a project in which I no longer work - is one of the reasons: try to register what happened. Because if you do not know what happened, it is difficult to understand how to restore and run the software.

There is also a big “but” for this reason: it will not work normally.

Errors usually cannot be fixed from your working code, so there is such a difference.

-one
source

All Articles