Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

3. Profiling Java applications

Create a Java project "de.vogella.tptp.javaproject". Create the following class for testing TPTP.

			
package de.vogella.tptp.javaproject;

import java.util.ArrayList;
import java.util.List;

public class Main {
	
	private List<String> list;
	
	public void addString(){
		list = new ArrayList<String>();
		for (int i = 0; i < 10000; i++) {
			String string = "Hello";
			list.add(string);
		}
	}
	
	public void printString(){
		for (String string : list) {
			System.out.println(string);
		}
	}
	public static void main(String[] args) {
		Main test = new Main();
		test.addString();
		test.printString();
	}

}

		

To profile a Java application, simply select the Java main class, right click and select "Profile as -> Java Application"

The first time this will open a pop-up which allows you to select for which criteria you are profiling your application for.

Tip

The latest JVMTI profiler supported by JVM version 1.5+ allows only mutually exclusive analysis types. Therefore only a single analysis type is available per each profiling session. For example you can profile for memory or for runtime but not for both.

To change later your selection please choose the following.

Switch to the perspective "Profiling and Logging" if you are asked. Here you can then see the report based on your selection.

Here you can review the result of your profile. I believe the reports are more or less self-explaining so I encourage you to review your results and identify the hotspots in your application.

Tip

You can also specify a filter for the packages you are interested in.