Memory issues with InputStream in Java

后端 未结 4 1091
孤城傲影
孤城傲影 2021-01-21 19:31

I need to read a file into an array of Bytes.The entire file needs to be read into the array. The problem is I am getting an OutOfMemory Error since the file size is too large.

4条回答
  •  春和景丽
    2021-01-21 19:55

    You need to have far more free memory than the largest file you can have to use this approach. Given a machine with 24 GB costs less than £2K, this isn't as silly idea as it used to be. Actually the 2GB limit for a byte[] is more of a headache in some situations.

    However, the usual way to read an InputStream is to read a block of say 8KB at a time. This way you only need to have far more than 8KB free.

    BTW: You can use -mx1g instead of the option you are using.

提交回复
热议问题