Reputation: 1287
Am I right in thinking that when one specifies VM arguments in an IDE (I'm using NetBeans in this instance), that these arguments are only passed when the code is run through the IDE itself?
Essentially, I'd like to specify that when my program runs, the VM's minimum/initial heap size is 2Gb. I can do this using the -Xms2048m command, but I'm wondering if there's some way to achieve this without having to type a command (for the customer's sake).
Even thought I set the VM argument in NetBeans, and Launch4J (I wrap the JAR into an EXE file), when the program boots & outputs the Runtime's total memory size, it always gives ~120Mb.
What am I missing?
Edit: I output the total memory size using...
int mb = 1024 * 1024;
System.out.println("Max Memory: " + Runtime.getRuntime().totalMemory() / mb);
Edit 2: Could one not create a initialising program that takes no arguments, but starts the main program with the relevant VM arguments? Something like...
public class Main {
public static void main(String[] args) {
String execName = new File(new File("").getAbsolutePath()) + "\\Program.exe";
Runtime rt = Runtime.getRuntime();
rt.exec("java -Xms2048m -Xmx4096m -jar " + execName);
}
}
Upvotes: 0
Views: 3079
Reputation: 533472
The only way to do this is to have the program start another copy of the program witht eh heap you want. Note: the end user might not want 2 GB e.g.
If they have 32-bit windows they cannot e.g. if their default is only 120 MB, they most likely have a 32-bit windows client JVM which can't be 2 GB. If they have 32 GB or more they might want more than 2 GB.
BTW Gb = Giga-bit, Mb = Mega-bit, GB = Giga Byte. MB = Mega Byte.
Upvotes: 1
Reputation: 4693
Heap is able to shrink after GC iteration if GC decides that there are too many unused heap space allocated. Maybe that is way after your application starts it shows you the same size each time.
And also -Xms seems to set initial size of heap, not minimum
C:\Users\AStybaev>java -X
...
-Xms<size> set initial Java heap size
...
Upvotes: 0
Reputation: 121998
Since the values
must be set during JVM initialization
,You cannot.
Some Useful discussion on the same on oracle forums.
And on SO :programatically setting max java heap size
Upvotes: 0
Reputation: 22710
No, You can't change the heap size programatically. Command line is the only way
Upvotes: 0