10. Hierarchy Viewer
Purpose: Debugging &optimize user interface
Running HV:
Connect your device or launch an emulator.
Install the application you want to work with.
Run the application, and ensure that its UI is visible.
Launch hierarchyviewer
The first window you see displays a list of devices and emulators.
Select the name of your Activity from the list. You can now look at
its view hierarchy using the View Hierarchy window, or look at a
magnified image of the UI using the Pixel Perfect window
13. layoutopt
Purpose: analyze XML layout file to find inefficiences.
Running layoutopt: layoutopt <xmlfile>
Information display when effect is detected:
The filename
The line number.
The description.
14. TraceView & Method Profiling
TraceView: a graphical viewer for execution logs that you create by using the Debug
class to log tracing information in your code.
Application:
Method profiling to find the bottle neck in your code execution.
Usage:
Create trace file:
Using Debug class:
// start tracing to "/sdcard/xml.trace"
Debug.startMethodTracing(“xml");
//Call to the method you want to trace.
// stop tracing
Debug.stopMethodTracing();
Using method profiling feature of DDMS to generate trace file.
Copy file to host machine.
Viewing trace file in Traceview.
Limitation:
Thread name is not emitted if thread exits during profiling.
Reuses thread ID of VM.
Reference:
http://developer.android.com/guide/developing/debugging/debugging-tracing.html
16. dmtracedump
Generate graphical call-stack diagram from trace log file.
Call reference number Inclusive elapsed time Exclusive elapsed time
Number of calls
dmtracedump [-ho] [-s sortable] [-d trace-base-name] [-g outfile] <trace-base-name>
17. jhat & Heap Profiling
Heap Profiling helps to find the leak memory, speed up the program:
How many instances of a class.
How much memory usage.
Usage:
Set the break point:
Exception break point.
Conditional break point.
Dump HPROF file from DDMS.
Using jhat (java heap analysis tool) to parse hprof file.
Open the favorite web browser and visit: http://localhost:7000
18. jhat & Heap Profiling
Exception breakpoint:
Run > Add Java Exception Breakpoint …
Conditional breakpoint:
Right click on breakpoint > Properties > Check Conditional checkbox > Enter the
condition.
Read more about MAT: http://10.1.6.46/mediawiki/index.php/Dectect_memory_leak_using_MAT
19. jhat & Heap Profiling
Using DDMS dump hprof file:
Click on “Show heap histogram”:
20. ANR & StrictMode
ANR.
Causes
Activity
BroadcastReceiver
StrictMode:
public void onCreate() {
if (DEVELOPER_MODE) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
}
super.onCreate();
}
Reference:
http://developer.android.com/guide/practices/design/responsiveness.html
21. Dev Tools Application
Get it from emulator & install into real device.
Application:
Enable some settings in your device for testing & debugging.
Usage:
Launch it > Development Settings
Debug app
Wait for debugger
Show screen updates
Immediately destroy activities
Show CPU usage
Show background
Reference:
http://developer.android.com/guide/developing/debugging/debugging-devtools.html
22. Debug Tips
Dump the stack trace:
adb shell > ps > kill -3
Display useful info on the emulator screen:
View Dev Tools App
Get application and system state information from the emulator:
View dumpsys and dumpstate
Get wireless connectivity information:
DDMS > Device menu > Dump radio state
Log trace data:
Call startMethodTracing()
Log radio data:
adb shell>logcat –b radio
Capture screenshots:
DDMS> Select device >Screen Capture
Use debugging helper classes:
Log, Debug, StrictMode
Garbage collection:
Any object the debugger is aware of is not garbage collected until after the debugger disconnects