mercredi 27 juillet 2016

Check if there is enough memory before allocating byte array

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?

Aucun commentaire:

Enregistrer un commentaire