2

I am getting following error on the commented line:

"OutOfMemoryError: Java heap space" 

The data.bin file is huge so this error is expected. My question is how exactly do I get around this error? I have seen some stuff like -Xmx1024m or similar, but I don't know how to run that using jgrasp compiler

 try {
    RandomAccessFile data = new RandomAccessFile("data.bin","rws");
    long l = data.length();
    long recs = l / 1024;
    long cnt = 0;
    byte []b = new byte[1024];

    while(cnt < recs){
       cnt++;
       data.readFully(b);
       byte []key = Arrays.copyOfRange(b, 0, 24);
       byte []value = Arrays.copyOfRange(b, 24, 1024);
       en = new TEntry<String, String>(new String(key), new String(value));//ERROR
       lst.add(en);
    }
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
MadeInJapan
  • 45
  • 1
  • 8
  • possible duplicate of [changing jvm heap size - jgrasp](http://stackoverflow.com/questions/8332370/changing-jvm-heap-size-jgrasp) – Paul Vargas May 03 '13 at 05:01
  • -Xmxn and -Xmsn are the maximum and minimum values which determine the heap size of your application. – prasanth May 03 '13 at 05:04

3 Answers3

2

You can increase your heap/stack size using following commands:

-Xms<size>        set initial Java heap size
-Xmx<size>        set maximum Java heap size
-Xss<size>        set java thread stack size

You can also find more descriptive answers over here.

Community
  • 1
  • 1
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
1

You need to set the heap min and max size according to your needs and within the boundary of available main memory. You can do so by adding the following params to the java run time -J-Xmx1024m -J-Xms512m

Are you on Windows? In the installation folder there's a file called "winconfig.exe", double click it and you can change your heap there. If you're on Linux or Mac OS, it's probably called something similar (with the word "config" in it).

jgrasp -J-Xmx512m -J-Xms512m

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

You are trying to load the entire file into memory. When you turn Bytes into Strings it will be taking just over double the size of the file. (Strings use 16-bit characters) Either you have enough memory to do this (in which case increase your maximum) or you need to change when your program does.

Instead of trying to load all the keys and values, you could load just the values and remember where those values are. i.e. store the offset in the file for that value.

When you look up the value again, you need to read the value as required. In this case it could reduce the memory you need by a factor of 20.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130