title: Java - Heap vs Direct memory access
body: <p>I recenty came across <code>sun.misc.Unsafe</code> class, allowing user to allocate,deallocate and in general access memory in a similar fashion like in C. I read in a couple of blogs that tackle this issue e.g. </p>
  
  <ol>
  <li><a href="http://mentablog.soliveirajr.com/2012/11/which-one-is-faster-java-heap-or-native-memory/" rel="nofollow noreferrer">Which is faster - heap or direct memory</a> - test results claim heap</li>
  <li><a href="http://www.javacodegeeks.com/2013/08/which-memory-is-faster-heap-or-bytebuffer-or-direct.html" rel="nofollow noreferrer">Off-heap memory vs DirectByteBuffer vs Heap</a> - Off-heap seems to be fastest</li>
  <li><a href="http://vanillajava.blogspot.co.uk/2012/11/when-using-direct-memory-can-be-faster.html" rel="nofollow noreferrer">Memory mapped files for time series data</a> - <code>MappedByteBuffer</code> faster than heap objects</li>
  </ol>
  
  <p>Article 1) seems to be in contradiction with the other ones and I fail to comprehend why. DirectMemoryBuffer is using <code>sun.misc.Unsafe</code> under the hood (so is <code>MappedByteBuffer</code>), so they should also suffer from JNI calls as described in article 1. Also, in article 2, the Off-heap memory accesses resemble the ones in article 1, and give completely opposite results.</p>
  
  <p>Could someone generally comment on how to proceed with Off-heap memory i.e. when to use it, is there a significant benefit to it, and most importantly, why similar subject gives highly different results based on the articles above? Thanks.</p>