OptimizeIt quick tour

 
Welcome to the OptimizeIt Java profiler. 

This tutorial guides you through the basic features of OptimizeIt software. More information is available in the OptimizeIt User Guide, a PDF document included in the OptimizeIt package. Note that an HTML version of this document is also available. If you have any questions not answered in the documentation, please contact us at support@intuisys.com.


Installing OptimizeIt

This tutorial contains an demonstration program that uses Swing. That program requires JDK 1.2 or later. For information about installing and configuring OptimizeIt, see Configuring OptimizeIt.

This tutorial assumes that Java Developer's Kit (JDK) version 1.2 has been installed at its default location (c:\jdk1.2). If you installed the JDK somewhere else, make sure to point to the installed location when instructed to in the tutorial. 

To be able to view JDK source code, you must extract the source files  from the src.jar file located in your JDK home directory. To perform this operation, type the following lines from the command line (change c:\jdk1.2 to your JDK location if it is installed somewhere else):

cd c:\jdk1.2
c:\jdk1.2\bin\jar xf src.jar


Profiling a Java program

To profile a Java program, OptimizeIt launches your Java program and the OptimizeIt audit system. The audit system runs in the test virtual machine and reports profiling information to OptimizeIt. The following steps show how to launch a simple application for profiling. 

To launch an application

  1. Run OptimizeIt. 

  2. The Edit settings dialog box opens automatically when you first invoke OptimizeIt. If you already have OptimizeIt open, select New from the File menu
  3. Select Application in part "Program type" of the dialog box
  4. Click the Browse button at the right of the "Program main class or Jar file" box
  5. Select <OptItDir>\Tutorial\QuickTourApp.jar
  6. Click the OK button to return to the Edit settings dialog box
  7. Click the "Virtual Machines" tab
  8. Make sure to select your JDK 1.2 in the list of the available virtual machines. If your JDK 1.2 does not appear in that list, it means you have not configured OptimizeIt to use your JDK. Just click on the "Add virtual machines..." button and follow the wizard to add your JDK to the list
  9. Click the Start Now button.

  10. The Edit settings dialog box closes, OptimizeIt launches the audit system and the example application, and the OptimizeIt window fills with profiling information. 


Using the memory profiler

When you first launch a test program, the OptimizeIt window opens to the memory profiler. The window can also display the CPU profiler, as the tutorial describes later.

The memory profiler lists the classes included in the test program and the real-time count of the number of instances allocated for each class. This is Heap mode. It allows you to see object allocations as they occur. The following steps show how to use the controls in the memory profiler to analyze how the example application uses memory. 

To use the memory profiler

  1. Click the Instance count column header to sort classes by number of allocated instance. 
  2. Limit the class list to java.awt classes

  3. The Filter box at the bottom of the window shows the classes being displayed. To show only java.awt classes, type java.awt.* followed by a Return in the box
  4. Notice that for many classes, instance count increases and decreases alternatively. Increase is due to instance allocations,  and decrease to garbage collection. To get an idea of temporary object allocation, click the "Disable garbage collector" option at the right bottom of OptimizeIt window. OptimizeIt keeps now count of every instances allocated
  5. Display backtrace information for the java.awt.Color class. 

  6. The backtrace information shows the methods involved in the allocation of instances of this class 
    To switch to Allocation Backtrace mode, double-click the class name in the list. You can also click the Allocation Backtrace button. 
  7. Expand the line EventDispatcherThread.run(), and keep on expending lines with highest percentages until you have expanded the line Graphics2d.fill3DRect().

  8. Notice that this method is responsible for almost all allocated colors.
  9. Double-click the line Graphics2d.fill3DRect() to show the source code which allocates the colors.

  10. The source code can help you understand why this drawing routine allocates so many instances of the class. The fill3DRect method allocates many colors because it calls Color.brighter() and Color.darker(), and those methods create a new instance of Color at each call
    Note: The first time you use OptimizeIt it may not have the path to the example source code. If OptimizeIt prompts you for the source code location, browse to c:\jdk1.2\src\java\awt\XXX.java and respond "Yes" to a prompt to save the source code location. 
Using the memory profiler allows you to minimize temporary object allocations. Temporary objects are usually quick to allocate, however they keep the garbage collector busy. With most available Java virtual machines, any Java program may freeze for several hundred milliseconds when the garbage collector is busy. If too many temporary objects are allocated, the application can feel really slow to the user because of these interruptions.
In the QuickTour example, we use the method Graphics2D.fill3DRect() to paint the colored squares. This method uses Color.brighter() and Color.darker() to get the colors of the borders of the square, to obtain the 3D look. As we always create squares of the same colors, a workaround for this would be to draw 3D rectangles ourselves and reuse colors.

The OptimizeIt memory profiler also allows you to understand why an object is not freed by the garbage collector. For example, this can be useful to verify that a document storage is really garbage collected when a document is closed.

The following steps describe using the memory profiler to determine whether an instance is being or can be garbage collected. 

To display information about a specific instance

  1. Return to Heap mode in the memory profiler.

  2. Click the Show Heap icon. 
  3. To show only Image related classes, clear the filter box completely then type *Image* followed by a  Return.
  4. Select the row javax.swing.ImageIcon
  5. Display the instance information. 

  6. Click the Show Instance Display icon. 
OptimizeIt displays the string representation of the instance in the top section of the window. The middle section displays which objects reference the selected instance and recursively which objects reference the object that references the instance. In this case, because the ImageIcon object is referenced, it cannot be garbage collected. 

The bottom section displays the allocation backtrace for the instance selected in the middle section. In this case, note that the instance was allocated in the constructor of the QuickTourApp. This image is the OptimizeIt logo displayed on the background of the application. 


Using the CPU profiler

While the memory profiler allows you to understand how to minimize object allocations, the CPU profiler allows you to understand how time is spent by objects. The CPU profiler can be seen as a recording device inside the Java virtual machine. The following steps describe how to produce time and CPU-usage information for your test program. 

To use the CPU profiler

  1. Switch to the CPU profiler display. 

  2. Click the CPU profiler icon. 
  3. Start recording the CPU usage of the example program. 

  4. Click the Start/stop CPU profiler icon. 
  5. Let the application run for approximately thirty seconds. 
  6. Stop the recording. 

  7. Click the Start/stop CPU profiler icon again. 
    The CPU profiler displays profiling information about the recorded session. 
  8. Select a specific thread to examine. 

  9. Click the drop-down box under the title "CPU profiler output". This drop-down box displays all threads currently running in the test virtual machine. Select the AWT-EventQueue-0 thread. 
    The AWT-EventQueue-0 thread is the thread used by AWT to process events such as repaint events. 
  10. Expand the line EventDispatchThread.pumpOneEvent()
  11. Expand all of the top methods under EventQueue.dispatchEvent(), until you have expanded the method JComponent.paintWithBuffer()

  12. Notice that the lines are marked with two icons: 
    The method immediately calls another method.
    The method actually consumes CPU.
    In this example the AWT-EventQueue-0 thread always processes repaint events. 
  13. Select the line JComponent.paintWithBuffer()

  14. Notice that if you leave the mouse cursor above a method called by JComponent.paintWithBuffer(), OptimizeIt displays how much time is spent in that method relative to the total time spent in JComponent.paintWithBuffer(). This can be very useful for fine granularity optimizations after resolving major bottlenecks. 
    Notice that the method JComponent.paint() consumes CPU. It draws the colored squares. The method WGraphics.drawImage() also consumes CPU, to display the OptimizeIt background logo.
To analyze where a thread sits idle, the CPU profiler can display pure CPU use and time use at the same time. Use this feature to choose a caching strategy, for example. By default OptimizeIt displays pure CPU use information. 

To display elapsed time and CPU usage together

  1. Open the Inspector. 

  2. Click the Inspector button. 

     
  3. Unselect the option Display CPU usage only. 


Using the virtual machine information
 
OptimizeIt can display in real-time high-level performance related data about the program being tested. Using this feature, you can understand if a performance problem is related to CPU, memory or both. 

To use the virtual machine information

 Switch to the virtual machine information display by clicking the virtual machine mode icon

This mode displays in 4 charts real-time information about virtual machine. 
The right top chart displays information about the garbage collector activity. Garbage collector activity represents the time spent garbage collecting to the total time. This can be useful to understand how often and how long the garbage collector is busy.
The left top chart displays the heap size and the current heap size actually used by the application.
The left bottom chart shows the number of threads in red, and in green the number of threads actually using some CPU.
The right bottom chart displays the number of classes currently loaded in the virtual machine.


Using snapshots

 
OptimizeIt allows to generate at any time a snapshot of the current profiling. The snapshot can then be reloaded in OptimizeIt for later analysis.

Generating a snapshot

  1. Select the menu "File/Generate snapshot"
  2. In the options section of the Generate snapshot panel, select the option "Include reference graph"
  3. Click Write snapshot
A snapshot of the current profiling has been generated in the c:\Temp directory.

Opening a snapshot

  1. Select the menu "File/Open snapshot"
  2. Browse to the c:\Temp directory and select the file with the name beginning with snapshot
  3. A warning box appears. Click OK to stop the current profiling and open the snapshot
The snapshot has been opened, you can access and browse the memory and CPU profiling information stored in the snapshot in the same way than described in the previous chapters.
 

For more information about OptimizeIt, see the OptimizeIt User Guide

Top