Sunday, January 22, 2012

How to find if JVM is 32 or 64 bit from Java program.

You can find JVM bit size e.g. 32 bit or 64 bit by using either running java command from command prompt or by using System.getProperty() from Java program. Question is why do you want to know hardware or platform configuration while writing Java code which is supposed to write once and read anywhere(32 bit, 64 bit etc)? Yes we don't really need to know whether JVM is 32 bit or 64 bit more often but there are many situation when this matters


Check if JVM is 32 or 64 bit from Java program:
1)Since in 32 bit JVM maximum heap size in Java can not be more than 4GB (theoretically) , if you can get JVM version from script like running java command you can have different memory configuration for your application. Also if there is any specific JVM options which only applicable to 64 bit JVM than you can set those.

2)If your Java application is using native libraries than you certainly want to know whether Java running on host is 32 bit or 64 bit because native library may have different versions loaded for 32 bit or 64 bit architectures.

I am sure there could me some more practical reasons when you like to find JVM bit size or whether JVM is 64 bit or not

Now let's come to second part how to find if JVM is 32 bit or 64 bit in Java.

How to check if JVM is 32 or 64 bit in host

check if JVM is 32 bit or 64 bit in JavaAs I said earlier there are two different approaches either using Java system property like "sun.arch.data.model" or "os.arch" or by running java command from script and checking its output for certain characters to identify whether JVM is 64 bit or not. let's see example of different ways to find if JVM is 32 bit or 64 bit:

1) By using System property sun.arch.data.model:
You can find whether your JVM is 32 bit or 64 bit by calling System.getProperty("sun.arch.data.model") at least on Sun's hotspot JVM. I don't expect this to be run on any other Vendor specific JVM but since most of the programmer or project uses Sun's hotspot JVM. this is an handy option. for 32 bit JVM "sun.arch.data.model" will be 32 and for 64 bit JVM this would be 64. here is an example:

System.out.println("JVM Bit size: " + System.getProperty("sun.arch.data.model"));

Output:
JVM Bit size: 32 //JVM is 32 bit
JVM Bit size: amd64 //JVM is 64 bit

2)By using System.getProperty("os.arch")
"os.arch" is another System property which you can use to find whether installed JRE or JVM is 32 bit or 64 bit. by name it sounds that it will return operating system arch but you can still give it a try. Though I haven't tested on all different JVM, I have read that it can indeed return JVM Bitness.If you try this on 64 bit machine and 32 bit JVM combination than please let us know what does it return. here is what it returns in case of 32 bit JVM:

System.out.println("JVM Bit size: " + System.getProperty("os.arch"));

JVM Bit size: x86 //on 32 bit JVM
JVM Bit size: amd64 //on 64 bit JVM

3)java -d64 -version
This is another way of finding whether installed JRE or JVM is 64 bit or not but unfortunately it
doesn't work in all windows version like Windows XP but it works fine on Windows 7. So you still
use it on Windows7 Machine for finding JVM bitness.

4)java -version
Plain old java -version reveals information about JVM bitness only if installed JRE is 64 bit, in case
of 32 bit JVM it doesn't provide any information related to architecture but in case of 64 bit JVM it
prints :
C:\>java -version
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)

while in case of 32 bit JVM it will print
C:\> java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)


That’s all on how to find if JVM is 32 bit or 64 bit from Java program and command prompt. As I said its particularly useful if you have dependency on native libraries which has different build for 32 bit or 64 bit architecture. Let me know if you have some more ways to find JVM is 64 bit or not, you can also share on which scenario you need to know JVM bitness.

Some Java posts you may like

9 comments :

soya said...

I wanted know whether my JVM is 64 bit or not and found your tips very relevant. I don't require check if JVM is 32 bit but if any JVM is 64 bit than we do load native .so file from separate location.

Kresten P. Vester said...

Calling System.getProperty("os.arch") on a 64-bit Linux system (OpenSUSE 11.4) with a 32-bit JVM returns: i386

I used Java(TM) SE Runtime Environment (build 1.6.0_22-b04)

Anonymous said...

Hi, We have several JDK and JRE installed on Solaris Sparc box, I want to find out which installed JDK or JRE is 64 bit ? Can you please share command to do that ? Also is it possible to run 32 bit JVM in 64 bit machine i.e. if My Solaris sparc box is 64 bit but JVM is 32 bit can I execute Java program there ?

Anonymous said...

One of the interesting point to know relate to 32 bit or 64 bit installation is that it varies on different environment e.g In windows and Linux you have separate installation for 32 bit and 64 bit JVM but on Solaris machine e.g. Sparc, one JVM installation represent both 32 bit and 64 bit JVM and until specified by -d32 and -d64, they by default run on 32 bit data model. Which means you may not able to assign more than 3.5GB of heap, despite running 64 bit JVM in 64 bit machine. Solution is use -d64 JVM option to run JVM on 64 bit data model.

Dickson Duah said...

How do i solve this error:
Could not create the java virtual machine
A fatal exception occurred. program will exit

$$$$~$$$$ said...

I got "JVM Bit size: x86" for os.arch and "32bit" for sun.arch.data.model on my intel machine with 64 bit windows 7 os.

Anonymous said...

Just change the JRE to JDK 1.7.0_17, you will get the 64 bit JVM.

palak pal said...

A nice tutorial on
JVM Architecture Specification Basic The Heap Area Introduction, teach about the JVM Heap Area in details
http://www.youtube.com/watch?v=c-A7ZzxjWUI

JVM Architecture Specification Basic The Method Area explained, teach about the JVM method area
http://www.youtube.com/watch?v=a5GzF2fSSCE

Anonymous said...

As this question is already answered and explained by Java geeks , I just want to add little extra in it - generally people prefer to switch over 64 but JVM when the existing heap space (1.5 to 1.8 M varies OS to OS) size provided by 32 bit JVM is not suffice for their application , but these days it is very common that people are migrating to 64 bit without understanding the requirement of applications.

http://lotusmediacentre.com/difference-between-32-bit-and-64-bit-jvm-choose-wisely/

Post a Comment