Tuesday, February 5, 2013

Dalvik Debug Monitor Server (DDMS)



DDMS


DDMS (Dalvik Debug Monitor Server) is a graphical program that communicates with our devices through adb and allows reflecting and gathering information about device state, processes, threads, memory etc.

The DDMS tool can be run in the following two ways-

  • From Eclipse: Click Window > Open Perspective > Other... > DDMS.
  • From the command line: Type ddms (or ./ddms on Mac/Linux) from the tools/ directory.


The following two snapshots show the DDMS view from Eclipse and standalone execution respectively.

DDMS perspective view in Eclipse


The standalone view of DDMS
Please note that the Eclipse version and the command line version of DDMS have minor UI differences, but their functionality is same. In our explanation of using various functionalities of DDMS, we are going to consider the Eclipse version of DDMS.
The DDMS can be used to perform the following operations-

  • Viewing heap usage for a process.
  • Tracking memory allocation of objects.
  •  Working with an emulator or device's file system.
  • Examining thread information.
  • Starting method profiling.
  • Using Logcat.
  • Emulating phone operations and location.

 
A- Viewing Heap Usage
Using DDMS, we can figure out how much heap memory a particular process is consuming. To view heap usage for a process, we can follow the below process.
From the Devices tab, select the target process and click on the Update Heap button to enable heap information for the process. In the following diagram we are considering ―com.sec.android.widgetapp.diotek.smemo process.

Selecting the process from Device tab and using the "Update Heap" button

As the next step, click on the ―Cause GC” button available in the Heap tab to invoke garbage collector. As soon as we click on this button, it enables the collection of heap data. When the operation completes, we will see a group of object types and the memory that has been allocated for each type. The following figure shows data for ―com.sec.android.widgetapp.diotek.smemo process-

The Heap tab of DDMS

The first table of heap tab shows following information-

  • Total heap size.
  • Allocated heap size for this process
  • Free heap size
  • Usage percentage
  • No. of Objects


The second table shows list of various types of objects, their count, their total size, and the other statistics related to their sizes.
We can select any particular type of object from the list and it will display a chart showing number of objects allocated for a particular memory size of that type.

The Graph view object allocations


B- Memory Allocation Tracking
DDMS provides a feature to track objects that are being allocated into memory and to see which classes and threads are allocating those objects. This information can be useful in debugging issues related to memory performance of an application or dealing with Out of memory errors.
To start using the allocation tracker, select the process from the device tab and click on the ―Start tracking button available under Allocation Tracker tab.

Allocation Tracker window


Once we have executed the code path intended to be analyzed click on the ―Get Allocations button. We will get the following screen.


Object lists of Allocation Tracker window

As we can see the list of allocated objects is shown in the first table. By clicking on a line we can see, in the second table, the stack trace that led to the allocation. Not only we will know what type of object was allocated, but also in which thread, in which class, in which file and at which line.


C- Method Profiling
Method profiling can be useful in debugging issues related to user responsiveness, database interaction or any other issues related to application performance. Using method profiling of DDMS we can track certain parameters about a method, such as number of calls, execution time, and time spent executing the method.
To start the method profiling, select the process and click on the ―Start Method Profiling button from the device tab. C- Method Profiling Method profiling can be useful in debugging issues related to user responsiveness, database interaction or any other issues related to application performance. Using method profiling of DDMS we can track certain parameters about a method, such as number of calls, execution time, and time spent executing the method. To start the method profiling, select the process and click on the ―Start Method Profiling button from the device tab.

Starting the method profiling of a process

Once we have started the method profiling, whatever actions we will perform on the selected process, all the methods that are getting called will be recorded. If we want to stop the profiling click on the ―Stop Method Profiling button from the device tab.


D- Examining Thread Information
The ―Thread tab of DDMS shows us currently running threads for selected process. Use the following steps to gather thread related information.
1-    From the Devices tab, select the process that is intended to be examined and click on the ―Update Threads” button.

The Device tab of DDMS

2- In the Threads tab, we can view the thread information for the selected process

The Thread tab of DDMS

As we can see in above snapshot, there are multiple threads listed for smemo process. To get further information related to a particular thread, select the thread from the list and click on ―Refresh button. The following figure shows information that is available on click of refresh button-

Detailed thread information in Thread tab


E- Working with Device’s File System
Using DDMS we can interact with the Device‘s file system. The ―File Explorer tab of DDMS allows us to do the same. Using DDMS following operation can be done with the file system-
1- Pull a file from the device (or Emulator).
2- Push a file into the device (or Emulator).
3- Remove a file from the device (or Emulator).

The respective buttons for these operations are shown in the following snapshot-


Buttons for pull, push & remov3 files from device (Emulator)

File explorer is very useful in debugging. For example if there are some database related issues, we can fetch (pull) the current .db file.






No comments: