NIO IOUtils Direct vs Heap

Jon V. sybersnake at gmail.com
Mon Aug 1 00:23:13 UTC 2016


Andrew,

Thanks for the reply.

So it basically comes down to the need to keep the threads at safe points
as often as possible?  Don’t JNI calls already prevent GC due to references
as arguments?

On Sun, Jul 31, 2016 at 7:59 PM, Andrew Haley <aph at redhat.com> wrote:

> Hi,
>
> 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.
>
> Andrew.
>


More information about the jdk8u-dev mailing list