Over a million developers have joined DZone.

MineCraft and Off Heap Memory

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

MineCraft is a really good example of when off heap memory can really help.
The key requirement 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 is 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
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
There is still some premature promotion i.e. further improvements can be made but you can see that the application is spending.


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.


Many organisation 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 issue are more likely to be fix. c.f.https://bugs.mojang.com/browse/MC-56447

The Source Used

The source used for the test is available here. https://github.com/peter-lawrey/MineOffHeap

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.


Published at DZone with permission of Peter Lawrey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}