Difference between 32-bit and 64-bit JVM in Java?
本文最后更新于:2022年12月29日 早上
One of the rather new or latest Java interview question is, “What is the difference between 32-bit and 64-bit JVM? or, How do you choose between 32-bit vs 64-bit JVM? or When to use 32-bit or 64-bit JVM? Some interviewer also use this concept to trick candidates by asking what would be the size of int variable in 32-bit and 64-bit Java? As mentioned in 130+ Java questions from last 5 years. Another tricky questions is, does same Java program can run on both 32-bit and 64-bit JVM? Short answer of this question is that Java is platform independent and same Java program will run absolutely fine in both 32-bit and 64-bit JVM.The real difference comes in addressable memory orheap memory. In 32-bit JVM, the theoretical limit ofmaximum heap sizeis around 4G but practically you get way less than that e.g. around 1.5 GB in Windows and close to 3 GB in Solaris, see here why it’s like that.
Choosing a 64-bit JVM allows you to increase heap size beyond 4GB as addressable memory is way more now. But, it’s not all rosy because choosing 64-bit JVM and larger heap also has some disadvantages in terms of time required by Garbage Collectors and more memory needed by OOP pointers.
Difference between 32-bit and 64-bit JVM (Java Virtual Machine)
As I said before, main difference between these two JVM comes from addressable memory or heap size. In 32-bit JVM, the maximum addressable memory is 4GB but its way more in 64-bit JVM, around 2^64. All other differences derives from this fact only.
1.Bigger Heap Memory
64-bit JVM has bigger heap than 32-bit JVM. Maximum heap size for 32-bit JVM is much smaller than 64-bit JVM. theoretically its 4GB but practically on average its around 2GB. Specifying more heap will throw error JVM cannot created, invalid heap size.
2.OOP Pointer Size
OOP pointer size gets doubled in 64-bit JVM but you can use -XX:+UseCompressOops option to reduce the size of OOP pointer. This is the reason why same Java program will take more memory in 64-bit JVM than in 32-bit JVM.
Increased size of OOP pointer also means that less data can be cached in register that’s why its very important to enable -XX:+CompressOop JVM option when moving to 64 bit JVM.
3.Long GC Pause
Due to larger heap, GC pause time may increase, so the size of JVM heap must be decided properly to strike a balance between memory required and GC pause time.
Similarities between 32-bit and 64-bit JVM
As I said before, since Java is platform independent, same Java program will run fine in both 32-bit and 64-bit JVM. Java specification is also same for both 32-bit and 64-bit JVM e.g. size of all primitive variable like int, short, double, long and float is same i.e. int variable will take 4 bytes in both 32 and 64-bit JVM.
There is nothing like 32 or 64-bit Java. Though installation package for 32 and 64-bit JDK is different but Java specification is same. So, if you someone tries to trick you by saying that size of int variable will be double in 64-bit JVM then you can say no.
That’s all about difference between 32-bit and 64-bit JVM in Java. As I said, main difference is large heap memory available on 64-bit Java Virtual Machine and most people switch to 64-bit JVM for 2 reasons, a large heap and they are running their program on a 64-bit operating system.
Due to larger heap, Garbage collection can be long and that’s why you should allocate enough memory for your application to grow and don’t need to trigger Garbage collection until restart window kicks in. For example if your application is daily or weekly restart then just give it enough memory to run for the week without GC.