Why are there no subclasses of OutOfMemoryError?

As we all know, there are several reasons for OutOfMEmoryError (see first answer ). Why is there only one exception that covers all of these cases instead of the few fine-grained ones inherited from OutOfMEmoryError ?

UML

+6
source share
6 answers

I would expect, because you really can’t do anything when this happens: it doesn’t really matter WHY you are done since you are screwed. Additional information might be nice, but ...

I know that tomcat is trying to do this “out of memory parachute”, where they hold onto a piece of memory and try to free it, but I'm not sure how well it works.

+8
source

You only need to subclass the exception if applications need to catch and handle different cases differently. But you should not fish and try to fully recover from these cases ... therefore, the need should not arise.

... but yes, I would still like to have a more descriptive cause of death on me.

The exception message indicates which of the OOME subnets has occurred. If you complain that the messages are too short, the role of exception messages in Java does not fully explain the problem they are reporting. What are javadocs and other documentation for?


@ Thorbjørn presents an equally convincing argument. In principle, different sub-services are implementation specific. Providing them with some of the standard API risks that limit the implementation of the JVM to do things in suboptimal ways to meet API requirements. And this approach risks creating unnecessary application portability barriers when creating new subclasses for new implementation-specific tweaks.

(For example, the hypothetical UnableToCreateNativeThreadError 1) assumes that thread creation failed due to lack of memory and 2) the lack of memory is qualitatively different from the normal amount of memory. 2) applies to existing Oracle JVMs, but not to all JVMs. 1) may not even be true for existing Oracle JVM devices. Creating a theme may fail due to the limited OS limit on the number of native threads.)


If you're wondering why it's a bad idea to try and recover from OOME, see the following questions:

+7
source

The garbage collection process is intentionally very vaguely described to provide the greatest possible freedom for JVM developers.

Therefore, the classes you mentioned are not specified in the API, but only in the implementation.

If you rely on them, your program will crash when launched on the JVM without these subclasses of a particular provider, so you will not.

+7
source

IMO there is no definite answer to this question, and it all comes down to the design decisions made at that time. This question is very similar to something like “why the Date class is not immutable” or “why properties extend from HashTable”. As another poster noted, subclasses really don't matter, since you're screwed up anyway. In addition, descriptive error messages are good enough to start with troubleshooting measures.

+2
source

this is due to the fact that all 4 are fatal errors that cannot be recovered (except, possibly, from the heap space, but it will still remain near the edge of the point of failure)

+1
source

Mostly because computing something smart would require allocating memory at some point. So you should trhrow OutOfMemoryException without doing any more calculations.

This is not a big deal because your program is already confused. The most you can do is return an error message to System.exit(ERROR_LEVEL); but you can’t even write a log because it will require allocating memory or using memory that might be screwed.

+1
source

Source: https://habr.com/ru/post/889821/


All Articles