Java - 堆与直接内存访问 [英] Java - Heap vs Direct memory access

查看:377
本文介绍了Java - 堆与直接内存访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了 sun.misc.Unsafe 类,允许用户以类似于C的方式分配,解除分配和一般访问内存。我读了一个一些解决这个问题的博客,例如

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. 哪个更快 - 堆或直接内存 - 测试结果声明堆

  2. 关闭堆内存vs DirectByteBuffer与堆 - 堆外似乎最快

  3. 内存映射时间序列数据的文件 - MappedByteBuffer 比堆对象更快

  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

第1条)似乎与其他人相矛盾,我无法理解为什么。 DirectMemoryBuffer在引擎盖下使用 sun.misc.Unsafe (因此 MappedByteBuffer ),所以他们也应该受到JNI的影响第1条中描述的调用。此外,在第2条中,堆外存储器访问类似于第1条中的访问,并给出完全相反的结果。

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.

推荐答案

1)。使用Java的Native内存有其用途,例如当你需要使用大量数据(> 2 GB)或者
想要从垃圾收集器中逃脱时。但是就
延迟而言,来自JVM的直接内存访问速度并不比访问堆的
快,如上所示。结果实际上是
感,因为越过JVM屏障必须有成本。这是使用直接或堆ByteBuffer之间的
相同的困境。直接ByteBuffer的速度
优势不是访问速度,而是直接与操作系统的本机I / O
操作对话的
能力。 Peter Lawrey讨论的另一个很好的例子是在处理时间序列时使用内存映射文件的

来源: http://mentablog.soliveirajr.com / 2012/11 / one-is-fast-java-heap-or-native-memory /

2)。通过Unsafe离开堆快速燃烧速度为330/11200百万/秒。
所有其他类型的分配的性能要么适合读取,要么写入,没有一个分配对两者都有好处。
关于ByteBuffer的特别说明,这很可怜,我相信在看到这样的号码后你不会使用它。 DirectBytebuffer读取速度很慢,我不知道为什么它会如此慢。所以如果内存读/写正在成为你系统的瓶颈,那么绝对是堆外的方式,记住它是高速公路,所以小心驾驶。

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

这篇关于Java - 堆与直接内存访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆