重新启动具有更大堆空间的JVM [英] Re launch JVM with bigger heap space

查看:113
本文介绍了重新启动具有更大堆空间的JVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够执行.Jar文件,如果堆空间设置不够大,它应该启动一个具有相同.Jar文件的新JVM,但设置更大的堆空间,然后关闭第一个JVM和.Jar。

I want to be able to execute the .Jar file, and if the heap space isn't set big enough, it should launch a new JVM with the same .Jar file, but set with a bigger heap space, and then close the first JVM and .Jar.

我尝试过使用ProcessBuilder,但我无法使用它。

I've tried using the ProcessBuilder, but I can't get it to work.

它必须跨平台工作。

-ONi

推荐答案

我找到了解决方案,它可以跨平台工作。要从代码重新启动JVM,请使用以下命令。这个答案取自我在这里搜索数小时后发现的另一个问题。如果需要,可以使用System.exit(0)跟随它,以在调用此方法后终止启动新进程的JVM。

I have found the solution, and it works cross platform. To restart the JVM from code, use the following. This answer is taken from another question I found after hours of search in here. If you want, you can follow it with an System.exit(0), to terminate the JVM that started the new process, after a call to this method.

public static void startSecondJVM() throws Exception {
    String separator = System.getProperty("file.separator");
    String classpath = System.getProperty("java.class.path");
    String path = System.getProperty("java.home")
            + separator + "bin" + separator + "java";
    ProcessBuilder processBuilder = 
            new ProcessBuilder(path, "-Xmx1024m", "-cp",
            classpath, 
            Main.class.getName());
    Process process = processBuilder.start();
}

这篇关于重新启动具有更大堆空间的JVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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