Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it such a bad idea to capture OutOfMemoryError? [duplicate]

Possible Duplicate:
Catching java.lang.OutOfMemoryError

OutOfMemoryError are:

Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector

Java says:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.

This feels like hearing:

If you are drowning, be reasonable: you should not try to swim upwards to keep your head above water. Death is typically resulting from abnormal conditions.

Let's imagine a scenario where one is running a service. For some reason, another application on the same server is eating a lot of memory, causing an unexpected OOM in your service. Is it such a bad idea to try to reduce this service's memory consumption in order to remain available to user?

Or is there something more fundamental happening at the JVM level preventing the implementation of such a solution after the OOM has been thrown?

like image 557
Jérôme Verstrynge Avatar asked Sep 08 '11 16:09

Jérôme Verstrynge


People also ask

How do you deal with Outofmemory errors?

1) An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError.

Can we catch OutOfMemoryError?

As far as I understand, if we decide to catch it, the catch handler should not allocate any memory by itself. Otherwise OutOfMemoryError will be thrown again.

Can we handle error in catch block can we handle OutOfMemoryError in catch block if yes why we not do in general uses?

Don't Catch Throwable Errors are thrown by the JVM to indicate serious problems that are not intended to be handled by an application. Typical examples for that are the OutOfMemoryError or the StackOverflowError. Both are caused by situations that are outside of the control of the application and can't be handled.

Can the JVM recover from an OutOfMemoryError without a restart?

In other words if an OOME is thrown in an application server (jboss/websphere/..) do I have to restart it? No you don't have to restart. But it is probably wise to, especially if you don't have a good / automated way of checking that the service is running correctly. The JVM will recover just fine.


1 Answers

As you quoted

Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector

At that point your are screwed. That description implies that Java/the JVM can't get enough resources to operate, and if that is true, executing more Java code to fix the problem would itself be problematic.

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

A better solution is to do capacity planning and make sure

1) Your servers have enough memory to do their jobs
2) The services running on your servers perform within spec and don't consume more than a certain amount of resources.

like image 66
hvgotcodes Avatar answered Oct 11 '22 04:10

hvgotcodes