Reputation: 7488
I have the following Java program that I don't expect to need much java heap:
public class SleepTest {
public static void main(String[] args) {
while (true) {
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
}
}
}
}
Therefore I try to inform this to the JVM by specifying an initial heap size using the Xms
setting. However it seems that the JVM just ignores this and allocates with the default size. Why is this ?
To illustrate I start my program three times:
abc@yyy:~> java SleepTest & // default - 512 MB
[1] 15745
abc@yyy:~> java -Xms10M SleepTest & // specifying initial size of 10 MB
[2] 15756
abc@yyy:~> java -Xmx10M SleepTest & // forcing max heap size of 10 MB
[3] 15766
And investigates the memory consumption in Linux:
abc@yyy:~/ebbe> grep VmSize /proc/15745/status /proc/15756/status /proc/15766/status
/proc/15745/status:VmSize: 688828 kB // default
/proc/15756/status:VmSize: 688892 kB // specifying initial size of 10 MB
/proc/15766/status:VmSize: 166596 kB // forcing max heap size of 10 MB
Here the size of process 15756 indicates that it just allocated the same as default.
I'm using the following JVM:
java version "1.6.0"
Java(TM) SE Runtime Environment (build pxa6460sr16fp1-20140706_01(SR16 FP1))
IBM J9 VM (build 2.4, JRE 1.6.0 IBM J9 2.4 Linux amd64-64 jvmxa6460sr16-20140626_204542 (JIT enabled, AOT enabled)
J9VM - 20140626_204542
JIT - r9_20130920_46510ifx7
GC - GA24_Java6_SR16_20140626_1848_B204542)
JCL - 20140704_01
And I'm running on
SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 3
Upvotes: 0
Views: 1093
Reputation: 20059
The VM does honor the -Xms hint, but if you set it too low, the heap will immediately grow to satisfy allocation requests.
You conclude your code "doesn't need much memory" based on what's in your code. You do not take into account the memory consumed by the startup of the VM/JRE. Beware that due to the dependencies the VM has to load a lot of the JRE classes before it even begins executing your code.
The -Xms option is meant to ensure that the VM allocates at least as much heap, its the lower limit for the heap size. The VM is always free to allocate more. Thats where -Xmx comes into the picture (to define the upper limit).
Upvotes: 2