How I make my Java programs run faster on my Raspberry Pi 3

When my first attempt to install GitBucket on my Raspberry Pi 3 highlighted to me that it was the JVM that caused GitBucket to have sluggish performance, I did not invest more time to improve the performance of the JVM that I had installed on my Raspberry Pi 3. Instead, I went on to install Go Git Service on my Raspberry Pi 3 to act as the Git server to manage the source codes of my hobby projects.

However, I just couldn't get over my belief that Java programs would perform badly on my Raspberry Pi 3; after all Java was the first programming language that I learnt and I often use it to build software at work. Furthermore, with an Ubuntu Server 15.10.3 setup on my Raspberry Pi 3, I could use my Raspberry Pi 3 as an integration server for my Java hobby projects and be pretty sure that if my Java hobby projects run on my Raspberry Pi 3, they will most probably run on a Ubuntu Server 15.10.3 DigitalOcean droplet as well.

After spending some time researching the topic, I did manage to make my Java programs run around 10 times faster on my Raspberry Pi 3. This post documents the steps that I went through to speed up my Java programs on my Raspberry Pi 3.

Using another Java Virtual Machine to run my Java programs

In my first attempt to get GitBucket running, I had installed OpenJDK 8 to provide for the underlying Java Virtual Machine. However, there is another Java Virtual Machine from Oracle that I could try running my Java programs on. With that information in mind, I decided to try running my Java programs with the Oracle Java Virtual Machine on my Raspberry Pi 3.

Installing Oracle Java 8 on my Ubuntu Server 15.10.3 image on my Raspberry Pi 3

To install Oracle Java 8 on my Ubuntu Server 15.10.3 image on my Raspberry Pi, I ran the following command:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update 
sudo apt-get -y install oracle-java8-installer

After the installation had completed, I verified my Java Virtual Machine version by running the following command:

java -version

which returned me the following output:

java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) Client VM (build 25.101-b13, mixed mode)

This meant that the Oracle Java Virtual Machine would be used for running Java programs on my Raspberry Pi 3.

I then ran one of my Java hobby programs and was able to notice the significant improvement in performance.

Switching default Java runtime on Ubuntu Server 15.10.3 on my Raspberry Pi 3

For me, the Oracle Java 8 installer set its own runtime as the default Java runtime on my Raspberry Pi 3. In case when it doesn't, you can run the following command to set the default java runtime to Oracle Java runtime:

sudo update-alternatives --config java

When I ran this command, I got the following output:

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

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-8-oracle/jre/bin/java          1064      auto mode
  1            /usr/lib/jvm/java-8-openjdk-armhf/jre/bin/java   1063      manual mode
* 2            /usr/lib/jvm/java-8-oracle/jre/bin/java          1064      manual mode

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

To further verify that the Oracle Java 8 runtime runs Java programs faster than the OpenJDK 8 runtime, I selected option 1 and ran my Java programs again. When I did that, I could observed the decrease in the performance of my Java programs.

With that verification, I ran the update-alternatives command again to switch the default Java runtime back to the Oracle Java 8 runtime.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.