X Means Y

21
Copyright © 2012CommonsWare, LLC X Means Y: Interpreting the Development Tools Samsung Developer Office Hours NYC

description

from the Samsung Developer Office Hours NYC

Transcript of X Means Y

Page 1: X Means Y

Copyright © 2012CommonsWare, LLC

X Means Y: Interpreting the

Development Tools

Samsung Developer Office Hours NYC

Page 2: X Means Y

Copyright © 2012CommonsWare, LLC

01-03 01:32:11.986: E/AndroidRuntime(17991):Caused by: android.os.NetworkOnMainThreadException

Page 3: X Means Y

Copyright © 2012CommonsWare, LLC

StrictMode

● Reports obvious long-running stuff on main application thread

● Network I/O automatically reported on Android 3.0+– Do not disable the error – fix the problem!– Opt-in for Android 2.3

● Can also configure to report disk I/O on mainthread, failure to close files/databases/cursors,etc.

Page 4: X Means Y

Copyright © 2012CommonsWare, LLC

Page 5: X Means Y

Copyright © 2012CommonsWare, LLC

06-29 23:11:17.796: I/Choreographer(691):Skipped 18 frames! The application may be doingtoo much work on its main thread.

Page 6: X Means Y

Copyright © 2012CommonsWare, LLC

Choreographer● Information Message in LogCat

– “Skipped N frames” * 16ms = amount of time you took on main application thread

– The more frames skipped, the more likely it is the user will notice

● UI frozen and not responsive● Animation/scrolling hiccups

– Android 4.1+

Page 7: X Means Y

Copyright © 2012CommonsWare, LLC

Project Butter

● UI Updates @ 60 frames/second● Objective: smoother UI updates

– Especially animations● Transparent to developers

– No code changes to “opt into” Butter effects● Google I/O 2012: “For Butter or Worse”

Page 8: X Means Y

Copyright © 2012CommonsWare, LLC

Project Butter Ramifications

● Only ~16ms Per Frame– Your code on the main application thread– Framework code– Other interruptions (e.g., background process)

● Net: Get Off the Main Application Thread!– Anything taking more than a few milliseconds

needs to move to background thread or be executed incrementally

Page 9: X Means Y

Copyright © 2012CommonsWare, LLC

12-12 08:56:20.746: E/AndroidRuntime(1570):java.lang.RuntimeException: Can't create handler insidethread that has not called Looper.prepare()

Page 10: X Means Y

Copyright © 2012CommonsWare, LLC

Looper and Handler

● Queuing mechanism● Used by main application thread● Error results from trying to do wrong thing on

background thread– setContentView()– requestLocationUpdates()

● HandlerThread

Page 11: X Means Y

Copyright © 2012CommonsWare, LLC

java.lang.StackOverflowError at android.widget.TextView.onDraw(TextView.java:4329) at android.view.View.draw(View.java:6993) at android.view.ViewGroup.drawChild(ViewGroup.java:1732) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.View.draw(View.java:6996) at android.view.ViewGroup.drawChild(ViewGroup.java:1732) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.View.draw(View.java:6996) at android.view.ViewGroup.drawChild(ViewGroup.java:1732) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.View.draw(View.java:6996) at android.view.ViewGroup.drawChild(ViewGroup.java:1732) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.View.draw(View.java:6996) at android.view.ViewGroup.drawChild(ViewGroup.java:1732) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)...

Page 12: X Means Y

Copyright © 2012CommonsWare, LLC

StackOverflowError

● Your code?– Too-deep recursion

● View/ViewGroup code?– Too-complex UI: nested containers– 8KB stack space on main application thread

Page 13: X Means Y

Copyright © 2012CommonsWare, LLC

StackOverflowError

● Diagnostics– Open UI in emulator

● Or device if you embed ViewServer in it

– Run Hierarchy View● Eclipse perspective or via monitor

– Count depth– Find stuff safe to eliminate

● Containers with only one child

Page 14: X Means Y

Copyright © 2012CommonsWare, LLC

07-13 10:42:23.820: E/AndroidRuntime(2065):java.lang.OutOfMemoryError

Page 15: X Means Y

Copyright © 2012CommonsWare, LLC

OutOfMemoryError

● Limited heap sizes– 16-64MB, depending on OS version and device

capabilities● Non-compacting garbage collector

– Cannot allocate a block big enough for your request

● Primary culprit: bitmaps– 1080p image = ~8MB

Page 16: X Means Y

Copyright © 2012CommonsWare, LLC

OutOfMemoryError

● Diagnostics– MAT

● Dump heap (HPROF) using Debug or DDMS● Load into MAT (Eclipse plug-in or standalone)● Hunt for culprits

– adb shell dumpsys meminfo● Lots of low-level information● Difficult to accurately interpret

Page 17: X Means Y

Copyright © 2012CommonsWare, LLC

OutOfMemoryError

● BitmapFactory.Options– inSampleSize: downsample for less heap use– inPurgeable/inInputShareable: for sharing

backing data for mutations of common ancestor bitmap

– inBitmap: for reusing existing Bitmap buffer● Must be of the same dimensions and bit depth● Maintain your own object pools versus

fragmenting your heap

Page 18: X Means Y

Copyright © 2012CommonsWare, LLC

I/DEBUG (18110): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***I/DEBUG (18110): Build fingerprint: 'MOTO_O2DE/umts_sholes/umts_sholes/sholes:2.1-...I/DEBUG (18110): pid: 23912, tid: 23913 >>> com.example.test <<<I/DEBUG (18110): signal 11 (SIGSEGV), fault addr 0000000cI/DEBUG (18110): r0 00140120 r1 00000048 r2 00000000 r3 00140168I/DEBUG (18110): r4 afe3ae08 r5 00000006 r6 00140158 r7 0000a000I/DEBUG (18110): r8 100ffcf8 r9 4185eef8 10 4185eee0 fp 001243b0I/DEBUG (18110): ip 00000000 sp 100ffc50 lr afe0f130 pc afe0aeec cpsr 80000070I/DEBUG (18110): #00 pc 0000aeec /system/lib/libc.soI/DEBUG (18110): #01 pc 000008e6 /system/lib/libstdc++.soI/DEBUG (18110): #02 pc 00014124 /system/lib/libutils.soI/DEBUG (18110): #03 pc 0001423c /system/lib/libutils.soI/DEBUG (18110): #04 pc 0001af28 /system/lib/libbinder.soI/DEBUG (18110): #05 pc 0001ba20 /system/lib/libbinder.soI/DEBUG (18110): #06 pc 0001bcd6 /system/lib/libbinder.soI/DEBUG (18110): #07 pc 00014236 /system/lib/libutils.soI/DEBUG (18110): #08 pc 000293a6 /system/lib/libandroid_runtime.soI/DEBUG (18110): #09 pc 000313ca /system/lib/libandroid_runtime.soI/DEBUG (18110): #10 pc 0000ecb4 /system/lib/libdvm.soI/DEBUG (18110): #11 pc 00038838 /system/lib/libdvm.soI/DEBUG (18110): #12 pc 000139b8 /system/lib/libdvm.soI/DEBUG (18110): #13 pc 00019338 /system/lib/libdvm.soI/DEBUG (18110): #14 pc 00018804 /system/lib/libdvm.so...

Page 19: X Means Y

Copyright © 2012CommonsWare, LLC

SIGSEGV

● Doing NDK development?– Might be core dump from your code

● Repeatable across devices, or on Nexus?– Probably a bug in Android

● Only happens on certain ROM?– Probably a bug in the ROM mod

Page 20: X Means Y

Copyright © 2012CommonsWare, LLC

Getting Help

● StackOverflow– android tag

● Google Hangouts by Android Developer Support– QR code for YouTube archives

● www.andglobe.com– Q&A sites, organized by language

Page 21: X Means Y

Copyright © 2012CommonsWare, LLC

Speaker ContactInformation