4

For logging anything in a catch block, we need text which is a String object but JVM has already run out of memory. Two possible reasons which I can think of are:

  1. OutOfMemory error signifies heap space and String object can be saved in String pool.
  2. OutOfMemory error signifies that very little space is left in contrast to heap is completely full and that very little space is available for storing the string to be logged.
3
  • One of those strange questions on the surface; but giving interesting food for thought ... Commented May 11, 2017 at 12:42
  • Not exactly identical to your question, but many of the answers provide the kind of insight you're looking for. Commented May 11, 2017 at 12:43
  • @Michael Good point here! Commented May 11, 2017 at 12:44

1 Answer 1

4

Very broad question, but one simple answer: when implementing a JVM, you probably wouldn't want to wait until you ran out of 100% of your memory.

Meaning: you might simply pull the "emergency break" when you are at 99.99% of your limit. Because then you know that you have that "tiny" reserve required to allow for a (somehow) coordinated "emergency shutdown".

Beyond that: this might be "less" about some message strings given to the exception ... keep in mind that the JVM also collects stack trace information; and attaches that to exceptions/errors. (imho) that is more expensive then making room for some message strings!

Sign up to request clarification or add additional context in comments.

3 Comments

Yes I shared the same thought in my 2nd reason. I wonder if we have it written somewhere so that we can be sure that this is the actual reason.
I think the question mentioned as close reason might have some insight there. In any case: this would be really implementation specific; as there are many different ways to handle that.
@GhostCat I think you're right that it depends on implementation. You might be lucky and be able to log an OOME no problem, or you might not. There's a JVM option OnOutOfMemoryError which lets you specify an executable to run in the case of an OOME. The very existence of that suggests to me that the most reliable way to handle this is to defer to some other program.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.