spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / java / servletsguide / chap16 current pageTo page 2To page 3To page 4
[next]

Java Servlets Developer's Guide

Developer News
Eclipse Helios Update Brings New PHP Tools
Internet Explorer 9 Ups Standards Support
JBoss Portal 5 Release Easier to Use

Performance Tips and Tricks

In This Chapter:

  • Avoid String Concatenation
  • Avoid Debugging Statements
  • Avoid Use of StringTokenizer
  • Avoid Unnecessary Synchronization
  • Use a Connection Pool
  • Use a PreparedStatement
  • Cache Expensive Objects
  • Tune Your Servlet Container
  • Tune Your Virtual Machine
  • Summary

In this, the final chapter, we'll take a look at some general tips to maintain good performance when writing servlets. I will not attempt to quantify any of these suggestions; there are such a wide variety of processors and Virtual Machines that trying to compare performance timings can be very misleading. Keep in mind that as VMs continue to improve and processors continue to get faster, the overall relevance of the following tips may diminish. Even so, coding for performance will pay off in the long run, especially when creating servlets that will be used by hundreds, or even thousands, of concurrent users. Even small gains in performance can provide huge gains in scalability.

Avoid String Concatenation

Even though I've used String concatenation throughout this book, mostly to improve readability, you should try to avoid this habit and use a StringBuffer instead. For example, the following:

String a = "Hello ";
String b = "Karl";
String c = a + b;

should be replaced with this:

String a = "Hello";
String b = "Karl";
StringBuffer sb = new StringBuffer(a);
sb.append(b);
String c = sb.toString();

String concatenation results in a new String and StringBuffer instance to be created by the Virtual Machine and reassigned to the new String value; each old String is released for garbage collection, resulting in many short-lived objects. Note that this is not true for String literals; the compiler will optimize "Hello " + "Karl" as "Hello Karl" automatically.

Also, if you have a good idea of how large the final String may be, you can also preallocate the size of the StringBuffer by using a different constructor:

StringBuffer sb = new StringBuffer(256);

A very nice feature of the StringBuffer class is that the append() method returns the StringBuffer instance, so you can cascade the append() calls:

StringBuffer sb = new StringBuffer();
sb.append("Hello ").append("Karl");

home / programming / java / servletsguide / chap16 current pageTo page 2To page 3To page 4
[next]

webref The latest from WebReference.com Browse >
Flashmaps' DynamicLocator: Interactive Maps for Small Areas · Flashmaps' AreaSelector: Interactive Maps for Wide Areas · The DB Mapper: Interactive Street-level Maps of U.S. and Canada
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags

Created: July 31, 2002
Revised: July 31, 2002

URL: http://webreference.com/programming/java/servletsguide/