How can you improve Java I/O performance?

Java applications that utilize Input/Output are excellent candidates for performance tuning. Profiling of Java applications that handle significant volumes of data will show significant time spent in I/O operations. This means substantial gains can be had from I/O performance tuning. Therefore, I/O efficiency should be a high priority for developers looking to optimally increase performance. The basic rules for speeding up I/O performance are

  • Minimize accessing the hard disk.
  • Minimize accessing the underlying operating system.
  • Minimize processing bytes and characters individually.

Let us look at some of the techniques to improve I/O performance.

  • Use buffering to minimize disk access and underlying operating system. As shown below, with buffering large chunks of a file are read from a disk and then accessed a byte or character at a time.
    Without buffering : inefficient code With Buffering: yields better performance

    Note: fis.read() is a native method call to the underlying operating system.

    Note: bis.read() takes the next byte from the input buffer and only rarely access the underlying operating system.

    Instead of reading a character or a byte at a time, the above code with buffering can be improved further by reading one line at a time as shown below:


    By default the System.out is line buffered, which means that the output buffer is flushed when a new line character (i.e. “\n”) is encountered. This is required for any interactivity between an input prompt and display of output. The line buffering can be disabled for faster I/O operation as follows:

    It is recommended to use logging frameworks like Log4J with SLF4J (Simple Logging Façade for Java), which uses buffering instead of using default behavior of System.out.println(…..) for better performance. Frameworks like Log4J are configurable, flexible, extensible and easy to use.
  • Use the NIO package, if you are using JDK 1.4 or later, which uses performance-enhancing features like buffers to hold data, memory mapping of files, non-blocking I/O operations etc.
  • I/O performance can be improved by minimizing the calls to the underlying operating systems. The Java runtime itself cannot know the length of a file, querying the file system for isDirectory(), isFile(), exists() etc must query the underlying operating system.
  • Where applicable caching can be used to improve performance by reading in all the lines of a file into a Java Collection class like an ArrayList or a HashMap and subsequently access the data from an in-memory collection instead of the disk.
Tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.