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.
MappedByteBuffer
faster than heap objectsArticle 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.
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.
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.
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.
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). 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With