我試圖從服務器向多個客戶端發送大塊文件。當我嘗試發送大小爲700mb的文件時,它顯示「OutOfMemory java heap space」錯誤。我正在使用Netbeans 7.1.2版本。 我也試過VMoption的屬性。但仍然會發生同樣的錯誤。我認爲讀取整個文件存在一些問題。以下代碼正在工作達300MB。請給我一些建議。內存不足java堆空間
在此先感謝
public class SplitFile {
static int fileid = 0 ;
public static DataUnit[] getUpdatableDataCode(File fileName) throws FileNotFoundException, IOException{
int i = 0;
DataUnit[] chunks = new DataUnit[UAProtocolServer.singletonServer.cloudhosts.length];
FileInputStream fis;
long Chunk_Size = (fileName.length())/chunks.length;
int cursor = 0;
long fileSize = (long) fileName.length();
int nChunks = 0, read = 0;long readLength = Chunk_Size;
byte[] byteChunk;
try {
fis = new FileInputStream(fileName);
//StupidTest.size = (int)fileName.length();
while (fileSize > 0) {
System.out.println("loop"+ i);
if (fileSize <= Chunk_Size) {
readLength = (int) fileSize;
}
byteChunk = new byte[(int)readLength];
read = fis.read(byteChunk, 0, (int)readLength);
fileSize -= read;
// cursor += read;
assert(read==byteChunk.length);
long aid = fileid;
aid = aid<<32 | nChunks;
chunks[i] = new DataUnit(byteChunk,aid);
// Lister.add(chunks[i]);
nChunks++;
++i;
}
fis.close();
fis = null;
}catch(Exception e){
System.out.println("File splitting exception");
e.printStackTrace();
}
return chunks;
}
你可以增加你的客戶端程序的堆大小,請看看http://stackoverflow.com/questions/1565388/increase-heap-size-in-java。 – cerkiewny
btw:assert(read == byteChunk.length)可能會觸發。您無法確定讀取的調用會填充陣列 – MTilsted