Check if there is enough memory before allocating byte array

后端 未结 3 1416
轻奢々
轻奢々 2020-12-21 02:34

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 av

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-21 03:01

    You may want to look at the FileChannel.map(FileChannel.MapMode, long, long) method. This allows mapping a file (think POSIX mmap) without filling the heap. The operating system will (hopefully successfully) take care of the memory for you.

提交回复
热议问题