0
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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Hriday
  • 9
  • 5
  • 1
    BTW: `Xmx!="This is Default"` will be false if the string in the properties is this string as it won't be the same object. – Peter Lawrey Nov 28 '12 at 12:18

1 Answers1

4

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • How to know that the jvm is launched or not. Actually i am working on a java web start application and i am unable to find the instant when this jvm is launched? – Hriday Nov 28 '12 at 12:13
  • 2
    If you are running any Java code, the JVM has been launched. There is no way to do anything in Java before this happens. – Peter Lawrey Nov 28 '12 at 12:15
  • @PeterLawrey Then it should print the memory as i expected? why it is showing same result? – Hriday Nov 28 '12 at 12:22
  • The maximum heap size is allocated when the JVM launches. Your code can only run after the JVM launches, i.e. your code can only run after it is too late to change it. – Peter Lawrey Nov 28 '12 at 12:33