In a previous article we covered Java runtime benchmarks using various JVMs. Typical benchmark testing runs a series of varied task-based tests that attempt a wide array of coverage. While these benchmark tests are great for generalized comparisons, I was curious how fast Java programs could update/write to the GPIO pins. So this article is a very coarse-grained comparison on a single data point ... frequency of GPIO state changes.
The test program used simply toggles the GPIO state between HI and LOW states as fast as it can by executing in a tight continuous loop. Measurements were made using a digital oscilloscope (DSO) with the probe attached to GPIO pin 1.
Additionally ...
The following (single) snapshot reading was sampled from the test program while running it on the OpenJDK with the ZeroVM. The GPIO pin was able to toggle states at a frequency of approximately 5 kHz.
The following (single) snapshot reading was sampled from the test program while running it on the OpenJDK with the JamVM. The GPIO pin was able to toggle states at a frequency of approximately 10.75 kHz, a notable improvement over the ZeroVM.
I did attempt to run the test program using the Avian VM with OpenJDK; however the test program failed to start. This error message was displayed on screen: "NATIVE (GpioInterrupt) ERROR; GpioInterrupt class not found." It appears there is some issue loading the native JNI library.
The following (single) snapshot reading was sampled from the same test program while running it using Oracle's JDK SE 8 Preview for Linux/ARM. (Installing Oracle Java SE 8 (with JavaFX) Developer Preview for ARM) The GPIO pin was able to toggle states at a frequency of approximately 153 kHz. The VM really seems to make a difference here.
For reference, I wanted to also include a native C test program for comparison. The following readings were sampled from a native sample test program that implements the same test logic. Access to the GPIO is accomplished using Gordon's WiringPi library.
The GPIO pin was able to toggle states at a frequency of approximately 7 MHz.
Below is a summary of the results:
If you are using Java and need hi-speed IO changes, then running on the Oracle JVM seems to be the best option at this time. If you need really hi-speed IO, then you may want to think of pushing that work down into a native library. It you require very precise timing, then please keep in mind that Linux is not a real time operating system.
Hopefully this information sheds a little light, albeit on just one key metric, on how the different JVMs perform on the Raspberry Pi.
Here is a great article that performs similar GPIO frequency testing on other languages/configurations:
http://codeandlife.com/2012/07/03/benchmarking-raspberry-pi-gpio-speed/
Per request, I have added the measurements for Oracle JDK 7u10. One difference here is that I used Debian "Wheezy" and not Raspbian since this version of the JDK is does not support hard-float ABI. This test The GPIO pin was able to toggle states at a frequency between ~150 to ~162 kHz. So this was very close to the same results as Oracle's JSK 8 (ea).