简体   繁体   中英

How there is no affect of setting memory through System.setProperty?

import java.io.*;
import java.util.*;
public class ReadPropertiesFile {       
    public static void main(String[] args)throws Throwable{                
        Properties prop = new Properties();                
        prop.load(new FileInputStream(
            "C:\\Windows\\Sun\\Java\\Deployment\\deployment.properties"));
        String Xmx = prop.getProperty("deployment.javaws.jre.0.args",
                                      "This is Default"); 
        if(Xmx!="This is Default")
        {
            System.setProperty("javaplugin.vm.options","\"Xmx\"");
        }
        long maxMemory = Runtime.getRuntime().maxMemory();
        System.out.println("JVM maxMemory also equals to maximum heap size of JVM: "
                                         + maxMemory);
    }
}

It should print the value of maxMemory around 96MB(for 2 gb RAM) when nothing specified in the deployment.properties AND 512MB when explicitly mentioning the deployment.javaws.jre.0.args=-Xmx512m.But in both case i am getting the result 259522560

The JVM memory parameters for a Hotspot Java implementation can only be set via the command line options when the JVM is launched / started. Setting them in the system properties either before or after the JVM is launched will have no effect.

What you are trying to do simply won't work.

By the time any Java code is able to run, it is too late to change the heap size settings. It is the same whether you run your code using the java command, using web start, using an applet runner, using an embedded JVM in a browser ... or any other means .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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