Java - 堆与直接内存访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22332990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Heap vs Direct memory access
提问by Bober02
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.
我最近遇到了sun.misc.Unsafe
类,允许用户以类似于 C 中的类似方式分配、解除分配和一般访问内存。我阅读了一些解决这个问题的博客,例如
- Which is faster - heap or direct memory- test results claim heap
- Off-heap memory vs DirectByteBuffer vs Heap- Off-heap seems to be fastest
- Memory mapped files for time series data-
MappedByteBuffer
faster than heap objects
- 哪个更快 - 堆或直接内存- 测试结果声称堆
- Off-heap memory vs DirectByteBuffer vs Heap- Off-heap 似乎是最快的
- 时间序列数据的内存映射文件-
MappedByteBuffer
比堆对象更快
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.
第 1 条)似乎与其他条相矛盾,我不明白为什么。DirectMemoryBuffersun.misc.Unsafe
在幕后使用(也是如此MappedByteBuffer
),因此它们也应该受到文章 1 中描述的 JNI 调用的影响。此外,在文章 2 中,堆外内存访问与文章 1 中的类似,并给出完全相反的结果。
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.
有人可以评论如何继续使用堆外内存,即何时使用它,它是否有显着的好处,最重要的是,为什么类似的主题会根据上述文章给出截然不同的结果?谢谢。
回答by MGot90
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.
1)。在 Java 中使用本机内存有其用途,例如当您需要处理大量数据(> 2 GB)或想要从垃圾收集器中逃脱时。然而,就延迟而言,从 JVM 直接访问内存并不比访问堆快,如上所示。结果实际上是有道理的,因为跨越 JVM 障碍必须付出代价。这与使用直接或堆 ByteBuffer 之间的困境相同。直接 ByteBuffer 的速度优势不是访问速度,而是直接与操作系统的本机 I/O 操作对话的能力。Peter Lawrey 讨论的另一个很好的例子是在处理时间序列时使用内存映射文件。
Source: http://mentablog.soliveirajr.com/2012/11/which-one-is-faster-java-heap-or-native-memory/
资料来源: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.
2)。堆外通过 Unsafe 以 330/11200 百万/秒的速度飞快。所有其他类型的分配的性能要么适合读取,要么适合写入,没有一种分配适合两者。关于 ByteBuffer 的特别说明,它很可怜,我相信你看到这样的数字后不会使用它。DirectBytebuffer 读取速度很慢,我不知道为什么它这么慢。所以如果内存读/写成为你系统中的瓶颈,那么肯定是 Off-heap 是要走的路,记住它是高速公路,所以要小心驾驶。
Soruce: http://www.javacodegeeks.com/2013/08/which-memory-is-faster-heap-or-bytebuffer-or-direct.html
来源:http: //www.javacodegeeks.com/2013/08/which-memory-is-faster-heap-or-bytebuffer-or-direct.html