So that concerns me. Even well tested, "pure" code...
# arrow
d
So that concerns me. Even well tested, "pure" code could throw an OutOfMemoryError
s
Fatal errors are fatal to the jvm. Nothing you can do at that point
d
But unwinding the stack may result in a garbage collection that allows the process to continue.
s
But the jvm is dead at this point, no?
d
No, it's just an unchecked throwable. There's nothing stopping, say, a servlet container from catching an OOME, logging it, and continuing
Tomcat, WebLogic, JBoss, they all do this
s
Can you link anything on this? Because they manage vms can spin a new jvm running your program.
The vm gets into a inconsistent state on OOM so even if you attempt to recover it's in an inconsistent state and weird things can happen
d
I can create a sample program to demonstrate
s
Ye, but a sample might not run into weird behaviors.
d
Copy code
import java.lang.StringBuilder

fun main() {
    try {
        causeOutOfMemoryError()
    } catch(e: OutOfMemoryError) {
        println("Out of Memory Error was thrown!")
    }
    println("Still alive!")
    println("Doing more stuff: " + causeOutOfMemoryError(2))
}

fun causeOutOfMemoryError(power: Int = Int.MAX_VALUE) : String {
    val s = StringBuilder("*")
    repeat(power) {
        s.append(s.toString())
    }
    return s.toString()
}
output:
Copy code
Out of Memory Error was thrown!
Still alive!
Doing more stuff: ****
True, but even in single-server mode containers can attempt to recover
s
I'll see if I can find some lecture to back up my argumentation
d
Right, I'm not suggesting it wouldn't cause weird behaviors - that seems self evident. It's just that an OOME isn't automatically fatal in JVM land.