在JAR中指定JVM堆大小 [英] Specifying JVM heap sizes in JAR

查看:159
本文介绍了在JAR中指定JVM堆大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写游戏引擎,但它需要的内存多于默认设置。我知道如果你双击一个jar,就会使用默认设置。那么无论如何我可以指定JAR需要自定义堆大小的清单文件(或其他地方?)

I'm trying to write a game engine, but it needs more memory than the default settings. I know that if you double-click a jar, the default settings are used. So is there anyway I can specify that the JAR requires custom heap sizes in it's manifest file (or elsewhere?)

此外,引擎在它自己的jar和游戏中在另一个,引擎作为库。如果我在两个JAR中指定了不同的限制,那么JVM会优先考虑哪一个?

Also, the engine is in it's own jar and the game is in another, with the engine as a library. If I specify different limits in both JARs, which one will the JVM give preference to?

推荐答案

没有任何属性您的清单规范可以用来控制堆空间。相反,您可以在应用程序中编写一个启动器main方法,该方法使用 Runtime.exec 实现。由于您可以访问 java.home 系统变量,可以使用运行启动器的Java版本来启动游戏。

There aren't any properties in the manifest specifications that you can use to control heap space. Instead, you can write a launcher main method in your application that executes a second process with your real main method using one of the Runtime.exec implementations. Since you have access to the java.home system variable, you can use the version of Java that's running your launcher to also launch your game.

// Fix to use the version appropriate to the OS
String javaPath = System.getProperty("java.home") + "\bin\java.exe";
Runtime.exec("\"" + javaPath + "\" mygame.jar <heap_args>");

使用 java.home 获取Java路径避免了路径问题。如果您决定更改程序的启动方式,这也可以让您控制将来的启动。例如,可以将其更改为使用 处理 ,这样你也可以等待进程在你的启动器中终止,让你在游戏的JVM完全终止时控制运行后的清理。

Using java.home to get the Java path avoids path issues. This will also give you control over launching in the future if you decide to change how your program is launched. For example, this can be changed to use Process so that you can also wait for the process to terminate in your launcher, giving you control over post-run cleanup when the game's JVM is fully terminated.

这篇关于在JAR中指定JVM堆大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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