## Chapter 5

## Monitoring Applications

The J2ME Wireless Toolkit provides several tools to monitor the behavior of your applications. These tools are helpful in debugging and optimizing your code.

- The profiler lists the frequency of use and execution time for every method in your application.
- The memory monitor shows the usage of memory while your application runs.
- The network monitor shows network data transmitted and received by your application. It supports many network protocols including HTTP, HTTPS, SMS, and CBS.
- Tracing outputs low-level information to the KToolbar console.

Note – Monitoring features might slow down 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... from the KToolbar menu. Click on the Monitor tab. 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.

FIGURE 20 – Turning on the profiler

Now run your application by clicking on the Run button. Interact with your application as you normally would. When you’re finished, shut down the emulator. The profiler pops up with information about all the method calls in your application.

FIGURE 21 – The method profiler

The profiler displays two types of information:

- Method relationships are shown in a hierarchical list, the Call Graph.
- The right side of the profiler shows the execution time and number of calls for each method and its descendants.

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

### 5.1.1 The Call Graph

The call graph shows a hierarchy of method calls. Methods that call other methods are shown as folders. Double-click on 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 on 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 on the Find button.

As you click on 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 emualtor spent in the method. The execution time is described in four distinct ways:

- Cycles shows the amount of processor time spent in the method itself.
- %Cycles is the percentage of the total execution time that is spent in the method itself.
- Cycles with Children is the amount of time spent in the method and its called methods.
- %Cycles with Children shows the time spent in the method and its called methods as compared to the total execution time.

Click on 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 on the Save button in the profiler window. Choose a file name.

To load a profiler session, choose File > Utilities... from the KToolbar menu. Click on Open Session in the Profiler box. 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 J2ME Wireless Toolkit 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... from the KToolbar menu. Click on the Monitor tab. Check Enable Memory Monitor.

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

FIGURE 22 – The memory monitor graph

The memory monitor graph shows the following information:

- Current. The current amount of memory used by the application.
- Maximum. The maximum amount of memory used since program execution began, shown in the graph by a broken red line.
- Objects. The number of objects in the heap.
- Used. The amount of memory used.
- Free. The amount of unused memory available.
- Total. The total amount of memory available at startup.

Remember, you can modify the heap size using the Storage tab of the KToolbar preferences. See Chapter 3, “Working With Projects,” for details.

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

Note – The memory usage you observe with the emulator is not going to be 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 on the Objects tab in the memory monitor window.

FIGURE 23 – The memory monitor objects display

You’ll see a table with the following columns:

- Name. The class name of the objects.
- Live. The number of instances. Some of these might be eligible for garbage collection.
- Total. The total number of objects that have been allocated since the application began.
- Total Size. The total amount of memory used by the objects.
- Average Size. The average object size, calculated by dividing the live instances into the total size.

Click on 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 on the Save button. Choose a file name.

To load a memory monitor session, choose File > Utilities... from the KToolbar menu. Click on Open Session in the Memory Monitor box. 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... from the KToolbar menu. Click on the Monitor tab. Check Enable Network Monitoring.

Next time you run the emulator, the network monitor window pops up.

FIGURE 24 – The network monitor

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

The display on the left side shows a hierarchy of messages and message pieces. Click on a message or a portion of a message to see details in the right side of the network monitor.

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 – Network monitor filter settings
Network Protocol | Filter Settings
--- | ---
HTTP/HTTPS | Enter text to match the various parts of HTTP messages: URL, status line, headers, or body. For example, entering “slashdot” in the URL field would filter to show only messages whose URL contained “slashdot”.
SMS/CBS | 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.
MMS | 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.
OBEX/SPP/L2CAP | You can filter using the URL or header content.
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.

- Time. Messages are sorted in chronological order of time sent or received.
- URL. Messages are sorted by URL address. Multiple messages with the same address are sorted by time.
- Connection. Messages are sorted by communication connection. Messages using the same connection are sorted by time. This sort type enables you to see messages grouped by requests and their associated responses.

Sorting parameters are dependent on the message protocol you choose. For instance, sorting by time is not relevant for socket messages.

### 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... from the KToolbar menu. Click on Open Session in the Network Monitor box. 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.

| J2ME Wireless Toolkit User's Guide J2ME Wireless Toolkit 2.2

Copyright © 2004 Sun Microsystems, Inc. All rights reserved. Use is subject to the license terms.