Home » Java » Core Java » When using direct memory can be faster

About Peter Lawrey

Peter Lawrey

When using direct memory can be faster


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.

(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
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 our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

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

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
6 years ago

Thank you for the post, a question, is this a better way to load into memory BIG files? Instead of Typical file object?