In Java, is there a way to know that a StackOverflow error or OutOfMemory exception may happen soon?
The OutOfMemory exception might be an easier one to catch, if one is capable of getting memory usage statistics programmatically, and if one knows ahead of time how much memory needs to be used before the OutOfMemory exception is thrown. But are those values knowable?
For the StackOverflow error, is there a way to get recursion depth, and how does one know what value for recursion depth would cause the error to occur?
By knowing ahead of time whether these errors will happen, I feel I can recover the application more gracefully, instead of watching it crash.