Conclusion and Best Practices Writing great mobile device applications is a matter of balancing competing design goals, coming up with clever mechanisms to avoid doing unnecessary work, and most importantly keeping a disciplined performance-minded approach in your development. The following are a summary of some of the best practices to help with this:Make a conscious and proactive decision to manage your mobile application's memory. Do not allow your mobile device application's memory model to come about as a matter of passive or arbitrary design. Make an explicit statement of what is to be kept in memory and when it will be discarded.Define discrete application states and identify the resources that are required in each state. A state machine approach can be very useful in implementing this kind of memory management. Your mobile device application should maintain a specific list of states and state transition behaviors that enable you to tune your application's memory usage.Do not be afraid to cache commonly used resources. It should be a goal to keep your application's steady-state memory usage as low as possible, but this should not be done at the expense of throwing out useful objects. If you continually have to re-create and dispose of critical objects, these objects should be cached instead to avoid this memory churn. Consider having a resource dispenser class that is in charge of allocating, caching, freeing, and handing out resources to the rest of your application. This dispenser class can separate the dispensing of resources from their allocation and destruction. If you have a central resource dispenser, the rest of your application code does not need to worry about allocation and disposal strategies and your resource dispenser can be tuned to your application's needs in its different states.Use the wait cursor and give feedback to users for any task that takes more than an instant. For any task that takes more than half a second, a wait cursor should be used. For any task that takes more than a couple of seconds or takes an indeterminate amount of time, users should be given additional updates indicating what progress is being made on their behalf.Develop and test with real-world data sizes and connectivity models. It is important to test your mobile device application in a similar environment to the one that end users will experience. This means both using data sizes that end users will expect to work with as well as using connectivity models (for example, connection speed and latency) users will experience. If your application has capacity thresholds above which performance starts deteriorating significantly, consider explicitly disallowing the application to reach these states; you do your application's users no favors by allowing them to get into poorly performing states.Be mindful of dimensions of bitmaps you use. Common digital images today are significantly larger than can be usefully displayed on mobile device screens. It is time wasteful to move these images over networks, space wasteful to store them on mobile devices, and extremely memory wasteful to load them into memory. An extra 0.5MB of image data loaded into memory is equivalent to hundreds or thousands of nonimage data items held in memory; 500KB of pixels is approximately 500,000 integers worth of data. Whenever possible, reduce image dimensions down to the target device's display capacity before downloading them. If this is not possible and high-resolution images need to be loaded into memory, consider immediately making smaller-resolution copies in memory and releasing the large-sized original so that your application does not hold huge amounts of unnecessary memory.Look for ways to preprocess data. This is true for graphics code, XML code, and any other significant code that needs to be executed. The more you can precalculate and put into an easy-to-consume format, the less processing time you spend at runtime. Good performance is essential to building high-quality mobile applications. The necessary performance can be achieved by creative and disciplined design. The guidelines and design strategies outlined in the preceding chapters should serve as a good beginning in helping you approach performance challenges in a creative and well-considered way. |