Wednesday, December 16, 2020

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

Oracle Java Tutorial and Material, Oracle Java Exam Prep, Oracle Java Guides, Oracle Java Prep, Oracle Java Certification

You can find JVM bit size e.g. 32 bit or 64 bit by using either running java command from the command prompt or by using System.getProperty() from Java program. The 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 situations 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 a 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 then you certainly want to know whether Java running on the 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 be 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 the second part of 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

Oracle Java Tutorial and Material, Oracle Java Exam Prep, Oracle Java Guides, Oracle Java Prep, Oracle Java Certification
As 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 the script and checking its output for certain characters to identify whether JVM is 64 bit or not. let's see an 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 a 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 with 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 the 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 a dependency on native libraries which has a 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.

Related Posts

0 comments:

Post a Comment