Outofmemoryerror: Java heap space

Asked

Viewed 233 times

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 wonder how to fix it.

  • This error is independent of your code: it depends on how Java runs. How you are running this code?

  • Debugging by Netbeans.

  • this code compiles?? your constructor is wrong.. I don’t think that’s the code you’re running.

  • Yeah, compile it, @Math.

  • @Patrick man, I opened the IDE and here does not compile, missing the () in his builder

  • Oh yes, I forgot that detail.

  • Okay, I fixed here and I circled, and it didn’t burst the memory. There’s nothing more divergent than what you posted and what you’re running from?

  • No, it might be the computer, because isn’t it always that this error happens? I am using a Windows 8 64bit on a pc with 1GB of RAM.

Show 3 more comments

2 answers

4


Since there is no error in the code maybe the problem is in the size of the heap space java.

As your machine has only 1 GB of RAM the default size of the heap Java may be too small.

Try to increase heap size in project options in Netbeans:

  1. Right click on your project and choose "Estates"
  2. Select the category "Execute"
  3. Typo -Xmx128m in the field "Options of the VM"

-Xmx128m tells VM to run with 128 MB of heap space, you can change this value to more (-Xmx256m) or less (-Xmx64m) as needed.

1

Make sure you have write permissions in 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.