NIO IOUtils Direct vs Heap

Andrew Haley aph at
Sun Jul 31 23:59:01 UTC 2016


On 31/07/16 12:25, Jon V. wrote:

> As I’m sure we are all aware that all HeapBuffers must be copied to
> DirectBuffers in order to be written to NIO.  IOUtils provides the
> necessary automation for writing to the FD and working with the BufferCache.
> My question is this: Since we know that byte[] can be passed to JNI
> and safely copied to native memory. (exactly how the heap is copied
> to the native) Why can’t byte[] be written directly to the FD and
> bypass all the nastiness of copying to native memory.

You could use write() directly on the byte array.  But write() is a
blocking system call, so it could block for a very long time.  During
that time the byte array would have to be pinned in memory; it could
not be moved until write() completed.  You'd have a byte array stuck
in the middle of the heap, preventing compaction.  Eventually the
system would run out of memory.


More information about the jdk8u-dev mailing list