java 如何为 jar 文件设置最大 jvm 内存(XMX)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2848346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:07:51  来源:igfitidea点击:

How do I set maximal jvm-memory (XMX) for a jar-file

javajarjvm

提问by Fabian Zeindl

How do I set the maximal jvm-memory without adding an extra batch-script to my Programm. Answer is provided below.

如何设置最大 jvm 内存而不向我的程序添加额外的批处理脚本。答案如下。

采纳答案by mdma

It's a good question, but your implmenetation assumes a lot. I presume you have to document the name of your jar for your users to invoke "java -jar xyz.jar" on so can you also include in the documentation that "-Xmx256M" is required?

这是一个很好的问题,但您的实现假设了很多。我认为您必须记录您的 jar 的名称,以便您的用户调用“java -jar xyz.jar”,那么您是否还可以在需要“-Xmx256M”的文档中包含?

You may have more luck using a java launcher, such as this onefor Windows, where you put the launcher config (path, max memory etc.) in a separate file. For cross platform, there's LaunchAnywhere, and others, which function similarly. See How can I convert my Java program to an .exe file?

您可能需要使用Java启动,更多的运气如这一个针对Windows,你把发射器配置(路径,最大内存等),在一个单独的文件。对于跨平台,有 LaunchAnywhere 和其他类似的功能。请参阅如何将我的 Java 程序转换为 .exe 文件?

To improve upon your existing scheme:

改进您现有的方案:

  • use the java.homesystem property to resolve the location of the JDK/JRE
  • use the java.class.pathto set up the class path, and similarly for java.library.pathif your application requires native code.
  • pass command arguments passed to your main method to the spanwed process
  • 使用java.home系统属性来解析 JDK/JRE 的位置
  • 使用java.class.path来设置类路径,java.library.path如果您的应用程序需要本机代码,则类似。
  • 将传递给 main 方法的命令参数传递给 spanwed 进程

But still, this seems to be a lot of effort that at best represents a leaky abstraction, when instead simple and clear documentation would suffice.

但是,这似乎是很多努力,充其量只能代表一个有漏洞的抽象,而简单而清晰的文档就足够了。