0
public class Bad {
    public static void main(String[] args) {
        Integer[] buff = new Integer[5000000];
        int i = 0;
        while (true) {
            i++;
            if (i == buff.length)
                i = 0;
            Integer obj = new Integer(i); // line 14
            buff[i] = obj;
            // do something useful with buff[i];
        }
    }
}

terminated unexpectedly after several seconds and the following message was printed at the command line: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at exam.Bad.main(Bad.java:14)

Can anyone explain what went wrong, and please provide me with code to fix the problem?

Boann
  • 48,794
  • 16
  • 117
  • 146
  • because it is an infinite loop? – CasualT Aug 26 '13 at 21:04
  • You've allocated and created a large amount of Integer objects, which is consuming a lot of memory. If you expand the vm parameters to have a large heap space, you'll be fine. However, can't you just call buff[i] = i? – CBredlow Aug 26 '13 at 21:05
  • What does buff[i] have done to it? Is it something that could just be done to obj, and after all that set to buff[i]? – CBredlow Aug 26 '13 at 22:13

3 Answers3

3

You simply run out of heap space.
In few words heap is a (finite) portion of memory where dynamic data are allocated when you do a new instruction.

Integer[] buff = new Integer[5000000];

will allocate a huge portion of heap space and, in your loop, Integer obj = new Integer(i); will allocate more heap memory until the limit (this is the reason of exception).

With

-Xmx set maximum Java heap size

option (for java command) you can allocate a bigger heap space

EDIT (about your code):

for(int i=0;i < buff.length;i++) {
  if (i == buff.length) {
    // But this is only an hint to perform garbage as soon as possible
    System.gc();
    i = 0;
  }
  Integer obj = new Integer(i); // line 14
  buff[i] = obj;
  // do something useful with buff[i];
}

but you have (probably) to max out heap space to let it work, depends when garbage is executed

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • couldn't there be a check for if buff[i] != null, and if it isn't do operations with it, instead of creating an entirely new integer? – CBredlow Aug 26 '13 at 22:09
  • This seems to be a test code and I can't imagine the real use. IMHO this code is not robust at all and any fix is like a patch not a solution. my 2 cents, of course – Luca Basso Ricci Aug 26 '13 at 22:12
1

You have an infinite loop while(true)
and you create objects on the heap in the loop

Integer obj = new Integer(i);

So you run out of heap space.

You haven't told us what your code does but you need to break the loop at some point or have a condition that will return false. Since your just going through the loop over and over again doing except reseting the elements in the array you may want to just go through it once instead of reseting i to 0

aaronman
  • 18,343
  • 7
  • 63
  • 78
0

you have to put a break; but you have one bad operation after if (i == buff.length) erase the code: i = 0;

This is the code that i think can help you:

public class Bad {
    public static void main(String[] args) {
        Integer[] buff = new Integer[500];
        int i = 0;
        while (true) {
            Integer obj = new Integer(i); // line 14
            buff[i] = obj;
            // do something useful with buff[i];
            //After do something use a coditional to break the loop like this
             i++;
            if(i>=buff.length){i = 0;break;}

        }
    }
}