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.
trincot
  • 317,000
  • 35
  • 244
  • 286
Sandeep
  • 51
  • 2

1 Answers1

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!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 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. – Sandeep May 11 '17 at 12:44
  • 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 May 11 '17 at 12:45
  • @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. – Michael May 11 '17 at 12:47