[ Team LiB ] Previous Section Next Section

Buffering Pages to Improve Performance

Buffering Java I/O is one of the biggest performance boosts any network-centric application can make. Without buffering, every time you write to an output stream you make a system call, which takes a lot of time. By buffering the output stream, you keep the data in memory until the buffer fills. Only when the buffer fills do you incur the time for the system call. You can do this using the following code:


<%@ page session="false" buffer="12kb" %>

Buffering is a classic tradeoff between memory and speed. You use more memory for the buffering, but you improve your program's speed. Some JavaServer Pages and servlets might require different buffer sizes, and deciding on the optimal buffer size helps you keep memory usage down while keeping your server running quickly.

From a speed standpoint, you want the buffer size to be as large as the page you are returning. By default, the size of our (implicit object of JSPWriter) object is 8KB. That way, you perform only one system call to send the buffer back. When you look at memory usage, you want the pages with the largest buffers to execute as quickly as possible, so the memory can be reclaimed quickly. Unfortunately, it typically takes more time to generate the contents for a large buffer, especially if a lot of computation is involved.

Optimization is an iterative process. You make changes and observe the results. You should look at optimization only when it looks as if you have a problem. Before you deploy a large-scale production application, get a load-testing tool and see whether your application stands up under a heavy load. If not, look at the hotspots (statements that are executed frequently) and try to reduce the number of times you execute them.

As you start to tune your buffering, you can use response.getBufferSize() at the end of your JSP or servlet to find out how much buffer space you used. When doing this, set the initial buffer size to something much larger than you think you'll need. Otherwise, you might fill the buffer and then see a small buffer size because the buffer has already been flushed.

    [ Team LiB ] Previous Section Next Section