Java中InputStream的内存问题 [英] Memory issues with InputStream in Java

查看:451
本文介绍了Java中InputStream的内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个文件读入一个字节数组中.整个文件都需要读入该数组中.问题是由于文件太大,我收到内存不足错误.增加-XmX似乎没有任何效果.这是代码片段:

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. Increasing -XmX does not seem to have any effect. Here is the code snippet :

InputStream in = new FileInputStream(file);
long length = file.length();                        
byte[] out = new byte[(int)length]; 
// Process the byte array

该问题在字节数组实例化期间发生.是否有较少的内存密集型解决方案来解决此问题?

The problem occurs during the byte array instantion. Is there a less memory intensive workaround to this issue?

推荐答案

与使用这种方法所需的最大文件相比,您需要的可用内存要多得多.考虑到24 GB的机器成本不到2000英镑,这并不像以前那么愚蠢.实际上,在某些情况下,byte []的2GB限制更让人头疼.

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.

但是,读取InputStream的通常方法是一次读取一个8KB的块.这样,您只需要拥有8KB以上的可用空间即可.

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.

顺便说一句:您可以使用-mx1g代替正在使用的选项.

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

这篇关于Java中InputStream的内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆