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
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

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]


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger

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

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