When using direct memory can be faster

Overview

Using direct memory is no guarantee of improving performance.  Given it adds complexity, it should be avoided unless you have a compelling reason to use it.

This excellent article by Sergio Oliveira Jr shows its not simply a matter of using direct memory to improve performance Which one is faster: Java heap or native memory?

Where direct memory and memory mapped files can help is when you have a large amounts of data and/or you have to perform some IO with that data.

Time series data.

Time series data tends to have both a large number of entries and involve IO to load and store the data.  This makes it a good candidate for memory mapped files and direct memory.

I have provided an example here;  main and tests where the same operations are performed on regular objects and on memory mapped files.  Note: I am not suggesting that the access to the Objects is slow but the overhead of using objects which is the issue. e.g. loading, creating, the size of the object headers, garbage collection and saving the objects.

The test loads time series data with the time and two columns, bid and ask prices (normalised as int values) This is used to calculate and save a simple mid price basis point movement.  The test performs one GC to include the overhead of managing the objects involved.

Storage1 million10 million30 million100 million250 million
Objects  0.55s  4.4s  16.7s  86.7s  225s
MMap  0.056s  0.46s    1.3s    4.5s     11s

The full results

Not only is memory mapped data 10x faster for smaller data sets, it scales better for large data sizes because

  • Memory mapped data is available as soon as it is mapped into memory
  • It creates only a small number of objects, which has almost no heap foot print reducing GC times.
  • It can be arranged in memory as you desire reducing the over head per row as it doesn’t have an object per row.
  • It doesn’t have to do anything extra to save the data.

Using direct memory and memory mapped files is not as simple as using Java objects, but if you have big data requirements it can make a big difference.

Using direct memory and memory mapped files can also make a big difference for low latency requirements, something I have discussed in previous articles.
 

Reference: When using direct memory can be faster from our JCG partner Peter Lawrey at the Vanilla Java blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


5 × = twenty



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close