Chris
Chris

Reputation: 820

JVM: Unsafe to continue after failed memory allocation?

I have a process that uses a library to do a time and memory consuming step that sometimes takes too much memory and throws an allocation failure on a large request. In this case, it's reasonable to accept the failure and continue processing, so my solution is to null my reference to that tool and create a new one for continued processing on following input.

Other folks in my group dispute this strategy saying that once memory has become so tight in the JVM that allocation fails, the JVM is in a suspect state: no real proof, just FUD. On the other hand, I can only argue inductively that l haven't seen ill effects yet (I haven't seen any black swans yet).

Q: Is it absolutely unsafe, and verboten to continue with a JVM that has thrown a memory allocation failure?

Upvotes: 3

Views: 474

Answers (2)

stevevls
stevevls

Reputation: 10843

No, it's not absolutely unsafe. Though it's obviously much better to avoid it if you can because who knows what sort of indeterminate state your data structures will be in after you start failing to allocate memory.

There are many applications (especially webapps) that will survive memory allocation failures very well. However, whether it's safe or not depends to a large degree on the application. Code that is constantly updating shared resources will degrade worse than, to use an example again, webapps that have a large degree of separation between different tasks with typically little in the way of shared resources.

The main thing is that you evaluate your application carefully and have a good idea about the pitfalls you may run into. That, and try to restart the JVM as soon as possible after the failure. ;)

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533660

When you run out of memory, other threads can also run out of memory and they may not handle this so gracefully. You may find you can continue most of the time, but it is better not to run out of memory in the first place.

Upvotes: 1

Related Questions