Java 开始运行jar文件时如何设置-Xmx?

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

How to set the -Xmx when start running a jar file?

javajar

提问by Judking

As we know that we can set -Xmx1024Min window->preferences->java->installed jres->edit->default vm argumentsin eclipse. But when I package this project into a runnable jar file, how can I set the -Xmx1024Mwhen running the jar via java -jar A.jar?

因为我们知道,我们可以设置-Xmx1024Mwindow->preferences->java->installed jres->edit->default vm arguments日食。但是当我将这个项目打包成一个可运行的 jar 文件时,我如何设置-Xmx1024M运行 jar 的时间java -jar A.jar

Thanks a lot!

非常感谢!

采纳答案by Davy Cielen

try java -Xmx1024mfilename.

试试java -Xmx1024m文件名。

I found this on StackOverflow What does Java option -Xmx stand for?and use it when I start Netbeans for instance.

我在 StackOverflow 上找到了这个Java 选项 -Xmx 代表什么?例如,当我启动 Netbeans 时使用它。

use it like this

像这样使用它

java -Xmx1024m -jar JavaApplication.jar

info: -XmxnSpecify the maximum size, in bytes, of the memory allocation pool. This value must be a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 64MB. The upper limit for this value will be approximately 4000m on Solaris 7 and Solaris 8 SPARC platforms and 2000m on Solaris 2.6 and x86 platforms, minus overhead amounts.

info: -Xmxn指定内存分配池的最大大小,以字节为单位。该值必须是大于 2MB 的 1024 的倍数。附加字母 k 或 K 以指示千字节,或附加字母 m 或 M 以指示兆字节。默认值为 64MB。此值的上限在 Solaris 7 和 Solaris 8 SPARC 平台上约为 4000m,在 Solaris 2.6 和 x86 平台上约为 2000m,减去开销量。

回答by user1361991

Three methods:

三种方法:

  • Command Line:
    • Instruct your users to run your application using "java -jar SampleJavaApp.jar -Xmx1024m"
  • Java Control Panel:
    • Instruct your users to dedicate more memory to java by default: Win7 guide
  • Restart your jar with the appropriate Xmx value.
  • 命令行:
    • 指示您的用户使用“java -jar SampleJavaApp.jar -Xmx1024m”运行您的应用程序
  • Java 控制面板:
    • 指示您的用户默认为 Java 分配更多内存:Win7 指南
  • 使用适当的 Xmx 值重新启动 jar。

The last option is "evil" but doesn't require any extra effort from your users. Here's a sample block of code:

最后一个选项是“邪恶的”,但不需要用户做任何额外的努力。这是一个示例代码块:

public static void main(String[] args) throws IOException, URISyntaxException {
    String currentPath=SampleJavaApp.class
          .getProtectionDomain()
          .getCodeSource().getLocation()
          .toURI().getPath()
          .replace('/', File.separator.charAt(0)).substring(1);
    if(args.length==0 && Runtime.getRuntime().maxMemory()/1024/1024<980) {
        Runtime.getRuntime().exec("java -Xmx1024m -jar "+currentPath+" restart");
        return;
    }
}

回答by Philipp Merkle

Unfortunately, existing answers are wrong in one crucial point.

不幸的是,现有答案在一个关键点上是错误的。

-Xmxmust be passed to the Java runtime environment, not to the executed jar.

-Xmx必须传递给 Java 运行时环境,而不是传递给执行的 jar。

Wrong:

错误的:

java -jar JavaApplication.jar -Xmx1024m 

Correct:

正确的:

java -Xmx1024m -jar JavaApplication.jar 

More specifically, the java launcherneeds to be used as follows:

更具体地说,需要按如下方式使用java 启动器

java [options] -jar file.jar [arguments]

java [选项] -jar file.jar [参数]

  • [options]are passed to the Java runtime environment
  • [arguments]are passed to the main function
  • [options]传递给 Java 运行时环境
  • [arguments]传递给主函数

The -Xmxparameter belongs to the (nonstandard) JVM options, and--being an option--needs to be listed before -jar (or at least before file.jar). The JVM will not recognize an -Xmxargument passed to the main function as proposed in other answers.

-Xmx参数属于(非标准)JVM 选项,并且——作为一个选项——需要列在 -jar 之前(或至少在 file.jar 之前)。JVM 不会识别-Xmx其他答案中建议的传递给 main 函数的参数。

回答by user4599075

user1361991 > I like your evil option but I cannot comment it since I am still a newbie here. Anyway, I thought it deserved a little enhancement since I find it lacks the stderr and stdout redirection.

user1361991 > 我喜欢你的邪恶选项,但我不能评论它,因为我在这里还是个新手。无论如何,我认为它应该得到一点改进,因为我发现它缺少 stderr 和 stdout 重定向。

String currentPath= MyClass.class
    .getProtectionDomain()
    .getCodeSource().getLocation()
    .toURI().getPath()
    .replace('/', File.separatorChar).substring(1) ;
if ( args.length == 0 && Runtime.getRuntime().maxMemory()<512*1024*1024) {
    Process p= Runtime.getRuntime().exec("java -jar -Xmx512M " + currentPath + " restart") ;
    new StreamGobbler(p.getInputStream()).start() ;
    new StreamGobbler(p.getErrorStream()).start() ;
    p.waitFor() ;
    return ;
}

and the StreamGobbler source (probably retrieved from somewhere on the Internet to be honest and modified a little, I can't remember):

和 StreamGobbler 源代码(老实说,可能是从互联网上的某个地方检索到的,并做了一些修改,我不记得了):

public class StreamGobbler
    extends Thread
{
    public StreamGobbler( InputStream is )
    {
        this(is, System.out) ;
    }

    public StreamGobbler( InputStream is, PrintStream ps )
    {
        this.is= is ;
        this.ps= ps ;
    }
    private final InputStream is ;
    private final PrintStream ps ;

    @Override
    public void run()
    {
        try {
            InputStreamReader isr= new InputStreamReader(is) ;
            BufferedReader br= new BufferedReader(isr) ;
            for ( String line ; (line= br.readLine()) != null ; ) {
                ps.println(line) ;
            }
       }
       catch ( IOException ioe ) {
           ioe.printStackTrace() ;
       }
    }
}

回答by venkatesh akkisetty

Correct way to set Xmx value for jar file at run time is:

在运行时为 jar 文件设置 Xmx 值的正确方法是:

java -Xmx512m -jar Application.jar