24 Feb 2016

Raspberry Pi: Change Java JVM – OpenJDK / Oracle

Source code

When I first began using the Raspberry Pi to run Java applications, Oracle had yet to release a Java virtual machine that utilised ARM hard floats (hardware accelerated floating-point math processing). This meant that the Oracle JVM had to do all of this arithmetic in software causing excessive CPU load and poor performance. It was for this reason I decided to use the OpenJDK virtual machine which did use hard float and was much more efficient. Since then I’ve had no real reason to try anything else.

Well, a few years have gone by, the Oracle JVM now supports hard float, and OpenJDK JVM is giving me more issues with complex applications (virtual machine crashes). I’ve decided to change my JVM back to Oracle, and make a quick guide in process 🙂

Check your current Java version

To see what version of Java you are currently running, use the following command:

java -version

The following response shows that I’m currently running OpenJDK virtual machine:

java version "1.7.0_65"
OpenJDK Runtime Environment (IcedTea 2.5.3) (7u71-2.5.3-2~deb7u1+rpi1)
OpenJDK Zero VM (build 24.65-b04, mixed mode)

Installing Oracle Java

To install the Oracle JVM, use the following commands. First we’re updating apt-get so it’s aware of the latest software versions, then we install Java:

sudo apt-get update
sudo apt-get install oracle-java7-jdk

Installing OpenJDK Java

To instead install the OpenJDK JVM, use these commands:

sudo apt-get update
sudo apt-get install openjdk-7-jdk

Selecting which Java version to use

There’s no need to uninstall anything, instead we can just select the default version of Java to use with this command:

sudo update-alternatives --config java

This will list the available versions with an asterisk next to the default. Enter the number for the desired default Java (in my case ‘2’).

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-armhf/jre/bin/java   1043      auto mode
  1            /usr/lib/jvm/java-7-openjdk-armhf/jre/bin/java   1043      manual mode
  2            /usr/lib/jvm/jdk-7-oracle-armhf/jre/bin/java     317       manual mode

Press enter to keep the current choice[*], or type selection number: 2

Now we can confirm this change by running this command again:

java -version

You can see I now have Oracle Java displayed:

java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) Client VM (build 24.0-b56, mixed mode)

That’s all that was required for me to switch out my Java virtual machine!

Performance comparison

This is just a quick comparison with one of my Java applications:

Java versionCPUMemory
OpenJDK34.7 %10.9 %
Oracle15.7 %10.7 %

You can see switching to Oracle Java gave me a huge increase in performance! Hopefully I also get less JVM crashes now too.

About the Author:

Hardware and software engineer with experience in product development and building automation. Director at Cabot Technologies and Product Manager at NEX Data Management Systems.

2 comments

  1. Earl

    Hi

    While its good to compare 2 JVM side by side, it would be nice to note the boundaries of your test scenario.

    • That’s a good point. In general terms, it’s a multi-threaded data acquisition application with no GUI. So lots of serial and IP communications, lots of database access, and no graphics (headless service).

Leave a Reply

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.