如何增加Netbeans中的Java堆内存? [英] How to increase java heap memory in netbeans?

查看:97
本文介绍了如何增加Netbeans中的Java堆内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在大型文本文件上执行Java脚本,但这给了我这个错误

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

我试图在我的netbeans项目中做到这一点:

I tried to do that on my netbeans project:

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

我也尝试过:

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

但是我没有解决问题..有什么建议吗?

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

推荐答案

字符串存储可能非常昂贵.使用随机的〜80个字符长的字符串,3 000 000个字符串可以算出1GB的空间

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);
    }
}

输出: 数:3000000 内存:981.5

Output: Count:3000000 Memory:981.5

尝试使用与我在此处创建的相同的try-finally块将代码段包装在读取文件的位置.当错误弹出时,您将获得大约正在使用的内存量,以及已设法读取的文件百分比.这会让您知道需要多少内存.

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.

此外,您会知道您的-Xmx指令是否有效,因为如果它崩溃大约是500mb,则说明它要么被netbeans遗漏,要么系统没有可用内存.

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.

这篇关于如何增加Netbeans中的Java堆内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆