Check if JVM is 32 or 64 Bit



In this article, we will learn how to determine whether the installed JVM in your system is 32 bit or 64 bit. For this operation, we use the getProperty() method of System class, which helps in retrieving system property of specified argument.

In computer architecture, 32 bit and 64 bit refers to those components that operate on data in 32-bit units and 64-bit units respectively. 64 bit machines are said to be faster and more secure compare to the 32 bit machines.

What is JVM?

JVM is Java Virtual Machine that is responsible for executing the byte code. It is a part of Java Runtime Environment (JRE). The Java programming language is platform independent but the JVM is platform dependent. We need separate JVM for every OS. If we have byte code of any java source code, we can run it on any platform easily because of JVM. The whole process of execution of a java file is as follows ?

  • First, we save java source code with extension .java that is converted by compiler to byte code with extension .class. This happens at the compile time.

  • Now at the runtime, JVM reads and verifies byte code, allocates memories to variables and then, converts that byte code to machine readable form.

How to Check if JVM is 32 or 64 bit?

We can check if JVM is 32 or 64 bit by using the following ways ?

  • Using os.arch property
  • Using sun.arch.data.model property
  • Using Command Line Interface

Using os.arch

The os.arch property of Java System class returns a string that represents architecture of the Java Virtual Machine (JVM).

Example

In the below Java program, we are checking if JVM is 32 bit or 64 bit by passing os.arch property as a parameter to the getProperty() method.

public class Main {
   public static void main(String[] args) {
      String info = System.getProperty("os.arch");
      System.out.println(info + "-bit JVM is installed in your device ");
   }
}

On running this code, it will produce the following result ?

amd64-bit JVM is installed in your device

Using sun.arch.data.model

If we pass sun.arch.data.model property as a parameter value to the getProperty() method, it will describe the configuration of the current JVM installed in your system.

Example

The following Java program demonstrates how to verify if JVM is 32 bit or 64 bit by using sun.arch.data.model property.

public class Main {
   public static void main(String[] args) {
      String info = System.getProperty("sun.arch.data.model");
      if (info.equals("64")) {
         System.out.println(info + "-bit JVM is installed in your device ");
      } else {
         System.out.println(info + "-bit JVM is installed in your device ");
      }
   }
}

On executing, this code generates following output ?

64-bit JVM is installed in your device

Using Command Line Interface

Open cmd in your device and type java --version. When you press enter, you will see the result depending on the configuration of your device ?

C:\Users\Lenovo>java --version
java 17.0.6 2023-01-17 LTS
Java(TM) SE Runtime Environment (build 17.0.6+9-LTS-190)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.6+9-LTS-190, mixed mode, sharing)

Conclusion

JVM is platform dependent machine that converts byte code to machine readable format. The main difference between the 32 bit and 64 bit JVM is the memory size limit. We can specify a maximum of 4 GB in case of 32 bit but 64 bit has much more capacity. In this article, we have discussed two java programs to check if JVM is 32 or 64 bit.

Updated on: 2024-08-01T11:01:47+05:30

796 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements