How to handle OutOfMemoryError in Java? [duplicate]

I know that the official Java answer is “Oh noes! Out of memories! I give in!”. This is all rather frustrating for anyone who has programmed in environments where running out of memory is not allowed to be a fatal error (for example, writing an OS, or writing apps for non-protected OSes).

The willingness to surrender is necessary – you can’t control every aspect of Java memory allocation, so you can’t guarantee that your program will succeed in low-memory conditions. But that doesn’t mean you must go down without a fight.

Before fighting, though, you could look for ways to avoid the need. Perhaps you can avoid Java serialization, and instead define your own data format which does not require significant memory allocation to create. Serialization allocates a lot of memory because it keeps a record of objects it has seen before, so that if they occur again it can reference them by number instead of outputting them again (which could lead to an infinite loop). But that’s because it needs to be general-purpose: depending on your data structure, you might be able to define some text/binary/XML/whatever representation which can just be written to a stream with very little need to store extra state. Or you might be able to arrange that any extra state you need is stored in the objects all along, not created at serialization time.

If your application does one operation which uses a lot of memory, but mostly uses much less, and especially if that operation is user-initiated, and if you can’t find a way to use less memory or make more memory available, then it might be worth catching OutOfMemory. You could recover by reporting to the user that the problem is too big, and inviting them to trim it down and try again. If they’ve just spend an hour setting up their problem, you do not want to just bail out of the program and lose everything – you want to give them a chance to do something about it. As long as the Error is caught way up the stack, the excess memory will be unreferenced by the time the Error is caught, giving the VM at least a chance to recover. Make sure you catch the error below your regular event-handling code (catching OutOfMemory in regular event handling can result in busy loops, because you try to display a dialog to the user, you’re still out of memory, and you catch another Error). Catch it only around the operation which you’ve identified as the memory-hog, so that OutOfMemoryErrors you can’t handle, that come from code other than the memory hog, are not caught.

Even in a non-interactive app, it might make sense to abandon the failed operation, but for the program itself to carry on running, processing further data. This is why web servers manage multiple processes such that if one page request fails for lack of memory, the server itself doesn’t fall over. As I said at the top, single-process Java apps can’t make any such guarantees, but they can at least be made a bit more robust than the default.

That said, your particular example (serialization) may not be a good candidate for this approach. In particular, the first thing the user might want to do on being told there’s a problem is save their work: but if it’s serialization which is failing, it may be impossible to save. That’s not what you want, so you might have to do some experiments and/or calculations, and manually restrict how many million items your program permits (based on how much memory it is running with), before the point where it tries to serialize.

This is more robust than trying to catch the Error and continue, but unfortunately it’s difficult to work out the exact bound, so you would probably have to err on the side of caution.

If the error is occurring during deserialization then you’re on much firmer ground: failing to load a file should not be a fatal error in an application if you can possibly avoid it. Catching the Error is more likely to be appropriate.

Whatever you do to handle lack of resources (including letting the Error take down the app), if you care about the consequences then it’s really important to test it thoroughly. The difficulty is that you never know exactly what point in your code the problem will occur, so there is usually a very large number of program states which need to be tested.

Leave a Comment