Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Heap vs Direct memory access

I recenty came across sun.misc.Unsafe 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.

  1. Which is faster - heap or direct memory - test results claim heap
  2. Off-heap memory vs DirectByteBuffer vs Heap - Off-heap seems to be fastest
  3. Memory mapped files for time series data - MappedByteBuffer faster than heap objects

Article 1) seems to be in contradiction with the other ones and I fail to comprehend why. DirectMemoryBuffer is using sun.misc.Unsafe under the hood (so is MappedByteBuffer), 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.

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.

like image 961
Bober02 Avatar asked Mar 11 '14 18:03

Bober02


People also ask

What is the difference between heap and memory?

Overview. Stack memory is the space allocated for a process where all the function calls, primitive data types (int, double, etc.) and local and reference variables of the functions are stored. On the other hand heap memory is used to store the objects that are created during the execution of a Java program.

Is JVM memory the same as heap memory?

The Java Virtual Machine has memory other than the heap, referred to as Non-Heap Memory. It is created at the JVM startup and stores per-class structures such as runtime constant pool, field and method data, and the code for methods and constructors, as well as interned Strings.

Is Java a heap of RAM?

The Java heap lives in RAM (ignoring virtual memory :). You can change the default initial heap size and maximum heap size with the -Xms and -Xmx VM args, respectively.

What is the difference between heap memory and stack memory in Java?

The major difference between Stack memory and heap memory is that the stack is used to store the order of method execution and local variables while the heap memory stores the objects and it uses dynamic memory allocation and deallocation.


1 Answers

1). Working with Native memory from Java has its usages such as when you need to work with large amounts of data (> 2 gigabytes) or when you want to escape from the garbage collector. However in terms of latency, direct memory access from the JVM is not faster than accessing the heap as demonstrated above. The results actually make sense since crossing the JVM barrier must have a cost. That’s the same dilema between using a direct or a heap ByteBuffer. The speed advantage of the direct ByteBuffer is not access speed but the ability to talk directly with the operating system’s native I/O operations. Another great example discussed by Peter Lawrey is the use of memory-mapped files when working with time-series.

Source: http://mentablog.soliveirajr.com/2012/11/which-one-is-faster-java-heap-or-native-memory/

2). Off heap via Unsafe is blazing fast with 330/11200 Million/Sec. Performance for all other types of allocation is either good for read or write, none of the allocation is good for both. Special note about ByteBuffer, it is pathetic , i am sure you will not use this after seeing such number. DirectBytebuffer sucks in read speed, i am not sure why it is so slow.So if memory read/write is becoming bottle neck in your system then definitely Off-heap is the way to go, remember it is highway, so drive with care.

Soruce: http://www.javacodegeeks.com/2013/08/which-memory-is-faster-heap-or-bytebuffer-or-direct.html

like image 87
MGot90 Avatar answered Oct 02 '22 14:10

MGot90