C H A P T E R  5

Monitoring Applications

The Sun JavaTM Wireless Toolkit for CLDC provides several tools to monitor the behavior of your applications. These tools are helpful in debugging and optimizing your code:

Note - Monitoring features might slow the execution of your application.

5.1 Using the Profiler

The profiler keeps track of every method in your application. For a particular run, it figures out how much time was spent in each method and how many times each method was called. After you finish running your application and shut down the emulator, the profiler pops up, allowing you to browse through the method call information.

To turn on the profiler, choose Edit > Preferences. See FIGURE 5-1. Click Monitor in the left pane. In the right pane, check Enable Profiling. If you are interested in seeing profiling information for all the system implementation methods, check Show System Classes. Otherwise, the profiler shows only system methods that contain calls to your application methods. Click OK.

FIGURE 5-1 Turning on the Profiler

In the preferences window, select the monitor category, then check Enable Profiling

Now click the Run button to start your application. Interact with your application as you normally would. When you are finished, shut down the emulator. The profiler pops up with information about all the method calls in your application.

FIGURE 5-2 Method Profiler

Methods Profiler viewing the DefaultColorPhone

The profiler displays two types of information:

Note - The profiling values obtained from the emulator do not reflect actual values on a real device.

5.1.1 Call Graph

The call graph shows a hierarchy of method calls. Methods that call other methods are shown as folders. Double-click a method to open it and see the methods it calls. Methods that do not call any other method are shown as gray circles.

You can search for a particular class or method name. Click Find and fill in a name. The search is performed from the current selection in the call graph to the end. If you want to search the entire call graph, check Wrap before you click the Find button.

As you click different nodes in the call graph, the right side of the profiler shows details about the methods for that node.

5.1.2 Execution Time and Number of Calls

The right side of the profiler window displays detailed information about methods. You can see the method name, the number of times it was called, and the amount of time that the emulator spent in the method. The execution time is described in four distinct ways:

Click any column to sort by that column. Click a second time to switch the sort between ascending and descending.

The right pane shows the methods contained in the currently selected node in the call graph. If you want to see every method, click on the <root> node in the call graph.

5.1.3 Saving and Loading Profiler Information

To save your profiler session, click the Save button in the profiler window. Choose a file name.

To load a profiler session, choose File > Utilities. Click Profiler and press Launch. When you select a file, the profiler window appears with all the session information.

5.2 Using the Memory Monitor

Memory is scarce on many MIDP devices. The Sun JavaTM Wireless Toolkit for CLDC includes a memory monitor that makes it easy to examine the memory usage of your application. You can see the total memory used by your application and a detailed listing of the memory usage per object.

To turn on the memory monitor, choose Edit > Preferences. Click on Monitor in the left pane. Check Enable Memory Monitor.

Next time you run the emulator, the memory monitor window appears, displaying a graph of your application's memory usage over time. The memory monitor slows your application startup because every object created is recorded.

FIGURE 5-3 Memory Monitor Graph

Memory monitor extension with Graph tab selected

The memory monitor graph shows the following information:

Remember, to modify the heap size select Edit > Preferences and choose the Storage tab. See Chapter 3 for details.

To request the system to perform a garbage collection, click Run GC.

Note - The memory usage you observe with the emulator is not exactly the same as memory usage on a real device. Remember, the emulator does not represent a real device. It is just one possible implementation of its supported APIs.

To see details about the objects in your application, click the Objects tab in the memory monitor window.

FIGURE 5-4 Memory Monitor Objects Display

Memory monitor extension Objects tab

A table with the following columns appears:

Click any column header to sort on that column.

You can search for a specific class name by choosing View > Find... from the memory monitor window menu.

5.2.1 Saving and Loading Memory Monitor Information

To save your memory monitor session, click the Save button. Choose a file name.

To load a memory monitor session, choose File > Utilities. Click Memory Monitor and press Launch. When you select a file, the memory monitor window appears with all the session information.

5.3 Using the Network Monitor

MIDP applications, at a minimum, are capable of HTTP network connections, but many other types of network connections are also possible. The network monitor provides a convenient way to see the information your application is sending and receiving on the network. This is helpful if you are debugging network interactions or looking for ways to optimize network traffic.

To turn on the network monitor, choose Edit > Preferences. Click Monitor in the left pane. Check Enable Network Monitoring.

Next time you run the emulator, the network monitor window appears.

FIGURE 5-5 Network Monitor

Network Monitor HTTP/HTTPS tab

When your application makes any type of network connection, information about the connection is captured and displayed. The figure shows HTTP requests and responses.

The display on the left side shows a hierarchy of messages and message pieces. Click a message or a portion of a message to see details in the right side of the network monitor. Double-click messages or message portions to expand or collapse them.

Message bodies are shown as raw hexadecimal values and the equivalent text.

Note - You can examine messages that are still in the process of being sent. Incomplete messages are indicated by bold highlighting in the message tree.

5.3.1 Filtering Messages

Filters are useful for examining some subset of the total network traffic. Filter settings are specific to the network protocol used.

Press the Filter Settings button to use the filter. Change the filter settings to suit your needs.

TABLE 5-1 Network Monitor Filter Settings

Network Protocol

Filter Settings


Enter text to match the various parts of HTTP messages: URL, status line, headers, or body. For example, entering slashdot in the URL field filters to show only messages whose URL contain slashdot.


You can specify a protocol, message type, and direction to match. Furthermore, you can enter text to match in the sender, receiver, and message content.


Enter text to match the direction, sender, receiver, and copied (cc) and blind copied (bcc) receivers. In addition, you can filter on the subject, content ID, content location, MIME type, and encoding.


You can filter using the URL or header content.


Filter on the URL or the message content.


None available

Socket, SSL, Datagram, Comm

Enter text to match in either the connection string (URL) or content.

When you are done entering filter settings, press OK to return to the network monitor. The Filter checkbox is checked, indicating that a filter is in use. To disable the filter and see all messages, uncheck the checkbox.

5.3.2 Sorting Messages

To arrange the message tree in a particular order, click on the Sort By combo box and choose a criteria.

5.3.3 Saving and Loading Network Monitor Information

To save your network monitor session, choose File > Save or File > Save As from the network monitor window menu. Choose a file name.

To load a network monitor session, choose File > Utilities. Select Network Monitor from the list and press Launch. When you select a file, the network monitor window appears with all the session information.

5.3.4 Clearing the Message Tree

To remove all messages from the network monitor, choose Edit > Clear from the network monitor menu.