重复的Java运行时选项:首选项的顺序是什么?

时间:2020-12-11 17:18:48

Considering the following command line

考虑以下命令行

java -Xms128m -Xms256m myapp.jar

Which settings will apply for JVM Minimum memory (Xms option) : 128m or 256m ?

哪些设置适用于JVM最小内存(Xms选项):128m或256m?

5 个解决方案

#1


23  

Depends on the JVM, perhaps the version...perhaps even how many paper clips you have on your desk at the time. It might not even work. Don't do that.

取决于JVM,也许是版本......甚至可能是你当时桌面上有多少纸夹。它可能甚至不起作用。不要那样做。

If it's out of your control for some reason, compile and run this the same way you'd run your jar. But be warned, relying on the order of the options is a really bad idea.

如果由于某种原因它不受你的控制,编译并运行它就像你运行你的jar一样。但要注意,依靠选项的顺序是一个非常糟糕的主意。

public class TotalMemory
{
    public static void main(String[] args)
    {
         System.out.println("Total Memory: "+Runtime.getRuntime().totalMemory());
         System.out.println("Free Memory: "+Runtime.getRuntime().freeMemory());
    }
}

#2


41  

As always, check your local JVM's specific implementation but here is a quick way to check from the command line without having to code.

与往常一样,检查本地JVM的具体实现,但这是一种快速的方法,可以从命令行进行检查而无需编写代码。

> java -version; java -Xmx1G -XX:+PrintFlagsFinal -Xmx2G 2>/dev/null | grep MaxHeapSize

java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
uintx MaxHeapSize         := 2147483648        {product}

So you'll see in this case, the second instance of the argument (2G) is what takes precedence (at least in 1.8) and that has been my experience with most other modern versions as well.

所以你会看到,在这种情况下,参数的第二个实例(2G)是优先的(至少在1.8中),这也是我对大多数其他现代版本的体验。

#3


31  

The IBM JVM treats the rightmost instance of an argument as the winner. I can't speak to HotSpot, etc..

IBM JVM将最右边的参数实例视为获胜者。我不能和HotSpot等说话。

We do this as there are often deeply nested command lines from batch files where people can only add to the end, and want to make that the winner.

我们这样做是因为批处理文件中经常存在深层嵌套的命令行,人们只能添加到结尾,并希望使其成为赢家。

#4


27  

FTR, OpenJDK 1.7 also seems to take the rightmost value, at least for -Xms.

FTR,OpenJDK 1.7似乎也取得了最合适的价值,至少对于-Xms而言。

#5


9  

I bet it's the second one. Arguments are usually processed in the order:

我打赌这是第二个。参数通常按顺序处理:

for( int i=0; i<argc; i++ ) {
  process_argument(argv[i]);
}

But if I were writing java argument parser, I'd complain on conflicting arguments.

但是,如果我正在编写java参数解析器,我会抱怨冲突的参数。

#1


23  

Depends on the JVM, perhaps the version...perhaps even how many paper clips you have on your desk at the time. It might not even work. Don't do that.

取决于JVM,也许是版本......甚至可能是你当时桌面上有多少纸夹。它可能甚至不起作用。不要那样做。

If it's out of your control for some reason, compile and run this the same way you'd run your jar. But be warned, relying on the order of the options is a really bad idea.

如果由于某种原因它不受你的控制,编译并运行它就像你运行你的jar一样。但要注意,依靠选项的顺序是一个非常糟糕的主意。

public class TotalMemory
{
    public static void main(String[] args)
    {
         System.out.println("Total Memory: "+Runtime.getRuntime().totalMemory());
         System.out.println("Free Memory: "+Runtime.getRuntime().freeMemory());
    }
}

#2


41  

As always, check your local JVM's specific implementation but here is a quick way to check from the command line without having to code.

与往常一样,检查本地JVM的具体实现,但这是一种快速的方法,可以从命令行进行检查而无需编写代码。

> java -version; java -Xmx1G -XX:+PrintFlagsFinal -Xmx2G 2>/dev/null | grep MaxHeapSize

java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
uintx MaxHeapSize         := 2147483648        {product}

So you'll see in this case, the second instance of the argument (2G) is what takes precedence (at least in 1.8) and that has been my experience with most other modern versions as well.

所以你会看到,在这种情况下,参数的第二个实例(2G)是优先的(至少在1.8中),这也是我对大多数其他现代版本的体验。

#3


31  

The IBM JVM treats the rightmost instance of an argument as the winner. I can't speak to HotSpot, etc..

IBM JVM将最右边的参数实例视为获胜者。我不能和HotSpot等说话。

We do this as there are often deeply nested command lines from batch files where people can only add to the end, and want to make that the winner.

我们这样做是因为批处理文件中经常存在深层嵌套的命令行,人们只能添加到结尾,并希望使其成为赢家。

#4


27  

FTR, OpenJDK 1.7 also seems to take the rightmost value, at least for -Xms.

FTR,OpenJDK 1.7似乎也取得了最合适的价值,至少对于-Xms而言。

#5


9  

I bet it's the second one. Arguments are usually processed in the order:

我打赌这是第二个。参数通常按顺序处理:

for( int i=0; i<argc; i++ ) {
  process_argument(argv[i]);
}

But if I were writing java argument parser, I'd complain on conflicting arguments.

但是,如果我正在编写java参数解析器,我会抱怨冲突的参数。