Using the Java 5 profiling facilities
for servlet and server profiling

A significant enhancement in Java 5, which has received surprisingly little attention in the Java programming literature, was the introduction of various profiling facilities directly in Java. Since Java 5 it has been possible to perform various profiling tasks directly from Java, without having to resort to native code. An upshot of this is that it is much easier and safer to embed profiling facilities in a live environment. This article will be geared slightly towards profiling in a Servlet environment, although much of what we discuss will be applicable to server (and indeed client) profiling generally.

To start with it's important to note that the profiling techniques we'll be discussing here don't involve a simple plug-in-one-line-of-code-and-go. Java 5 provides you with the core routines to get profiling data out of the JVM, but you'll generally need to wrap some code around it for this information to be useful. But in a sense that's the whole point: you can tailor your profiling code to do exactly what you need for your particular application. And it turns out that the code required isn't generally too long or complicated.

Introduction to thread profiling

We'll be looking specifically at thread profiling: that is, taking a measure of what proportion of time (and/or CPU) threads are spending in different parts of your code1. As we develop our profiling code, some questions we'll need to consider include:

On the next page, we'll begin looking at some basic thread profiling methodology.


1. Actually, we could also look at other things such as what proprtion of time they're spending in different states (running, waiting for IO etc).


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.