简体   繁体   English

java 启动选项“-Xmx512M -XX:MaxPerSize=1024M”不适用于我的 jvm

[英]java startup option "-Xmx512M -XX:MaxPerSize=1024M" doesn't work for my jvm

I'm using oracle jdk1.8 on win10, trying to set max heap size of JVM and see what happens when OOM.我在win10上使用oracle jdk1.8,试图设置JVM的最大堆大小,看看OOM时会发生什么。 Using this code snippet:使用此代码段:

package mygroup;
import java.util.ArrayList;
import java.util.List;

public class App 
{
    public static class AUser{
        private String name;
        private String gender;
        private int age;
        public AUser(String n,String g,int a){
            name=n;
            gender=g;
            age=a;
        }
    }

    public static void main( String[] args ) throws Exception
    {
        System.out.println( "App!" );
        List<AUser> users=new ArrayList<AUser>();
        int max=40960000;
        int c=0;
        while(c < max){
            ++c;
            users.add(new AUser("Me","male",35));
        }
        System.in.read();
    }
}

I compile and run it with我编译并运行它

java mygroup/App -Xmx512M -XX:MaxPerSize=1024M

I expect that before it hits "System.in.read()", it should crash and report OOM error for JVM.我希望在它点击“System.in.read()”之前,它应该崩溃并报告 JVM 的 OOM 错误。 But nothing happened.但是什么也没发生。 Acturally it runs normally, and wait for my key strike.实际上它运行正常,等待我的击键。

Then I started java visualVM, connect to "mygroup.App", and executed "heap dump".然后我启动 java visualVM,连接到“mygroup.App”,并执行“heap dump”。 I could see that this JVM is using around 3.8G memory and totally 40960000 instances of "mygroup.App@AUser" is using 1.47G of memory.我可以看到这个 JVM 使用了大约 3.8G 内存,总共 40960000 个“mygroup.App@AUser”实例使用了 1.47G 内存。

Seems that "-Xmx512M -XX:MaxPerSize=1024M" is not working.似乎“-Xmx512M -XX:MaxPerSize=1024M”不起作用。

And if I change my program to如果我将程序更改为

        while(true){
            users.add(new AUser("Me","male",35));
        }

to create objects, actually this program will run until there's no more memory of my machine(I have 16G memory!)要创建对象,实际上这个程序会一直运行到我的机器没有更多内存为止(我有 16G 内存!)

So how to make "max heap size" work?那么如何使“最大堆大小”起作用呢? Thansk!谢谢!

The JVM arguments need to go before the class you're running, as Slaw pointed out in a comment above.正如 Slaw 在上面的评论中指出的那样,JVM 参数需要放在您正在运行的类之前

Here's a really simple program, all it does it print out whatever string data was sent to the program as input parameters:这是一个非常简单的程序,它会打印出作为输入参数发送到程序的任何字符串数据:

import java.util.Arrays;

public class example {
    public static void main(String[] args) {
        System.out.println("arguments: " + Arrays.toString(args));
    }
}

Here are a few runs showing that everything after example.java is simply passed into the program as input arguments:以下是一些运行,显示example.java之后的所有内容都只是作为输入参数传递到程序中:

$ java example.java
arguments: []

$ java example.java one two three
arguments: [one, two, three]

Here's a variation identical to what you posted, with your intended JVM arguments isntead being treated as regular input parameters to the program:这是与您发布的内容相同的变体,您的预期 JVM 参数不会被视为程序的常规输入参数:

$ java example.java -Xmx512M -XX:MaxPerSize=1024M
arguments: [-Xmx512M, -XX:MaxPerSize=1024M]

So, you'll want to re-order the command input to set JVM parameters instead of sending input parameters to your program.因此,您需要重新排序命令输入以设置 JVM 参数,而不是将输入参数发送到您的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM