Java Reference
In-Depth Information
Runtime runtime = Runtime.getRuntime();
long before, after;
System.gc();
before = runtime.freeMemory();
Object newObject = new String();
after = runtime.freeMemory();
long size = before - after;
Aside from examining memory usage, you may also be concerned with the speed of your
application. Again, you can test this the old-fashioned way—look at the clock before you start
doing something, and then look at it again when you're finished. The relevant method comes
from the java.lang.System class:
public static long currentTimeMillis()
You might calculate the execution time for a method like this:
long start, finish;
start = System.currentTimeMillis();
someMethod();
finish = System.currentTimeMillis();
long duration = finish - start;
For accurate timing, you should measure the duration multiple times and calculate an
average.
Diagnostic Tools in the J2ME Wireless Toolkit
WTK 2.x contains three tools you can use to understand your application's performance
characteristics.
The first tool is a memory monitor. You can see a graph of memory usage in your applica-
tion over time or a detailed breakdown of every object in your application. Turn on the memory
monitor by choosing Edit
Preferences from the KToolbar menu. Click the Monitoring tab
and check off Enable Memory Monitor. Next time you run the emulator, an additional window
will pop up. You can examine the total memory used, which is useful when you're trying to
make an application fit on a device with a limited heap size. (You can even set the heap size of
the emulator in the Storage tab of the preferences window.) Figure 17-1 shows the memory
monitor graph.You can click any column in the table to sort by that column. You can even search
for specific items using View
Find. Examining the memory monitor window will help you
identify the places where memory is consumed most in your application.
Aside from the memory monitor, the toolkit also includes a code profiler —a tool that
shows how much time is spent in every method in your application. To turn on the profiler,
choose Edit
Preferences from the KToolbar menu. Choose the Monitoring tab and check off
Enable Profiling.
Search WWH ::




Custom Search