OutOfMemoryError: Java heap space

1

I have the following code:

class Main {
    public static void main(String[] args) {
        Lista chamar = new Lista();
        String[] vetor = {"Palavra", "Letra"};
        chamar.adicionar(vetor);
    }
}
class Lista {
    ArrayList<String> lista;
    public Lista() {
        lista = new ArrayList<>();
    } public void adicionar(String[] vetor) {
        int i = 0;
        while (i < vetor.length) {
            lista.add(vetor[i]);
            i++;
        }
    }
}

When I run, the error "OutOfMemoryError: Java heap space" appears, I would like to know how to fix it.

    
asked by anonymous 12.04.2014 / 19:09

2 answers

4

As there is no error in the code, the problem may be in the heap space size of Java.

Because your machine has only 1 GB of RAM, the default size of the heap of Java may be very small.

Try increasing the heap size in the NetBeans project options:

  • Right click on your project and choose " Properties "
  • Select the " Run " category
  • Type -Xmx128m in the " VM Options " field
  • -Xmx128m tells VM to run with 128 MB of heap space , you can change this value by more (-Xmx256m) or less (-Xmx64m) as needed.

        
    13.04.2014 / 01:24
    1

    Make sure you have write permissions on the / drive directory where Netbeans is installed. This OutOfMemoryError can happen in installations where the user running Netbeans does not have many permissions on the machine.

        
    14.04.2014 / 19:17