juacala
juacala

Reputation: 2235

Valid values of -Xmx flag in java

In every example I see of the -Xmx flag in java documentation (and in examples on the web), it is always a power of 2. For example '-Xmx512m', '-Xmx1024m', etc.

Is this a requirement, or is it a good idea for some reason?

I understand that memory in general comes in powers of 2; this question is specifically about the java memory flags.

Upvotes: 8

Views: 4988

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533500

It keeps things simple, but it is more for your benefit than anything else.

There is no particular reason to pick a power of 2, or a multiple of 50 MB (also common) e.g. -Xmx400m or -Xmx750m

Note: the JVM doesn't follow this strictly. It will use this to calculate the sizes of different regions which if you add them up tends to be lightly less than the number you provide. In my experience the heap is 1% - 2% less, but if you consider all the other memory regions the JVM uses, this doesn't make much difference.

Note: memory sizes for hardware typically a power of two (on some PCs it was 3x a power of two) This might have got people into the habit of thinking of a memory sizes as a power of two.

BTW: AFAIK, in most JVMs the actual size of each region is a multiple of the page size i.e. 4 KB.

Upvotes: 6

Related Questions