Summary: I / O memory faster than slow, especially in the current context of the big data era, I / O performance problem is particularly prominent, I / O read and write has become the bottleneck of the system performance under many scenarios , we can not ignore. This article deep knowledge of the Java I / O in high concurrency, exposing the next big data business scene performance problem, from the source to start learning optimization methods. References: Chao "Java Performance Tuning combat"
Article Directory
1. What is the I / O?
I / O access to and exchange machine main channel information, and the flow is the main way to complete the I / O operation . In the computer, to an information stream is converted. Stream is ordered, and therefore with respect to a machine or an application,Typically the machine or application information is referred to receive external input stream (InputStream), information output from the application machine or outwardly called output stream (OutputStream), collectively referred to as input / output streams (I / O Streams).
The Java I / O operation class in the java.io package, wherein InputStream, OutputStream and Reader, Writer class is the I / O package of four basic categories, which are processed character stream and byte stream.
1.1 byte stream
InputStream / OutputStream abstract class is a byte stream, both abstract classes sent birth to a number of subclasses, different subclasses process different types of operations . If the file is read and write operations, use FileInputStream / FileOutputStream; read and write operations if the array is, on the use of ByteArrayInputStream / ByteArrayOutputStream; if ordinary read and write operations of the string, to use BufferedInputStream / BufferedOutputStream.
1.2. Character stream
Reader / Writer character stream is an abstract class , these two abstract classes also derive a number of subclasses, different subclasses process different types of operations.
2, the traditional I / O performance problem
I / O operation into the disk I / O operations and network I / O operations.
IO operations | Detail |
---|---|
Disk I / O operations | Data read from the disk is input to the memory source, then the read information is output on the persistent physical disk; |
Network I / O operations | Reading the information from the network is input to the memory, the final output information to the network. |
However, both the disk I / O or network I / O, there are serious performance problems in the conventional I / O in.
Performance issues | Detail |
---|---|
1. multiple memory replication | Data copied to from the external device to the kernel space, and then copied from the kernel space to user space, which occurred two memory copy operations. This action causes unnecessary data copying and context switching, thereby reducing I / O performance |
2. obstruction | In the traditional I / O in, InputStream's read () is a while loop operation, it would have been waiting for data to be read, will not return until the data is ready. This means that if no data is ready, the read operation will always be suspended, the user will thread is blocked. |
3, how to optimize I / O operations
JDK1.4 released java.nio package (new I / O acronym), NIO release optimizes memory copy and serious performance problems caused by obstruction. JDK1.7 has released NIO2, made to realize from the operating system level, asynchronous I / O.
Optimize operations | Detail |
---|---|
1. Use optimized buffer write stream operation | Traditional I / O and NIO biggest difference is that legacy I / O stream oriented, oriented NIO Buffer ( Buffer is a contiguous block of memory is read and write data in transit NIO ). Buffer file may be read into memory one time to do the subsequent processing, the traditional way is to read the file side edge processing data. Although the traditional I / O is used behind the bumper, for example BufferedInputStream, but still not comparable to the NIO. NIO used instead of the traditional I / O operations, can improve overall system performance. |
2. Use DirectBuffer reduce memory copy | NIO Buffer addition is made to optimize buffer block, but also provides a direct access to physical memory classes DirectBuffer. Common Buffer JVM heap memory is allocated,And the physical memory is allocated directly DirectBuffer. DirectBuffer application memory is not responsible for garbage collection directly by the JVM, but when DirectBuffer packaging is recycled, the memory block will be released by Java Reference mechanism. |
3. avoid blocking to optimize I / O operations | Blocking problem is the legacy I / O biggest drawbacks of the NIO release,And a multiplexer channelThese two basic components to achieve a non-blocking NIO's. |
Detailed channel and a multiplexer (some of the information on the blog post from BIO to NIO Netty threading model to explain in )
Package | Detail |
---|---|
Channel | == has its own processor, I can be accomplished between kernel space and disk / O operations ==. In NIO, we have to read and write data through the Channel, because the Channel is bidirectional, so reading and writing can be performed simultaneously. |
Selector | Event-driven implementation, we can register accpet, read listen for events, Selector will continue to poll registers on the Channel in Selector, if a monitor event occurs above the Channel, the Channel is in a ready state, then I / O operations. When there is no I / O operations on the Channel, the thread would not have been waiting for, but will continue to poll all the Channel, in order to avoid blocking occurs |