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

时间:2022-09-29 07:40:41

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参数...

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文件中。那可能吗?

8 个解决方案

#1


12  

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

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

#2


20  

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文件本身中。但是,它可以覆盖在命令行上经常编写相同命令的问题。

#3


4  

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。

#4


3  

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。

#5


3  

Found an answer on 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中是,并且你应该在你的(可能是系统特定的)启动器/包装器脚本/应用程序中执行。

#6


2  

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堆的Silent Development Blog文章

#7


1  

Apparently this works on Ubuntu

显然这适用于Ubuntu

> export _JAVA_OPTIONS="-Xmx1g"

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

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

#8


0  

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。但要小心,如果从网上下载,用户必须设置脚本的权限才能运行

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捆绑器,Apple App捆绑器for Java 6构建.app文件。您仍然无法重新分发它,因为不会设置必要的权限。为app文件构建dmg。这可以用于分发。适用于Windows的类似安装程序

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

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

#1


12  

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

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

#2


20  

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文件本身中。但是,它可以覆盖在命令行上经常编写相同命令的问题。

#3


4  

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。

#4


3  

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。

#5


3  

Found an answer on 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中是,并且你应该在你的(可能是系统特定的)启动器/包装器脚本/应用程序中执行。

#6


2  

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堆的Silent Development Blog文章

#7


1  

Apparently this works on Ubuntu

显然这适用于Ubuntu

> export _JAVA_OPTIONS="-Xmx1g"

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

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

#8


0  

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。但要小心,如果从网上下载,用户必须设置脚本的权限才能运行

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捆绑器,Apple App捆绑器for Java 6构建.app文件。您仍然无法重新分发它,因为不会设置必要的权限。为app文件构建dmg。这可以用于分发。适用于Windows的类似安装程序

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

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