Java zero-copy

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/wufaliang003/article/details/90473395

1、MappedByteBuffer

FileChannel java nio offer provides a map () method, which can create a virtual memory mapping between an open file and MappedByteBuffer, MappedByteBuffer inherited from ByteBuffer, similar to a memory-based buffer, but the object's data elements stored in a file on disk; call to get () method gets the data from the disk, the data reflect the current contents of the file, call the put () method updates the file on disk, and file modifications made to other reader is also visible; see the following example a simple reading, and then analyzed for MappedByteBuffer:

 
 

Map FileChannel provided mainly by () to achieve map, map () method is as follows:

Three parameters are provided, MapMode, Position and size; respectively represent:
mapmode : mapping modes, options include: READ_ONLY, READ_WRITE, PRIVATE;
the Position : mapped starting position from which the position number of bytes;
Size : from position start back how many bytes;

Focus look MapMode, please indicate two read-only and read-write, and of course the requested mapping mode by access restrictions Filechannel object, if enabled READ_ONLY on a file does not have read permission, will throw NonReadableChannelException; PRIVATE mode It represents copy-on-write mapping means through put () method will result in any changes made to produce a private copy of the data and the data of the copy only MappedByteBuffer example can be seen; this process does not make any changes to the underlying file , and once the buffer is subjected to the action of garbage collection (garbage collected), those changes will be lost; a quick look at the source map () method:

 
 

Roughly meaning acquired by the native method address memory-mapped, and if that fails, to manually re-mapping gc; the last address of the memory mapped by instantiating a MappedByteBuffer, MappedByteBuffer itself is an abstract class, in fact, there is a real example of words out of DirectByteBuffer;

2、DirectByteBuffer

DirectByteBuffer inherited MappedByteBuffer, you can guess from the name opens up some direct memory, does not occupy jvm memory space; the one mapped out by Filechannel MappedByteBuffer actual DirectByteBuffer also, of course, in addition to this way, you can manually open up some space:

 
 

As opened direct memory space of 100 bytes;

3, Channel-to-Channel transmission

Often you need to transfer files from one location to another location, provided FileChannel transferTo () method is used to increase the efficiency of the transmission, first look at a simple example:

() Method will be transmitted through the FileChannel transferTo file data to System.out channel, the interface is defined as follows:

 
 

Several parameters are better understood, it is transmitted starting position, number of bytes transferred, and a target channel; transferTo () allows one channel to the other channel cross-connect, without the need for an intermediate buffer to transfer data;
Note: here without intermediate buffer has two meanings: the first buffer layer does not need to copy the user space kernel buffer, another layer of two kernel buffer channel has its own, you can do two kernel buffer no need to copy data;

Netty zero-copy

netty provides zero-copy buffer, during data transmission, the data may require the final processing of individual transmission packets, combining and splitting, ByteBuffer Nio native not possible, by providing a netty Composite (combination) and Slice (split) in two buffer for zero copy; see below would be a relatively clear FIG:

HTTP packets TCP layer is divided into two ChannelBuffer, two of our top Buffer Logic (HTTP processing) it makes no sense. ChannelBuffer but the two are combined, it becomes a meaningful HTTP packet, this packet corresponding ChannelBuffer, is able to call it "Message" thing, here used the word "Virtual Buffer".
Netty CompositeChannelBuffer can look at the source code provided:

 
 

components is used to save all received buffer, indices recording start position of each buffer, ComponentId on lastAccessedComponentId record first visit; CompositeChannelBuffer will not open new direct memory and copy all the contents ChannelBuffer, but directly saved All ChannelBuffer references, and read and write in the sub-ChannelBuffer years, to achieve a zero copy.

Other zero-copy

RocketMQ message written commitlog sequential file, and then using the index file as consume queue; RocketMQ using zero-copy mmap + write way to respond to a request Consumer;
there are also a large number of network data persisted to disk and disk files over the network in kafka the process of transmitting, kafka sendfile using zero-copy mode;

to sum up

If the zero-copy simple probability java objects inside to understand, in fact, is the use of references to all objects, where each reference object can change its change on this subject, there is always only one object.

No micro-channel public attention and headlines today, excellent article continued update. . . . .

 

 

Guess you like

Origin blog.csdn.net/wufaliang003/article/details/90473395