簡體   English   中英

內存不足錯誤:Java堆空間

[英]Out of Memory Error: Java Heap space

我正在嘗試使用映射的字節緩沖區讀取文件,並且在行buffer.position(position);處獲取OutOfMemoryError:JavaHeapSpace ..我不了解代碼中的錯誤。.可能是錯誤的原因嗎?

private void readFile()
{
    int lastPos = buffer.getInt();

    int dirLen = buffer.getInt();
    byte[] dirBytes = new byte[dirLen];
    buffer.get(dirBytes);
    this.dir = new String(dirBytes);

    int filePatternLen = buffer.getInt();
    byte[] filePatternBytes = new byte[filePatternLen];
    buffer.get(filePatternBytes);
    this.filePattern = new String(filePatternBytes);

    int numFileMetaDatas = 0;
    while (buffer.position() < lastPos)
    {
        numFileMetaDatas++;
        int position = buffer.position();
        //Size is needed as we are reserving some extra bytes
        int size = buffer.getInt();
        buffer.position(position + ByteUtil.SIZE_OF_INT + ByteUtil.SIZE_OF_BYTE + ByteUtil.SIZE_OF_LONG + ByteUtil.SIZE_OF_LONG + ByteUtil.SIZE_OF_LONG);
        int fileNameLen = buffer.getInt();
        byte[] fileNameBytes = new byte[fileNameLen];
        buffer.get(fileNameBytes);

        FileMetaData fileMetaData = new FileMetaData(buffer, size, position);
        UniqueID uniqID = fileMetaData.getUniqueID();

        uniqIdVsFileMetaData.put(uniqID, fileMetaData);

        position = position + size;
        buffer.position(position);
    }
    nextMetaStartPos = lastPos;
}

解決Java中OutOfMemoryError的簡單方法是使用JVM選項“ -Xmx512M”增加最大堆大小,這將立即解決您的OutOfMemoryError。 當我在構建項目時在Eclipse,Maven或ANT中遇到OutOfMemoryError時,這是我的首選解決方案,因為根據項目的大小,您很容易耗盡內存。這是增加JVM最大堆大小的示例,而且最好保持-如果要在Java應用程序中設置堆大小,則Xmx與-Xms的比率為1:1或1:1.5

export JVM_ARGS =“-Xms1024m -Xmx1024m”

2)解決Java中OutOfMemoryError的第二種方法相當困難,並且當您沒有足夠的內存並且即使增加最大堆大小之后,您仍然會遇到java.lang.OutOfMemoryError,在這種情況下,您可能希望分析應用程序,尋找任何內存泄漏。 您可以使用Eclipse Memory Analyzer檢查您的堆轉儲,也可以使用任何配置程序,例如Netbeans或JProbe。 這是一個艱難的解決方案,需要一些時間來分析和查找內存泄漏。

    -vm
C:/Program Files (x86)/Java/jdk1.6.0/bin/javaw.exe
-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.100.v20110502
-product
com.springsource.sts.ide
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
384M//incease to 512 mb
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xmn128m
-Xms256m
-Xmx768m
-Xss1m
-XX:PermSize=128m// increase to 512 mb 
-XX:MaxPermSize=384m//increase to 512 mb

轉到eclipse配置設置文件,即ini文件,更改燙發大小
下面的評論告訴你增加容量

-vm
C:/Program Files (x86)/Java/jdk1.6.0/bin/javaw.exe
-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.100.v20110502
-product
com.springsource.sts.ide
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
384M//incease to 512 mb
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xmn128m
-Xms256m
-Xmx768m
-Xss1m
-XX:PermSize=128m// increase to 512 mb 
-XX:MaxPermSize=384m//increase to 512 mb

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM