java直接访问内存,Java - 堆与直接内存访问

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.

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). 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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值