[英]Java Out of memory - Out of heap size
我下載了示例程序,用於客戶端和服務器之間的文件傳輸。 當我嘗試使用1 GB文件運行程序時,在線程“主” java.lang.OutOfMemoryError中出現異常:Client.main(Client.java:31)上的Java堆空間。
編輯:第31行:字節[] mybytearray =新字節[FILE_SIZE];
public final static int FILE_SIZE = 1097742336;
// receive file
long startTime = System.nanoTime();
byte [] mybytearray = new byte [FILE_SIZE];
InputStream is = sock.getInputStream();
fos = new FileOutputStream(FILE_TO_RECEIVED);
bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
有沒有解決辦法?
好吧,考慮到您要在內存中放入1gb文件,可以理解內存不足錯誤。 Java沒有足夠的可用內存。
-Xmx
標志允許您設置最大堆大小。
-Xmx2000m
應該足夠了。
如果出現“無法保留足夠的空間”錯誤,則表明您的計算機沒有足夠的可用內存。 試用較小的文件,或將文件分成小塊,然后一次對其進行分析。 或通過分塊讀取文件將其作為多個數據包發送。
您可以嘗試按如下所述設置Xmx和Xms屬性: -Xms:初始堆大小還是最小堆大小?
即使您克服了此文件的內存分配問題,仍然可能出現此問題。您可能要研究流傳輸:
int count;
byte[] buffer = new byte[8192];
while ((count = inStream.read(buffer)) > 0)
{
outStream.write(buffer, 0, count);
}
使用標志-Xmx2G或大於1G的某個值,然后重試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.