2011-12-28 7 views
1

次のコードを使用中にヒープエラーが発生しました。誰かが私がここで間違っていることを教えてもらえますか?デバッグのバイト配列のファイルを読み込んでいるときにJavaヒープスペースエラーが発生しました

私は長さのtaht値は問題はあなたが配分されたバイト配列が大きすぎると、それはヒープ領域を使用することである709582875

In main function 

File file = new File(fileLocation+fileName); 
if(file.exists()){ 
s3Client.upload(bucketName,fileName,getBytesFromFile(file)); 
} 


// Returns the contents of the file in a byte array. 
public static byte[] getBytesFromFile(File file) throws IOException { 
InputStream is = new FileInputStream(file); 

// Get the size of the file 
long length = file.length(); 

// You cannot create an array using a long type. 
// It needs to be an int type. 
// Before converting to an int type, check 
// to ensure that file is not larger than Integer.MAX_VALUE. 
if (length > Integer.MAX_VALUE) { 
// File is too large 
log.debug("file is too large"+length); 
System.out.println("file is too large"+length); 
} 

if (length < Integer.MIN_VALUE || length > Integer.MAX_VALUE) { 
throw new IOException 
(length + " cannot be cast to int without changing its value."); 
} 

// return "test".getBytes(); 
// Create the byte array to hold the data 

try{ 
byte[] bytes = new byte[(int)length]; 
} 
catch(OutOfMemoryError e){ System.out.println(e.getStackTrace().toString());} 

// Read in the bytes 
int offset = 0; 
int numRead = 0; 
while (offset < bytes.length 
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 
offset += numRead; 
} 

// Ensure all the bytes have been read in 
if (offset < bytes.length) { 
throw new IOException("Could not completely read file "+file.getName()); 
} 

// Close the input stream and return bytes 
is.close(); 
return bytes; 
} 
+0

ファイル全体を一度にすべてメモリに読み込まないでください。あなたはチャンクでそれを行う必要があります。 – adatapost

+0

小さなファイルで試してもうまく動作していることを確認しましたか? – kosa

+0

'length

答えて

2

でご覧ください。

-Xmsおよび-Xmxオプションを指定してプログラムを実行すると、プログラムを実行するためにJava仮想マシンが使用する最小および最大のヒープスペースを指定できます。

しかし、ファイル全体をバイト配列に読み込んで処理しないことをお勧めします。その一部を小さなバイト配列に読み込み、その部分を処理して次の部分に進むことができます。この方法では、ヒープスペースを少なくします。

1

Java仮想マシン(JVM)によって割り当てられたヒープサイズを増やすよう 何かしよう:たとえば

java -Xms<initial heap size> -Xmx<maximum heap size> 


java -Xms64m -Xmx256m HelloWorld

2

あなたは(677メガバイト程度)709582875のバイトを消費しています現時点では、tryブロック内のバイト配列が割り当てられます。これは、従来のパーソナルコンピューティング標準では非常に大きく、JVMのメモリの大部分(すべてではないにせよ)がデフォルト設定で開始されます。

一部の情報デフォルトでJVMのメモリ設定はhere

0

一部のJVMのチューニングを必要とする のjava -Xms256m -Xmx1024m

1

見つけることができますDONOTような巨大なバイト[]配列を作成します。あなたのヒープはメモリ不足になる可能性があります。このような大きなファイルに対して、ファイル長のbyte []配列を作成することは悪い考えです。 小さなバイト配列を作成し、チャンク単位でファイルを読み込みます。

0

ファイル全体を一度にバイト[]として読む特別な理由はありますか? ByteBufferにマップされたメモリを使用することはできますか?これは、ファイルのサイズに関係なく、ほとんどヒープを使用しないためです。

関連する問題