有没有办法在代码本身中增加Java堆空间? [英] Is there a way to increase Java heap space in the code itself?

查看:192
本文介绍了有没有办法在代码本身中增加Java堆空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

是否可以动态更改最大Java堆大小?

我知道Java启动器有一个XMX选项,但是有某种预处理指令可以做到这一点(所以运行我的代码的所有计算机都会增加它们的堆) 。

I know there is an XMX option for the Java launcher, but is there some kind of preprocessing directive that does this instead (so that all computers that run my code will have their heap increased).

就像现在一样,java.exe只能达到~280MB最大值(在它崩溃之前) - 这是正常的吗?

As it is right now, java.exe reaches ~280MB max only (before it crashes) - is this normal?

推荐答案


启动具有更多内存的新进程可以在代码中完成吗?

"launch a new Process with more memory" can this be done in the code?

是的。请参阅此简单示例。

Yes. See this simplistic example.

import java.awt.EventQueue;
import javax.swing.JOptionPane;
import java.io.File;

class BigMemory {

    public static void main(String[] args) throws Exception {
        if (args.length==0) {
            ProcessBuilder pb = new ProcessBuilder(
                "java",
                "-Xmx1024m",
                "BigMemory",
                "anArgument"
                );
            pb.directory(new File("."));
            Process process = pb.start();
            process.waitFor();
            System.out.println("Exit value: " + process.exitValue());
        } else {
            Runnable r = new Runnable() {
                public void run() {
                    JOptionPane.showMessageDialog(
                        null,
                        "Max Memory: " +
                        Runtime.getRuntime().maxMemory() +
                        " bytes.");
                }
            };
            EventQueue.invokeLater(r);
        }
    }
}

这篇关于有没有办法在代码本身中增加Java堆空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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