Java Reference
In-Depth Information
CHAPTER 17
■ ■ ■
Performance Tuning
M IDP is a small platform. The processor on a MIDP device will probably be much slower
than a typical desktop computer processor, and the available memory will be much smaller
also. Making your application run fast and lean is important. You'll need to use memory sparingly,
make your application run fast enough to be easily usable, and structure it so that the code
itself is as small as it can be.
This chapter describes simple methods for benchmarking your existing code. It then goes
on to describe various optimizations that can make your code run faster or use less memory.
Common sense will take you a long way, but this chapter is devoted to giving you the basic
techniques for optimizing your application.
The important rule of thumb is this: only optimize where it's needed. Said another way:
if it ain't broke, don't fix it. We suggest that your first pass at coding your application should
concentrate on cleanliness and maintainability. If performance problems exist, identify them
and begin optimizing. You shouldn't be optimizing code as you write it—that's just likely to
result in hard-to-read, hard-to-maintain code. Write first, then test, then optimize.
Benchmarking
In the J2SE world, developers have many tools for examining the performance of code, the
location of bottlenecks, and memory usage. Unfortunately, little of this is available in the J2ME
world. For the most part, you'll have to perform benchmarking the old-fashioned way. For this,
there are several methods in the MIDP API that will be useful. To test memory use, you can use
the following methods in java.lang.Runtime :
public long freeMemory()
public long totalMemory()
The first method tells how much memory, in bytes, is currently available. The second
method gives the total number of bytes in the current runtime environment, whether they are
used for objects or not. Interestingly, this number can change because the host environment
(device operating system) can give more memory to the Java runtime environment.
To find out how much memory an object uses, you can do something like this:
331
Search WWH ::




Custom Search