1

I'm trying to read a file using mapped byte buffer and am getting an OutOfMemoryError:JavaHeapSpace at the line buffer.position(position);.. I don't understand what's wrong in the code.. What could be the reason for the error?

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;
}
Arun
  • 21
  • 1
  • 6
  • go to eclipse configiration setting file i.e ini file change the perm size – vaib Jan 30 '15 at 11:32
  • As the code doesn't include all informations. Here is only a guess `int size = buffer.getInt();` might return a huge int. – SubOptimal Jan 30 '15 at 11:33
  • Have you seen this link < http://www.mkyong.com/eclipse/eclipse-java-lang-outofmemoryerror-java-heap-space/ > This link will help. I think just increasing the heap size will do the trick for you. – RishiKesh Pathak Jan 30 '15 at 11:39
  • @Rishikesh.. increasing the heap size can not be the solution because the code was working fine for months. But I'm getting this error in recent times. – Arun Jan 30 '15 at 12:59

2 Answers2

0

Easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError. This is my preferred solution when I get OutOfMemoryError in Eclipse, Maven or ANT while building project because based upon size of project you can easily ran out of Memory.here is an example of increasing maximum heap size of JVM, Also its better to keep -Xmx to -Xms ration either 1:1 or 1:1.5 if you are setting heap size in your java application

export JVM_ARGS="-Xms1024m -Xmx1024m"

2) Second way to resolve OutOfMemoryError in Java is rather hard and comes when you don't have much memory and even after increase maximum heap size you are still getting java.lang.OutOfMemoryError, in this case you probably want to profile your application and look for any memory leak. You can use Eclipse Memory Analyzer to examine your heap dump or you can use any profiler like Netbeans or JProbe. This is tough solution and requires some time to analyze and find memory leaks.

    -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
vaib
  • 319
  • 4
  • 27
-2

go to eclipse configiration setting file i.e ini file change the perm size
below comment tell you increse the permsize

-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
vaib
  • 319
  • 4
  • 27
  • 1
    Why do you think the permgen is involved? The OP specifically said that the error was out-of-memory in the **JavaHeapSpace**, not in the PermGen space. – Erwin Bolwidt Jan 30 '15 at 11:44
  • @Erwin Bolwidt javaHeap Space and permGen Space both are showing are occuring when JVM ran out of memory. – vaib Jan 30 '15 at 11:47
  • When you run out of PermGen space, the exception is `java.lang.OutOfMemoryError: PermGen space`. The OP said that his error was `OutOfMemoryError: JavaHeapSpace` So there is no indication in the question that the PermGen was involved. If you still think it might be, you could ask the OP in a comment. – Erwin Bolwidt Jan 30 '15 at 11:49
  • @Erwin why down vote answer both error have slightly diffrent but solution i given it may solve the problem – vaib Jan 30 '15 at 11:52
  • I don't downvote and comment at the same time. But I understand a downvote: answers aren't meant for hunches, especially when they contradict what the OP has posted. – Erwin Bolwidt Jan 30 '15 at 12:21