Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if there is enough memory before allocating byte array

Tags:

java

I need to load a file into memory. Before I do that I want to make sure that there is enough memory in my VM left. If not I would like to show an error message. I want to avoid the OutOfMemory exception.

Approach:

  1. Get filesize of my file
  2. Use Runtime.getRuntime().freeMemory()
  3. Check if it fits

Would this work or do you have any other suggestions?

like image 961
matthias Avatar asked Feb 11 '23 11:02

matthias


2 Answers

The problem with any "check first then do" strategy is that there may be changes between the "check" and the "do" that render the entire thing useless.

A "try then recover" strategy is almost always a better idea and, unfortunately, that means trying to allocate the memory and handling the exception. Even if you do the "check first" option, you should still code for the possibility that the allocation may fail.

A classic example of that would be checking that a file exists before opening it. If someone were to delete the file between your check and open, you'll get an exception regardless of the fact the file was there very recently.

Now I don't know why you have an aversion to catching the exception but I'd urge you to rethink it. Since Java relies heavily on them, they're generally accepted as a good way to do things, if you don't actually control what it is you're trying (such as opening files or allocating memory).


If, as it seems from your comments, you're worried about the out-of-memory affecting other threads, that shouldn't be the case if you try to allocate one big area for the file. If you only have 400M left and you ask for 600, your request will fail but you should still have that 400M left.

It's only if you nickle-and-dime your way up to the limit (say trying 600 separate 1M allocations) would other threads start to feel the pinch after you'd done about 400. And that would only happen if you dodn't release those 400 in a hurry.

So perhaps a possibility would be to work out how much space you need and make sure you allocate it in one hit. Either it'll work or it won't. If it doesn't, your other threads will be no worse off.

I suppose you could use your suggested method to try and make sure the allocation left a bit of space for the other threads (say 100M or 10% or something like that), if you're really concerned. But I'd just go ahead and try anyway. If your other threads can't do their work because of low memory, there's ample precedent to tell the user to provide more memory for the VM.

like image 185
paxdiablo Avatar answered Feb 13 '23 01:02

paxdiablo


Personally I would advice against loading a massive file directly into memory, rather try to load it in chunks or use some sort of temp file to store intermediate data.

like image 40
Low Flying Pelican Avatar answered Feb 13 '23 02:02

Low Flying Pelican