Pages

Maximum heap size for 32 bit or 64
bit JVM looks easy to determine by looking at addressable memory space like 2^32
(4GB) for 32 bit JVM and 2^64 for 64 bit JVM. Confusion
starts here because you can not really set 4GB as maximum heap size for 32 bit
JVM using -Xmx JVM heap options. You will getcould not create the Java virtual machine
Invalid maximum heap size: -Xmxerror. There could be many different reason why maximum heap space for
JVM is less than there theoretical limit and vary from one operating system to
other e.g. different in Windows, Linux and Solaris. I have seen couple of
comments on my post 10 points on Java Heap Space
regarding what is maximum heap space for Java or 32 bit JVM or 64 bit JVM and
why Windows allows only upto 1.6G memory as maximum heap space etc. In this
Java article, I have collected some of the frequently asked questions around
maximum heap space on both 32 and 64 bit JVM and tried to
explain them.

FAQ Maximum Java Heap Space on 32 and 64 bit JVM

Here is list of some confusions I have seen on Java programmers regarding
maximum heap space of 32 and 64 bit Java Virtual Machines :

What is maximum heap
size for 32 bit JVM?2GB or 4GB?

Why my JVM not able to
start on windows when maximum heap space around 1600M?

Can we set more than
4GB as maximum heap size for 32 bit JVM running on 64 bit or x64 operating system?

What is maximum heap
size for 64 bit or x64 JVM, Is it 8GB or 16GB?

Can I specify more
than 1GB as heap space if physical memory is less than 1GB?

If you also have similar confusion on JVM maximum heap space no
matter whether its for your own Java application or any Java web or application server like Tomcat, JBoss or WebLogic, This discussion applies to all of them.

What is
maximum heap size for 32 bit JVM?2GB or
4GB?

This confusion comes because of sign bit, many programmer think in terms
of signed integer and they think maximum addressable memory (size of address
bus) for 32 bit architecture is 2^32-1 or 2GB
and this confusion is supported by fact that you can not provide maximum
heap space as 2GB on windows machine. But this is wrong. Memory is nothing to
do with signed or unsigned bit as there is no negative memory address. So
theoretical limit for maximum heap size on 32 bit JVM is 4GB and for 64 bit JVM
its 2^64.

Why JVM
not able to start on Windows XP when maximum heap space around 1600M?

This problem is most obvious on Windows platform like Windows XP, which
tries to allocate a contiguous chunk of memory as requested by -Xmx JVM parameters. Windows
reserves some space for his own and seems also allocate memory around half of memory address bar, which consequently reduces contiguous memory space
somewhere less than 2GB, around 1500 to 1600M and when you give more than this
size, JVM throws error as.

Could not create the Java virtual machine.

Invalid initial heap size: -Xms1.5G

Remember, this limit on heap space is due to windows operating system's
own behavior. You can set maximum heap space, more than this size in Linux or
Solaris. Though maximum heap size for 32 bit or 64 bit JVM will always be less
than theoretical limit of addressable memory. By the way you can get this error
due to many reasons, see How to fix Invalid Initial and Maximum heap size in JVM for more details.

This point is also related to
second. Though there could be multiple reasons for that but I think It could be
because of Windows trying to allocate contiguous chunk of memory as Java heap
space. Happy to hear your opinion on this.

Can we
set more than 4GB as maximum heap size for 32 bit JVM running on 64 bit or x64
operating system?

This is a tricky question as you are
running 32 bit JVM on x64 server. In my opinion you can set upto 4GB for 32 bit
JVM but not more than that. Though x64 Servers has more memory for his needs
and since every process can have upto 2^64 bit it may
look perfectly OK for 32 bit JVM to accept 4GB
as maximum heap size. In practice, I have tried both Linux and Solaris
servers setting maximum heap size as 4G but it didn’t accept. Solaris goes more
close to 4GB by allowing upto 3.6G (approx).

What is
maximum heap size for 64 bit or x64 JVM, Is it 8GB or 16GB?

This question mostly arise because of available physical memory on
machine. As no system currently have 2^64 bit of
physical memory or RAM and often high end servers has memory around 8G, 16GB or
32GB. Theoretical maximum memory for x64 machines is 2^64 bit but again its depend on how much your operating systems allows.I read some where
that Windows allowed maximum of 32GB for 64 bit JVM.

Can I
specify more than 1GB as heap space if physical memory is less than 1GB ?

Theoretically yes, because operating system can use virtual memory and
swap pages between physical memory and virtual memory, when there is no room in
physical memory. Practically, if you are running on windows than it depends how
far you can go, I have run Java program with -Xmx1124M even though
my machine has less than 1GB RAM.

That’s all on what is maximum Java heap space for 32 bit and 64 bit
JVM. As you see maximum heap size depends upon host operating system. Solaris and Linux provides more heap space than windows and that could be one
of the many reason that Java Server application mostly run on UNIX based
systems. Let me know what’s your thought and experienceon maximum Java heap space forx86 and x64 JVM running on both x86 and x64
machines.

11 comments
:

A very important point about why you cannot allocate 4GB an only allocate a lot less than 4GB to a java process on a 32-bit machine. The threads created by your java process have their own stackspace and that also has to fit into this 4GB limit (normally due to OS overhead this limit is 3.2GB and not 4GB). Now this default thread-stackspace (Xss) is different on different operating systems and hence the difference between total memory youcan allocate to heap.As I mentioned above, Max Heap = 3.2GB - total thread-stackspace.

One quick way to use more heap on a multi-threaded java app is to reduce your Xss.You can refer my blog for exact numbers.http://oakgreen.blogspot.sg/2011/08/thread-count-supported-by-jvm-on.html

What is the maximum memory limit for a 64 bit JVM? Also I heard that Java Garbage Collectors are not optimized for large heap sizes? Considering that in mind, How much maximum heap size should be without compromising performance?

If you want to get rid of OutOfMemoryException, one immediate thing you can do is to switch to a 64-bit JVM, that will allow you to allocate more memory than your 32-bit JVM and can handle capacity better. Ofcourse, this is only work if you don't have a memory leak, otherwise no mater how much memory you allocate, you will eventually run out with it after some time.

I have doubt from the question 2 you have mentioned that Java runs out of memory if xmx is 1600mb in windows. But in question 5 you have mentioned that OS will use swap memory in harddisk if it crosses the heap memory size.

I have a big doubt am using java 32 bit on a 64 bit operating server.While i trying to extends jvm memory in tomcat multiple instance(tomcat 7),startup.bat is not starting .Can anyone help me out with this issue?