3

I just read a wiki here, one of the passages said :

Although theoretically these are general-purpose data structures, the implementation may select memory for alignment or paging characteristics, which are not otherwise accessible in Java. Typically, this would be used to allow the buffer contents to occupy the same physical memory used by the underlying operating system for its native I/O operations, thus allowing the most direct transfer mechanism, and eliminating the need for any additional copying

I am curious about the words "eliminating the need for any additional copying", when will JVM need this and why NIO could avoid it ?

Sitansu
  • 3,225
  • 8
  • 34
  • 61
WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138

5 Answers5

1

It's talking about a direct mapping between a kernel data structure and a user space data structure; normally a context switch is required when moving between the two. However, with nio and a direct buffer, the context switch (and corresponding memory copies) does not occur.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

From java.nio package API:

A byte buffer can be allocated as a direct buffer, in which case the Java virtual machine will make a best effort to perform native I/O operations directly upon it.

Example:

FileChannel fc = ...
ByteBuffer buf = ByteBuffer.allocateDirect(8192);
int n = fc.read(buf);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

simply, old IO way always copy data from the kernel to memory in the heap. Using NIO allows to use buffers where file/network stream is mapped by the kernel directly. Result: less memory consumption and far better performance.

JosefN
  • 952
  • 6
  • 8
0

Many developers know only a single JVM, the Oracle HotSpot JVM, and speak of garbage collection in general when they are referring to Oracle’s HotSpot implementation specifically. but the thing is check Bob's post

Community
  • 1
  • 1
Zuko
  • 2,764
  • 30
  • 30
0

New input/output (NIO) library, introduced with JDK 1.4, provides high-speed, block-oriented I/O in standard Java code.

Few points on NIO,

  • IO is stream oriented, where NIO is buffer oriented.
  • Offer non-blocking I/O operations
  • Avoid an extra copy of data passed between Java and native memory
  • Allows to read and write blocks of data direct from disk, rather than byte by byte

The NIO API introduces a new primitive I/O abstraction called channel. A channel represents an open connection to an entity such as a hardware device, a file, a network socket. When you are using APIs FileChannel.transferTo() or FileChannel.transferFrom() JVM uses the OS's access to DMA (Direct Memory Access) which is potential advantage.

According to Ron Hitches on Java NIO

Direct buffers are intended for interaction with channels and native I/O routines. They make a best effort to store the byte elements in a memory area that a channel can use for direct, or raw, access by using native code to tell the operating system to drain or fill the memory area directly.

Direct byte buffers are usually the best choice for I/O operations. By design, they support the most efficient I/O mechanism available to the JVM. Nondirect byte buffers can be passed to channels, but doing so may incur a performance penalty. It's usually not possible for a nondirect buffer to be the target of a native I/O operation.

Direct buffers are optimal for I/O, but they may be more expensive to create than nondirect byte buffers. The memory used by direct buffers is allocated by calling through to native, operating system-specific code, bypassing the standard JVM heap. Setting up and tearing down direct buffers could be significantly more expensive than heap-resident buffers, depending on the host operating system and JVM implementation. The memory-storage areas of direct buffers are not subject to garbage collection because they are outside the standard JVM heap

Chapter 2 on below tutorial will give you more insight ( especially 2.4, 2.4.2 etc) http://blogimg.chinaunix.net/blog/upfile2/090901134800.pdf

Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46