java 直接内存,在Java中使用直接内存的目的是什么?

Direct memory was introduced since java 1.4. The new I/O (NIO) classes introduced a new way of performing I/O based on channels and buffers. NIO added support for direct ByteBuffers, which can be passed directly to native memory rather than Java heap. Making them significantly faster in some scenarios because they can avoid copying data between Java heap and native heap.

I never understand why do we use direct memory. Can someone help to give an example?

解决方案

The JVM relies on the concept of garbage collection for reclaiming memory that is no longer used. This allows JVM language developers (e.g., Java, Scala, etc) to not have to worry about memory allocation and deallocation. You simply ask for memory, and let the JVM worry about when it will be reclaimed, or garbage collected.

While this is extremely convenient, it comes with the added overhead of a separate thread, consuming CPU and having to go through the JVM heap constantly, reclaiming objects that are not reachable anymore. There's entire books written about the topic, but if you want to read a bit more about JVM garbage collection, there's a ton of references out there, but this one is decent: https://dzone.com/articles/understanding-the-java-memory-model-and-the-garbag

Anyway, if in your app, you know you're going to be doing massive amounts of copying, updating objects and values, you can elect to handle those objects and their memory consumption yourself. So, regardless of how much churn there is in those objects, those objects will never be moved around in the heap, they will never be garbage collected, and thus, won't impact garbage collection in the JVM. There's a bit more detail in this answer: https://stackoverflow.com/a/6091680/236528

From the Official Javadoc:

Direct vs. non-direct buffers

A byte buffer is either direct or non-direct. Given a direct byte

buffer, the Java virtual machine will make a best effort to perform

native I/O operations directly upon it. That is, it will attempt to

avoid copying the buffer's content to (or from) an intermediate buffer

before (or after) each invocation of one of the underlying operating

system's native I/O operations.

A direct byte buffer may be created by invoking the allocateDirect

factory method of this class. The buffers returned by this method

typically have somewhat higher allocation and deallocation costs than

non-direct buffers. The contents of direct buffers may reside

outside of the normal garbage-collected heap, and so their impact upon

the memory footprint of an application might not be obvious. It is

therefore recommended that direct buffers be allocated primarily for

large, long-lived buffers that are subject to the underlying system's

native I/O operations. In general it is best to allocate direct

buffers only when they yield a measureable gain in program

performance.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值