Child pages
  • Ramdisk Java

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Introduction

Putting a JVM in a read only ramdisk file system improve the overall performance of Java on Ubuntu Linux.

It doesn't replace a SSD but rooming 200M of RAM to get some substantial performance is always welcome.

However there are some limitations with this technic:

  • tmpfs is swappable. Under heavy loaded machine our ramdisk could be transferred in a mechanical hard drive.
  • SquashFS is building a highly compressed file, reducing the IO and the size of the ramdisk. Not sure if an unsquashed JVM image would outperform a squashed image.

SquashFS

In order to put a JVM in a RAM disk we are going to use the tool SquashFS.

...

Code Block
languagebash
themeEmacs
sudo apt-get update
sudo apt-get install squashfs-tools

Make a squashfs file out of your desired JVM:, use LZMA2 compression rather than gzip.

Code Block
languagebash
themeEmacs
cd /usr/lib/jvm
sudo mkdir ramdisk
mksquashfs /usr/lib/jvm/java-8-oracle /usr/lib/jvm/ramdisk/java-1.8.0.91-oracle.sqsh -comp xz

At this point you have an existing JVM squashed in .sqsh file with a roughly size of 176M160M:

Code Block
languagebash
themeEmacs
user@machine:/media$ ls --block-size=M -al /usr/lib/jvm/ramdisk/java-1.8.0.91-oracle.sqsh 
-rw-r--r-- 1 user users 176M160M May 12 15:53 /usr/lib/jvm/ramdisk/java-1.8.0.91-oracle.sqsh

Mount Point

Create a mountpoint for the ramdisk:

Code Block
languagebash
themeEmacs
sudo mkdir /media/ramdisk-java-8-oracle

Edit your /etc/fstab and define your mount point as a tmpfs file system and run your squashed JVM against this mount point by adding the following declarations:

Code Block
languagetext
# changedefine a thetmps defaultmount sizepoint for theJava ramdisk
tmpfs /media/ramdisk-java-8-oracle tmpfs defaults,size=350M170M,mode=1777 0 0
# run a squashed JVM against our tmps mount point
/usr/lib/jvm/ramdisk/java-1.8.0.91-oracle.sqsh /media/ramdisk-java-8-oracle squashfs ro,defaults,loop 0 0

If you reboot at this point you will have a ramdisk of 350M defined at /media/ramdisk-java-8-oracle filled with your squashed JVM:

Code Block
languagebash
themeEmacs
user@machine:/media$ du -hs ramdisk-java-8-oracle/
349M ramdisk-java-8-oracle/

That way we know how correctly size what we need for our ramdisk.

350M for a java-1.8.0.91 from Oracle.

Installation

At this point you need to reboot to initialize the ramdisk with your Squashed JVM. Now you have to proceed with a standard JVM installation.

in the /usr/lib/jvm directory we define the default-java to point to a local symlink who will point to our ramdisk:

 

Code Block
languagebash
themeEmacs
user@machine:/usr/lib/jvm$ ls -al
total 92
drwxr-xr-x 7 root root 4096 Jul 13 13:40 .
drwxr-xr-x 205 root root 53248 Jul 13 09:22 ..
lrwxrwxrwx 1 root root 34 Jul 7 15:33 default-java -> /usr/lib/jvm/ramdisk-java-8-oracle
lrwxrwxrwx 1 root root 28 Jul 13 12:15 .default-java.jinfo -> .ramdisk-java-8-oracle.jinfo
lrwxrwxrwx 1 root root 20 Jul 7 11:46 ramdisk-java-8-oracle -> /media/ramdisk-java-8-oracle
-rw-r--r-- 1 root root 3569 Jul 13 10:24 .ramdisk-java-8-oracle.jinfo

...

Then we create a bunch of install and remove shell scripts to update-alternatives either the default-java and the ramdisk-java-8-oracle:.

First we update-alternatives ramdisk-java-8-oracle then default-java and finally update-java-alternatives our default-java.