我可以设置从 jar 文件运行的 Java 最大堆大小吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1018217/
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-08-11 22:19:16  来源:igfitidea点击:

Can I set Java max heap size for running from a jar file?

javamemory-managementjar

提问by Kip

I am launching a java jar file which often requires more than the default 64MB max heap size. A 256MB heap size is sufficient for this app though. Is there anyway to specify (in the manifest maybe?) to always use a 256MB max heap size when launching the jar? (More specific details below, if needed.)

我正在启动一个 java jar 文件,它通常需要超过默认的 64MB 最大堆大小。不过,256MB 的堆大小足以满足此应用程序的需求。无论如何要指定(可能在清单中?)在启动 jar 时始终使用 256MB 的最大堆大小?(如果需要,下面有更具体的细节。)



This is a command-line app that I've written in Java, which does some image manipulation. On high-res images (about 12 megapixels and above, which is not uncommon) I get an OutOfMemoryError.

这是我用 Java 编写的命令行应用程序,它执行一些图像处理。在高分辨率图像(大约 12 兆像素及以上,这并不罕见)上,我收到 OutOfMemoryError。

Currently I'm launching the app from a jar file, i.e.

目前我正在从 jar 文件启动应用程序,即

java -jar MyApp.jar params...

java -jar MyApp.jar params...

I can avoid an OutOfMemoryError by specifying 256MB max heap size on the command line, i.e.:

我可以通过在命令行上指定 256MB 最大堆大小来避免 OutOfMemoryError,即:

java -Xmx256m -jar MyApp.jar params...

java -Xmx256m -jar MyApp.jar params...

However, I don't want to have to specify this, since I know that 256MB is sufficient even for high-res images. I'd like to have that information saved in the jar file. Is that possible?

但是,我不想指定这一点,因为我知道即使对于高分辨率图像,256MB 也足够了。我想将该信息保存在 jar 文件中。那可能吗?

采纳答案by willcodejavaforfood

you could also use a wrapper like launch4jwhich will make an executable for most OS:es and allows you to specify VM options.

您还可以使用像launch4j这样的包装器,它可以为大多数操作系统生成可执行文件:es 并允许您指定 VM 选项。

回答by instanceof me

Found an answeron google.

在谷歌上找到了答案

He says no for the JAR file, yes in JavaWeb Start, and that you should do it in your (possibly system-specific) launcher/wrapper script/app.

他对 JAR 文件说不,在 JavaWeb Start 中说是,并且你应该在你的(可能是特定于系统的)启动器/包装器脚本/应用程序中执行它。

回答by Alex Beardsley

This isn't great, but you could have it as an executable JAR, and then in it's main, have it execute itself via the command-line as a non-daemon thread with the proper params stored in a properties file or calculated or whatever, then exit the original. You could even have it execute the jar with another "real" entry-point that expects those parameters.

这不是很好,但你可以将它作为一个可执行的 JAR,然后在它的主要部分中,让它作为非守护线程通过命令行执行自身,并将正确的参数存储在属性文件中或计算或其他,然后退出原来的。您甚至可以让它使用另一个需要这些参数的“真实”入口点来执行 jar。

回答by Ben

YES!You need to write a small starter program - I wrote a small mostly cross platform compatible (paths should be fine) program to do it - see below. This has been tested on Windows/Ubuntu/Mac OS X.

是的!你需要编写一个小的启动程序——我写了一个小的,主要是跨平台兼容的(路径应该没问题)程序来完成它——见下文。这已经在 Windows/Ubuntu/Mac OS X 上测试过。

Silent Development Blog article on increasing executable Java jar heap

关于增加可执行 Java jar 堆的静默开发博客文章

回答by Andrew

I had a similar need, and thought I should be able to specify this in the manifest as suggested above. However, this is not possible. My solution is similar to what Alex suggested above, however, what I did was to have the main method use Java's ProcessBuilder to start another Java process. When starting another process you can specify the maximum memory for the new process. You are essentially having one Java process start another one. It's a little hokey, but it does work and it still allows you to launch your app by double-clicking the Jar file, which I know you want to do. Look into the ProcessBuilder.

我有类似的需求,并认为我应该能够按照上面的建议在清单中指定这一点。然而,这是不可能的。我的解决方案与上面 Alex 建议的类似,但是,我所做的是让 main 方法使用 Java 的 ProcessBuilder 来启动另一个 Java 进程。启动另一个进程时,您可以为新进程指定最大内存。您实际上是让一个 Java 进程启动另一个进程。这是一个小技巧,但它确实有效,它仍然允许您通过双击 Jar 文件来启动您的应用程序,我知道您想要这样做。查看 ProcessBuilder。

回答by Jatin

There are many ways of doing it:

有很多方法可以做到:

You can by having another Jar file which triggers your main Jar.

您可以使用另一个 Jar 文件来触发您的主 Jar。

Or, have a batch file which will start your jar. But be careful, if downloaded from say net, the user will have to set permissions for the script to be runnable

或者,有一个批处理文件来启动你的 jar。但要小心,如果从 say net 下载,用户必须设置脚本的权限才能运行

Build an installer. On Mac, using the Oracle Java App bundler for Java 7, Apple App bundler for Java 6 build the .app file. You still cant redistribute it as the necessary permissions wont be set. Build a dmg for the app file. This can be used for distribution. A similar installer for Windows

构建安装程序。在 Mac 上,使用适用于 Java 7 的 Oracle Java App bundler 和适用于 Java 6 的 Apple App bundler 构建 .app 文件。您仍然无法重新分发它,因为不会设置必要的权限。为应用程序文件构建 dmg。这可以用于分发。适用于 Windows 的类似安装程序

The third technique would be the best, as you can then package the dependencies well, set all JVM arguments etc

第三种技术是最好的,因为您可以很好地打包依赖项,设置所有 JVM 参数等

回答by Syd Gillani

Apparently this works on Ubuntu

显然这适用于 Ubuntu

> export _JAVA_OPTIONS="-Xmx1g"

java -jar jconsole.jar & Picked up _JAVA_OPTIONS: -Xmx1g

java -jar jconsole.jar & 选择 _JAVA_OPTIONS: -Xmx1g

回答by kravi

Write a batch or shell script containing the following line. Put into the folder, where MyApp.jar is stored.

编写包含以下行的批处理或 shell 脚本。放入存放MyApp.jar的文件夹。

java -Xmx256M -jar MyApp.jar

After that always open this batch/script file in order to launch the JAR file. Although, This will not embed specification of virtual memory size into jar file itself. But, It can override the problem to write the same command often on command line.

之后,始终打开此批处理/脚本文件以启动 JAR 文件。虽然,这不会将虚拟内存大小的规范嵌入到 jar 文件本身中。但是,它可以覆盖经常在命令行上编写相同命令的问题。