MineCraft and off heap memory

Overview

MineCraft is a really good example of when off heap memory can really help.
The key requirements are:
  • The bulk of the retained data is a simple data structure (in minecraft’s case its lots of byte[])
  • The usage of off heap memory can be hidden in abstraction.

 
 

The test

I used the following test for starting minecraft server from a seed from scratch which is a particularly expensive operation for the server.
  • Preset the level-seed=114 in server.properties
  • Delete the world* directories
  • Start the server with these options to see what the GC is doing -Xloggc:gc.log
    -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps
    -XX:+PrintGCTimeStamps -XX:+PrintTenuringDistribution -Xmx6g
  • Connect with one client
  • Perform /worldgen village
  • Perform /save-all
  • Exit.
To analyse the logs I am using jClarity’s Censum.

Standard MineCraft

There are two particularly expensive things it does:
  • It caches block stage in many byte[]s
  • It attempts to cache int[] used for processing without limit.

A Censum report for the above test looks like this:

censum-original-perc
The high pause times are partly due to having to manage the large objects.

Off heap MineCraft

Two changes were made to address this:
  • Use off heap ByteBuffer for long term caching.  Unsafe would be more efficient but not as portable.
  • Put a cap on the number of int[] cached.

Note: the problem with the temporary int[] only became visible to me after moving the bulk of the data off heap. Addressing the biggest problem over reveals more quick fix problems.

A Censum report for the same test looks like this:

 

censum-offheap-perc
There is still some premature promotion i.e. further improvements can be made but you can see that the application is spending.

Conclusion

Using off heap memory can help you tame you GC pause times, especially if the bulk of your data is in simple data structures which can be easily abstracted.  Doing so can also help reveal other simple optimisations you can do to improve the consistency of your performance.

Footnote

Many organisations treat performance as an enhancement and optional, however if you instil a culture where reasonable performance is a requirement, and failing to meet this requirement is a bug, performance issues are more likely to be fixed. c.f. https://bugs.mojang.com/browse/MC-56447

The Source Used

Reference: MineCraft and off heap memory 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


seven × = 7



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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