Android DDMS: A Guide to the Ultimate Android Console
Developing is a tricky business. The target keeps moving, new technologies and domains periodically come to life, new tools pop every now and again, andlanguageschange in what seems to be managed havoc.
Still, even with all these changes, the fundamental rules stay the same. One of the most important of these underlying rule states that to create really awesome software, you must gain deep, ongoing and detailedintrospectioninto your executing system.Diagnostics,debugging, andprofilingare terms sometimes used in this context, but the rule goes deeper. A top-notch developer literally feels his system. He knows what will cause is to chunk waiting for more memory to release, what will run its threads into CPU starvation, which actions will result in extensive I/O or network access hence will slow its entire operation.
Theres really no way around it. You could be a very smart developer writing awesome code, but, until you will not have the above skill, namely being able to monitor and study the details of your systems runtime behavior, you will still fall behind when it comes to delivering really top notch applications.
In fact, after gaining some experience, you will detect a whole category of code diseases which can be traced to neglecting the rule of introspection: Briefly put, writing code (sometimes smart code) without continuous monitoring of its effects on the actual platform.
DDMS in Android: My Weapon of Choice for Introspection
Fortunately for us, the Android community had managed to deliver so many top notch introspection tools. FacebooksStethois amongst the best, AT&TsARO(Application Resource Optimizer) is a somewhat older but still top-notch, with probably the best network monitoring console out there, whileLeakCanarytakes a way more limited approach concentrating (and doing great at it) on runtime memory leak detection library. Long story short, there is no shortage of Android debugging tools out there.
Still, the diamond in the crown, the introspection tool to trust when crucial, accurate, and well-formatted data needs to be extracted regarding the runtime behavior of your app is still the good oldDalvik Debug Monitor Server (DDMS)in Android Studio, which has been with us (alas underused by so many teams) since the days of the Eclipse Android plugin.
How important is DDMS in Android development? Well, knowing what I know now about DDMS and mobile app monitoring, in general, say 5-6 years ago, as a less experienced Android developer, would have saved me alotof headaches and nights of debugging.
And the thing is that DDMS is so simple to master!
Of course, a large portion of using it correctly, as with any other software tool, comes with experience. You need to hone your professional skills for some time until you become really good at runtime performance monitoring. But even in a matter of hours, say after reading this article, if you follow my suggestions and apply them on your next app, the results will be amazing! Profiling and tuning even complex systems is not that hard. It can be fun as well!
A question often gets asked regarding the difference between novice and master-level mobile developers. Mastering DDMS in Androidor in general terms, application profiling and introspection capabilitiesis one such major difference.
Note:A major portion of becoming a top notch developer is using the best libraries available in your domain.
A Quick Guide to DDMS in Android Studio
And now, without further ado, let us now delve into the description of DDMS, one of the ultimate Android developer tools.
When weighing effort against benefit, probably no other tool can improve the quality of your app and help you in locating thereallymessy and elusive bugs it may contain. But still, for some reason (laziness, anyone?), so many teams fail to use DDMS.
Lets start with a crash course in DDMS:
DDMS is accessible viaStudio > Tools > Android > Android Device Monitorand clicking the DDMS button on menu. You can also place is as a shortcut icon (I do) in your upper panel.
Once opened, this is what youll see:
The left panel allows for device/app selection and the right console provides you will multiple views, each in its own tab, each exhibiting a specific view of your app.
The main services provided by Dalvik Debug Monitor Server are:
To obtain the current heap memory value used by your app, simply do as follows:
This last line probably requires additional explanation. Memory usage is one of those analytical values where itsdynamicsare way more important than the initial value. For most apps, we will not care much about the initial heap usage value. We will care very much about the progress of this value, as it will provide us with a clear indication of one the true nightmares awaiting mobile developersAndroid memory leaks:
My usage of the heap stat module is simple; as part of the lifecycle of developing the app, after introducing changes which should impact heap usage, I will activate the module, Cause GC to init stat gathering, activate (usually more than once) the heap-intensive positions of my app, and periodically Cause GC to refresh. If the heap usage keeps growing, I have a memory leak on my hands and I need to solve it (details on how - below). If not, and regardless of the actual heap size, Im good.
If a memory leak is detected, the next tool I will use is the Object Allocation Tracker. Lets see what it can do for memory management in Android.
Object Allocation Tracker
The allocation tracker, simply put, will provide you with the information required to figure out whos the party to blame for the current heap size. This module will tell you from which threads and methods did the allocation commands came from in a real-time manner, making it invaluable for memory analysis in Android.
To commence tracking, do as follows:
Now, from my own experience, performing allocation-intensive actions on your app followed by clicking Get Allocations to view allocation counters should typically direct you to the leak in a straightforward manner; sometimes, either when the leak is non-linear (i.e., happens every now and again) OR when your app contains multiple leaks that might not work. In such cases, and I have not encountered many of these, you will need to resort on manually creating a dump HPROF file and analyzing it. Memory analysis and Android memory management will not be covered in-depth in this article. Seeherefor some leads.
Thread Info Console: Android CPU Usage Made Easy
Well known to any developer, synchronous paths of executing logic are grouped into threads, each making up one serial flow of execution within your app. Literally all apps use more than a single thread of execution. Some of them use dozens.
An overall examination of potential problems when using threads is way out of the scope of this article. Let us concentrate then on a single one, namely thread starvation, which is the main problem you would visit the thread info console for.
In all mobile applications, different threads will compete for CPU time. There are simply not enough of those to go around. What happens if, for any reason, one or more if these threads will not get the execution time that they need? Usually bad things. The system will not behave as you planned it to behave, which is always a bad idea. Potential reasons for this problem could be setting low priority, other threads simultaneously executing setting themselves with overly high priority, spending long time on synchronization monitors, and more. Allnotoriouslyhard to detect by code review alone.
Android DDMS thread console to the rescue!
As you enter the thread view, you will see a list made up of thread records, each containing the threads name and ID, and two additional counters called utime and stime. Utime measures the total time spent by the thread executing user code (think your functions and third-party libraries), while stime measures the total time spent on system code (sleep, synchronize, system callsthe lot). The pfirst oneutimewill usually be more interesting for us, though I can think of problems that will mostly manifest themselves by the stime counter.
OK, we have our code running, including several threads, and we want to make sure all of our threads get their share of CPU time. For this, we first let our system run for a while, and then open the thread tab and start looking for peculiar utime values. Zero can certainly represent a problemthe thread got literally no CPU time and no CPU utilization. But overly high values might represent a different aspect of the same problem: namely, threads whose priority is so high as to cause others to starve.
Note that for one type of threads, zero or near zero utime value will not indicate a real problem. These are the I/O bound threads, threads that mostly do networking or disk (or database) access. These threadsshouldspend most of their time either waiting for data to arrive or blocking on pending system calls, neither of these actions increases the utime counter. Know your threads!
Tip:Never use threads default name. It means nothing and you will typically fail to detect it in the DDMS views. instead, whenever creating a thread or fetching it from a thread pool, start your interaction byassigning it with a self-explanatory name. This will make your life way easier than debugging/profiling your system. I usually prepend the apps name so to distinguish between Android generated thread and those spawned by my own code, example: MyApp-server-connector, MyApp-db-interactor, etc.
Tip:A threads priority denotes (loosely speaking) the amount of CPU time it will be granted by the scheduler. The priority assigned to your worker threads are of critical importance to the overall performance and smoothness of your app, and in many cases can be the difference between slick fast behavior and a bumpy slow one. The rule here is simple: The default priority assigned by Android, which is NORMAL=5, is almost always not the one you want to use. Instead, for most worker threads, you want a way smaller impact on overall CPU usage. To do this, at a threads startup,set its priorityto a lesser value, I usually go with priority=3.
Network Statistics Console
Network statistics is about allowing you to monitor both incoming and outgoing communication channels to your app in a reasonably human-readable manner.
The y-axis in the network chart represents the transmissions transfer speed measured in KB/second, while x-axis represents elapsed time in seconds. Therefore, to obtain a quick estimate of the transmissions size, try to estimate the area of the relevant spike. After a while, this becomes rather easy.
Note that, after this console is entered, you will need to click the upper enable button for network measurements to start appearing.
Before the network console had matured to the level it is at now, developers usually had to resort to using sniffer apps (some still do) to obtain similar information.
The great thing about this console is the way it visualizes one of the major battery draining behaviorsthat of ongoing small-packet size communication. As many of you know, what will make your app a battery drainer is not the five minutes of intensive networking that it does, but rather the long periods of short, repeating networking e.g., for the sake of keepalive, diagnostics, or status updates.
Once such a pattern is detected, and the visual packet display of the network console makes it ever so easy, immediately thinkbatching. Can I batch multiple small transmission into a single large one? The battery impact of this change is bound to move apps from a battery-drainer to a well-behaved category!
Tip:Never load an image into memory as is. This is an out-of-memory crash waiting to happen. Instead, performscaled-down loading, or even better, use athird-party libraryto manage the scaling for you.
Although you will rarely use this information, note that the DDMS relies on the Android Debug Bridge (ADB) stack to pass data back/from the device. If the DDMS fails to show your app, or freezes in the middle of a DDMS session, your best bet will be to open a console and type:
to make sure your device is accessible and authorized with ADB. If the is not the case, in many instances, restarting your local ADB server should solve the problem:
If youre still having problems and your app is installed on a physical device, try to disconnect all emulator instances. Why? Because DDMS connects itself to both physical devices device and emulator instances, the default being the latter.
Example of real life DDMS Usage:An app halts (not crashes, just halts). The user immediately rushes to nearby workstation, connects to USB, and opens DDMS in thread view to find out the thread stack failed thread stack tracein my case, due to synchronization deadlock that, once detected, was easily solved by switching.
Tip:If the standard RAM memory allocated to your app by Android is not enough, as might happen for, e.g., media intensive apps, note that you can gain some 15-20% additional memory on most devices by raising the _largeHeapmanifest flag: https://developer.android.com/guide/topics/manifest/application-element.html_
Device State Emulation in Android DDMS
As a rule, mobile apps are not linear constructs. Instead, they deploy awareness strategies that allow them to monitor and react to changes in the devices state. An app can, for example, listen to incoming call or text messages, can realign its state according to the network state, and can track and react to changes in the devices location.
A trivial example for the latter would be a GPS app. Most of us do not develop such apps (alas, the market is not big enough) but still, in many cases, we do deploy logic, which is location dependent, whether its a simple map view of the users current position, route tracking, or a location-sensitive data display.
Testing for such state sensitive conditions is notoriously complex, sometimes more so than writing the actual code. If you have a physical device with SIM you can, of course, issue and receive calls and SMSs. Changing your devices telephony status is way harder but still can be done. Test location changes could be trickier, though strolling around town with your laptop is an option
But stillhow would we handle emulator instances? How can we test them for these changes?
DDMS to the rescue, once again. One of the stronger yet often overlooked features of the DDMS is its ability to issue (spoof) mock events into a running emulator instance. DDMS can issue a call from a specific number to the emulator, send an SMS, change telephony status data, and more.
Once arriving into the emulator, all these spoofed events will no longer be distinguishable from real events, i.e., as if received by the underlying hardware sensors. Specifically, all of your relevant apps receivers will be activated in the same manner they would have upon receiving a real call/SMS message.
Activating the telephony status and actions is rather straightforward:
To test your app for cases of low network connectivity (which you should in any network-centered app) go to Telephony Status section and set the speed and latency values to the desired values. I usually go with the GPRS value for both as an effective way for emulating low connectivity, but feel free to set your own values.
To simulate phone calls or SMS, go to Telephony Action section, set the origin phone number, add a textual message if needed, and fire away. This tool is especially effective when you have set a dedicated code route for calls from abroad and want to test it on budget.
Things gets more interesting when it comes to mocking a new location.
If all you aim for is setting a new location for your emulator instance, choose Manual, set the desired latitude/longitude values, and hit Send.
But what if, instead of setting one fixed location, you want your app to go through a pre-set routesay, examine its behavior as the user is traveling from one city to another? Such a test can have great value for any map-backed app as well as other location-sensitive apps which set their data window per user location. Here, you will want to see that location shifting at different speed will keep the displayed data window up to date.
For this, we will use a special format called KML, which was specifically developed to be used with Google Earth, and which represent routes, or paths, as a set of connected points in space, which can be by GPS enabled devices.
GPX is an alternative path format supported by DDMS. For all practical purposes, these two should be considered interchangeable when used for mobile location spoofing.
Let us now walk through the stages of setting a mock route into the emulator.
and click to download the resulting GPX file (with a somewhat messy name e.g., 20170520030103-22192-data.gpx)
You do not need to create your own route. Plenty of routes to download from site such as OpenStreetMap (seeGPS Tracessection).
Finally, please note that unlike older DDMS versions, where route file loading was a breeze, newer versions might require some trial and error when loading a specific route.
For example it appears only GPX 1.1 is supported by DDMS. New GPX versions might require some manual adjustment.
Additionally, GPX waypoint format is no longer supported. Instead, use the GPX Track format:
Debugging Android: An Hour a Week Makes a Difference!
Enough theory! Its now time for some practice. I suggest, assuming you are anAndroid developer, that starting on your next project you will dedicatejust one hour a weekfor gaining introspection into your apps performance via DDMS.
You will be surprised from the amount ofquality information(i.e., information which may be used to immediately improve the state of your app) this will provide you with!
Android DDMS, as Ive witnessed time and again with novice developers, is a tool that can greatly improve a developers capabilities, provided it is mastered and properly employed. The ability of an Android developer to deliver top-notch systems will literally go up a notch or two once they tap the full potential of DDMS in Android development. Therefore, setting aside a few hours to make good use of DDMS sounds like a clever investment, as it can greatly improve Android performance and efficiency.
Be one of the smart guys. Use it.
Posted on :
About the Author
The Vital Guide to Android Interviewing - 2
Android Testing Tutorial: Unit Testing like a True Green Droid
Unlock Launcher Pro Plus..!! - (46191 Views)
IPL T20 Fever- Game - (45069 Views)
I Quran- With Complete Audio Recitation - (42707 Views)
[ROM] GingerDX V018 (X8/W8) - (35254 Views)
PowerAmp Music Player and Unlocker - (31890 Views)
Winamp- Pro version for free - (28104 Views)
Talking Tom 2 Pro for Free - (24403 Views)