views:

771

answers:

5

Hello, everyone

I know Java VM has "-XMx" and "-XMs" for setting the size of the heap. It also has a feature called "ergonomics", that can intelligently adjust the size of the heap. But, I have a problem at hand requiring the heap with strictly fixed size.

Here is the command line arguments:

"-Xms2m -Xmx2m -XX:+PrintGCDetails"

However, by observing the GC logs, it seems the size of the heap was not fixed at 2048K. See, e.g. 2368K, 2432K, 2176K, etc:

[GC [PSYoungGen: 480K->72K(704K)] 1740K->1332K(2368K), 0.0032190 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[GC [PSYoungGen: 560K->64K(768K)] 2094K->1598K(2432K), 0.0033090 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[GC [PSYoungGen: 544K->32K(768K)] 1675K->1179K(2176K), 0.0009960 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]

Is there a way to do the "strict sizing" (no more no less) of a Java heap?

A: 

As far as I know this is not possible using the standard VM. Unfortunately I have no references at hand right now.

Jan Jungnickel
Thanks, Jan. If there is no easy way out, I will have to dig into the source code.
peng
A: 

There are other options along with -Xmx and -Xms that determine the initial heap size. Check the jvm tuning guide for details.

+3  A: 

I believe the JVM will manage the heap as you intend, but the problem in your example is that the max heap size is effectively ignored as too low.

On Sun's Windows JVM version 1.6.0_06 I believe the minimum 'max heap size' is approximately 6MB (i.e. -Xmx6m). If you attempt a setting lower then this the heap may actually grow larger. (I had thought the minimum was 16m but a little experimentation shows that values as low as 6m appear to work.)

If you set -Xms8m and -Xmx8m, however, I think you'll find the heap stays at that size.

Jeremy Rishel
A: 

I guess it is just too small. Try something higher, like 16m or 64m. Additionally the internal and the external size are different shoes. The heap will not be full all the time, so a less than Xmx is always possible, even a less than Xms in case the program just has been started. But externally, you will see that Xms amount of memory has been allocated.

ReneS
A: 

When I first read that I thought it said 2 GB ;)

Don't forget Java uses at least 10 - 30 MB of non heap space so the few hundred K you save might not make as much difference as you think.

Peter Lawrey