Wednesday, July 28, 2021

What is the maximum Heap Size of 32 bit or 64-bit JVM in Windows and Linux?

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. The confusion starts here because you can not really set 4GB as the maximum heap size for 32 bit JVM using -Xmx JVM heap options. You will get could not create the Java virtual machine Invalid maximum heap size: -Xmx  error. There could be much different reasons why maximum heap space for JVM is less than its theoretical limit and varies from one operating system to other e.g. different in Windows, Linux, and Solaris.

I have seen a 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 up to 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

maximum Java heap space memory in Java for 32-bit and 64-bit JVMHere is a list of some confusions I have seen on Java programmers regarding maximum heap space of 32 and 64 bit Java Virtual Machines :
  1. What is the maximum heap size for 32 bit JVM?  2GB or 4GB?
  2. Why my JVM not able to start on windows when maximum heap space around 1600M?
  3. Why Linux or Solaris allow more maximum heap size than windows for same, 32 bit JVM?
  4. Can we set more than 4GB as maximum heap size for 32 bit JVM running on 64 bit or x64 operating system?
  5. What is maximum heap size for 64 bit or x64 JVM, Is it 8GB or 16GB?
  6. 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 it's 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 a sign bit, many programmers 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 a windows machine. But this is wrong. Memory is nothing to do with a signed or unsigned bit as there is no negative memory address. So the theoretical limit for maximum heap size on 32 bit JVM is 4GB and for 64 bit JVM it's 2^64.

Why JVM not able to start on Windows XP when maximum heap space around 1600M?
This problem is most obvious on Windows platforms 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 an error as.

Could not create the Java virtual machine.
Invalid initial heap size: -Xms1.5G

Remember, this limit on heap space is due to the 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 the 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.

Why Linux or Solaris allow more maximum heap size than windows for same, 32 bit JVM?
This point is also related to the second. Though there could be multiple reasons for that I think It could be because of Windows trying to allocate a 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 the x64 server. In my opinion, you can set up to 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 up to 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 the maximum heap size as 4G but it didn’t accept. Solaris goes closer to 4GB by allowing up to 3.6G (approx).


What is maximum heap size for 64 bit or x64 JVM, Is it 8GB or 16GB?
This question mostly arises because of available physical memory on the machine. As no system currently have 2^64 bit of physical memory or RAM and often high-end servers have memory around 8G, 16GB or 32GB. Theoretical maximum memory for x64 machines is 2^64 bit but again it depends on how much your operating systems allow. I read somewhere that Windows allowed a 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 the operating systems 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 then it depends on 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 the host operating system. Solaris and Linux provide more heap space than windows and that could be one of the many reasons that Java Server application mostly runs on UNIX based systems. Let me know what’s your thought and experience  on maximum Java heap space for  x86 and x64 JVM running on both x86 and x64 machines.


Other Java JVM Tutorials from Javarevisited Blog

11 comments:

  1. 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

    ReplyDelete
  2. How much heap size can be allocated if the physical memory/RAM is 3 GB? I got this question in written test. Can somebody say how much and why ?

    ReplyDelete
  3. 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?

    ReplyDelete

  4. Can any one please provide following details to me, I need this to submit to my project manager

    Default Heap Size for 32 bit JVM in 32-bit Windows
    Maximum Heap Size for 32 bit JVM in 32-bit Windows

    Default Heap Size for 32 bit JVM in 32-bit Redhat Linux
    Maximum HHeap Size for 32 bit JVM in 32-bit Redhat Linux

    Default Heap Size of 32 bit JVM in 32-bit Solaris x86
    Maximum Heap Size of 32 bit JVM in 32-bit Solaris x86

    Default Heap space for 64 bit JVM in 64 bit Windows 2003 Server
    Maximum Heap space for 64 bit JVM in 64-bit Windows 2003 Server

    Default Heap space for 64 bit JVM in 64 bit Linux
    Maximum Heap space for 64 bit JVM in 64-bit Linux

    Default Heap space for 64 bit JVM in 64 bit Solaris x86
    Maximum Heap space for 64 bit JVM in 64-bit Solaris x86

    Thank you

    ReplyDelete
  5. How to reduce java heap space (out of memory) exception... Java heap space is already set to max.. do the unhandled java runtime exception will consume the memory in java heap?

    ReplyDelete
  6. 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.

    ReplyDelete
  7. worth remembering that most 64-bit JVMS run with 32-bit references for heaps up to 32 GiB. In Java 8 up to 64 GiB.

    ReplyDelete
  8. 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.

    ReplyDelete
  9. 2^32 -1 is 4GB, not 2GB. I think that you meant a 32-bit signed int max would be 2^31-1

    ReplyDelete
  10. 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?

    ReplyDelete
  11. And also what is the maximum heap size that we can give for apache tomcat 7 where as am using 64 bit operating sever 32 GB RAM,32 bit JVM

    ReplyDelete