rgb_logo_pc_250.jpg
    (7844 bytes)

Getting started with Purify: Java code


The basic steps

Java applications can consume a lot of memory over time if a forgotten reference to an object unintentionally prevents it from being garbage collected. With Rational® Purify®, you can determine how much memory your Java program is using, and detect exactly which objects are responsible for these "memory leaks." You can also identify places where forcing a garbage collection would improve your code's performance.

To use Purify to profile Java memory usage:

  1. Run your Java program with Purify.
  2. Take a snapshot when memory usage stabilizes.
  3. Execute code that may be leaking and take another snapshot.
  4. Compare the two snapshots to identify methods that may be causing memory problems.
  5. Pinpoint the leaked objects allocated by these methods, and identify the references that are preventing the objects from being garbage collected.

Running your Java program with Purify

To Purify your Java program, start Purify and click Run in the Welcome Screen to display the Run Program dialog.

1j-rundl-small.gif (1479
    bytes)
Click to enlarge

Note: When you select a Java program (or applet, class, or JAR file) using the Browse button, Purify enters the name of the Purify Java helper program, pstart.exe, in the Program name field. The name of the Java program itself, along with the name of your specified Java virtual machine's Java viewer and any necessary options, is automatically entered in the Command-line arguments field. You must do the same if you enter information into these fields manually.

More information? Look up specifying a JVM and running Java programs in the Purify online Help index.

As your program runs, Purify intercepts and tabulates messages related to memory usage from the Java virtual machine. Based on these messages, Purify keeps track of how much memory your program has allocated to each method and object at any given time.

Taking snapshots of memory use

To zero in on memory leaks in your Java program, wait until your application's memory usage has stabilized (typically after it completes its initialization procedures), then click to take a snapshot of the current memory usage status. This snapshot is your baseline for investigating how your program uses memory as it runs.

Now exercise the program in a way that you suspect may be leaking memory. As your program runs, the Purify Data Browser's Memory tab displays a graph that indicates the amount of memory your program is using.

Click to enlarge

Watch the graph for fluctuations in memory usage. A large increase in memory usage may indicate a problem, especially when you can't reduce it by clicking to force a garbage collection.

Now take another snapshot so that you have a "before" and "after" record of what's going on, and exit your program.

More information? Look up taking snapshots (Java) and garbage collection in the Purify online Help index.

Comparing snapshots to identify problem methods

Select your second snapshot in the Navigator and click to compare the second snapshot with the first.

Purify now displays a call graph showing the methods that are responsible for allocating the largest amounts of memory during the interval between the first and second snapshots.

Click to enlarge

The call graph also shows you the calling relationship between methods. This can give you clues about which methods are holding on to unneeded objects and preventing the garbage collector from doing its job.

Move your cursor over the method or path you want to investigate. A tool tip pops up to give you memory-related statistics for that object.

Click to enlarge

This allows you to zero in on the method that is wasting memory, as well as its descendants.

To view your code from within Purify, right-click a method for which source is available, and select Source File.

More information? Look up diff'ing snapshots (Java), call graph (Java), and source code in the Purify online Help index.

Diagnosing leaks with the Function List View tab

The Function List View tab in the Data Browser provides a textual, non-hierarchical view of the same data. You can do full-program sorts in the Function List View to find the biggest memory-consuming methods in your entire program.

Click to enlarge

More information? Look up function list view (Java) in the Purify online Help index.

Focusing on a method with the Function Detail window

By double-clicking any method in the call graph or function list view, you can open a Function Detail window. This window shows how the method, its callers, and its descendants allocated memory.

Click to enlarge

More information? Look up function detail (Java) in the Purify online Help index.

If the amount of memory attributed to any method seems unexpectedly high, it may be the case that another method, possibly a descendant, has created a reference to an object that is preventing the memory from being garbage collected. For example, a descendant method may have created a static variable as part of a string array. This would keep the memory for the entire array from going out of scope, which may slow your program down, and even kill it.

When you've located a method that appears to be causing memory problems, go on to look at the method's objects. Purify provides extensive information not only about methods, but also about all objects in your program and their use of memory.

Looking for unneeded objects

Objects that a program no longer needs often prevent memory from being garbage collected and so, over time, slow down your program. Purify displays comprehensive memory data for objects in several formats, so that you can easily track down this sort of problem.

Note: To examine object data, use a snapshot or an aggregate data set. Comparison data sets, which are generated by clicking , do not contain object data.

Getting from a suspicious method to its objects

The Function Detail window, in addition to its information about a method, also lists objects that have been allocated by the method. You can sort the objects in the list by clicking on any column heading.

Click to enlarge

Examining object details

When you double-click an object in the Function Detail window, the Object Detail window opens. This window contains complete memory-related information for the object so that you can identify objects that are holding on to large chunks of memory, and determine how long these objects have been in existence.

Click to enlarge

Looking at all allocated objects together

To review the top-level objects in a program, open the Data Browser window for the snapshot that reveals potential memory problems, and click the Object List View tab.

Click to enlarge

The object list shows all top-level objects that were allocated at the time the snapshot was taken. In addition to the size of the objects, the object list provides information such as the time the object was created and the number of garbage collections it has survived. You can sort the list to find the objects that are holding on to the most memory, and the oldest objects in the list.

You can open the Object Detail window for any object by double-clicking the entry for the object. When you locate an object that may no longer be needed, look at your code. If you determine that the object is in fact no longer needed, modify your code to release all references to the object so that the object can be garbage collected.

More information? Look up object detail (Java) , object list view (Java) , and function detail (Java) in the Purify online Help index.

Saving Purify memory profiling data

You can save Purify data and analyze it later, share it with other members of your team, or include it in reports. Purify can save Java data in the following formats:

More information? Look up saving data (Java) in the Purify online Help index.


Advanced features for Java users


Highlighting methods that share key attributes

You can highlight methods in the call graph to display specific memory-related characteristics or to show calling relationships.

Click to enlarge

More information? Look up highlighting (Java) in the Purify online Help index.

Focusing your data

Use Purify's filter commands to remove a selected method, or all methods in a class file, from the set of data that Purify has collected. Alternatively, use subtree commands to focus on or remove a specific method and all its descendants from the dataset. Right-click a method in the call graph, function list view, or function detail to perform these operations.

Click to enlarge

Purify has undo capabilities for all filter and subtree commands so that you can easily return to any previous dataset configuration.

The call graph also provides a series of expand and collapse commands that work with subtrees. Unlike the filter and subtree commands, however, these commands affect only what is displayed in the call graph; they do not change the dataset.

In addition to the menu commands, you can use the Filter Manager to select the data you need.

Click to enlarge

More information? Look up filtering data (Java) and subtrees (Java) in the Purify online Help index.


Now you're ready to put Purify to work on your Java code. Remember that Purify's online Help contains detailed information to assist you.






Back| Next| Index| Table of contents





Copyright © 1996, 2001 by Rational Software Corporation. All rights reserved.