How to increase java heap memory in netbeans?

2019-09-03 12:31发布

I'm trying to execute a java script on a large text file, but it gives me this error

     java.lang.OutOfMemoryError: Java heap space

I tried to do that on my netbeans project:

Project Properties -> Run -> VM Options -> -Xmx2048m -Xms1024m

I tried also :

System -> Programs -> Java -> Java -> View -> Execution Parameters -> -Xincgc -Xmx2048M

But i didn't solve the problem .. any suggestions please ?

2条回答
一夜七次
2楼-- · 2019-09-03 13:00

I would suggest altering your code, the other alternative is described in here: http://wiki.netbeans.org/FaqSettingHeapSize

查看更多
爷的心禁止访问
3楼-- · 2019-09-03 13:11

String storage can be quite expensive. Using random String of avarage ~ 80 chars long, 3 000 000 strings can cosume almoust 1GB

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    String base = RandomStringUtils.random(80);
    long i = 0;
    try {
        while (i < 3e6) {
            list.add(base + i++);
        }
    } finally {
        System.out.println("Count:" + list.size());
        System.out.println("Memory:" + Runtime.getRuntime().totalMemory() / 1024d / 1024d);
    }
}

Output: Count:3000000 Memory:981.5

Try to wrap your piece of code where you are reading your file with the same try-finally block i have made here. When error will popout, you will get approx ammount of memory you are consuming at that point, and what percentage of file you have managed already to read. This will get you the idea of how much more memory you will need.

Moreover you will know if your -Xmx directive works, because if it will crash around lets say 500mb it is either ommited by netbeans, or there is no mo available memory it the system.

查看更多
登录 后发表回答