Java NIO and BIO comparison

Java NIO and BIO comparison

After learning the principles of computer composition, you know that the direct memory access DMA method in the IO system further improves the CPU utilization compared with the program interrupt method. Here, a verification is performed by reading and writing a file from the Java program.

public class TestChanel {
    
    
    public static void main(String[] args) throws IOException {
    
    
        long start = System.currentTimeMillis();
        FileChannel reader = FileChannel.open(Path.of("Tomcat内核设计剖析.pdf"));
        FileChannel writer = FileChannel.open(Path.of("Tomcat内核设计剖析ChannelCopy.pdf"), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
        ByteBuffer allocate = ByteBuffer.allocate(1024*1024);
        int read = reader.read(allocate);
        while (read != 0) {
    
    
            writer.write(allocate);
            read = reader.read(allocate);
        }

        System.out.println("The channel spend time: " + (System.currentTimeMillis() - start));
        reader.close();
        writer.close();
        start = System.currentTimeMillis();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("Tomcat内核设计剖析.pdf"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("Tomcat内核设计剖析BufferCopy.pdf"));
        byte[] bytes = new byte[1024*1024];
        read = bufferedInputStream.read(bytes);
        while (read != -1) {
    
    
            bufferedOutputStream.write(bytes);
            read = bufferedInputStream.read(bytes);
        }
        System.out.println("The buffer spend time: " + (System.currentTimeMillis() - start));
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }
}

The output is:

The channel spend time: 5
The buffer spend time: 189

The results are indeed quite different. It can be seen that the reading and writing efficiency of NIO is much faster than that of traditional BIO.

NIO reads and writes objects stored in the direct memory of the virtual machine, and -XX:MaxDirectMemorySize=Sizeparameters can be used to limit its size.

Exception in thread "main" java.lang.OutOfMemoryError: Cannot reserve 1048576 bytes of direct buffer memory (allocated: 8192, limit: 1047552)
	at java.base/java.nio.Bits.reserveMemory(Bits.java:178)
	at java.base/java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:121)
	at java.base/java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:332)
	at java.base/sun.nio.ch.Util.getTemporaryDirectBuffer(Util.java:243)
	at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:293)
	at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:273)
	at java.base/sun.nio.ch.FileChannelImpl.read(FileChannelImpl.java:232)
	at study.java.nio.TestChanel.main(TestChanel.java:36)

For this, 1024KB needs to be allocated, but if only 1023KB is allocated, an OutOfMemoryError exception will be thrown.

Guess you like

Origin blog.csdn.net/HHoao/article/details/129302649