android ANR產生原因(三總常見型別)和解決辦法
轉載:http://blog.csdn.net/lonlylove/article/details/42006285
記得好久之前,面試時候,面試官問我,造成ANR的原因有哪些?
答: 自我感覺良好的我,就說了好多(包括了經典中兩個),最後面試官說了句,其實就三種經典的,就文章中那三種,,,,今天看到這篇文章,儲存下當前的年輕歲月吧。
1)什麼引發了ANR?
在Android裡,應用程式的響應性是由Activity Manager和WindowManager系統服務監視的。當它監測到以下情況中的一個時,Android就會針對特定的應用程式顯示ANR:
在5秒內沒有響應輸入的事件(例如,按鍵按下,螢幕觸控)
BroadcastReceiver在10秒內沒有執行完畢
一個ANR對話方塊顯示給使用者
2)如何避免ANR?
考慮上面的ANR定義,讓我們來研究一下為什麼它會在Android應用程式裡發生和如何最佳構建應用程式來避免ANR。
Android應用程式通常是執行在一個單獨的執行緒(例如,main)裡。這意味著你的應用程式所做的事情如果在主執行緒裡佔用了太長的時間的話,就會引發ANR對話方塊,因為你的應用程式並沒有給自己機會來處理輸入事件或者Intent廣播。
因此,執行在主執行緒裡的任何方法都儘可能少做事情。特別是,Activity應該在它的關鍵生命週期方法(如onCreate()和onResume())裡儘可能少的去做建立操作。潛在的耗時操作,例如網路或資料庫操作,或者高耗時的計算如改變點陣圖尺寸,應該在子執行緒裡(或者以資料庫操作為例,通過非同步請求的方式)來完成。然而,不是說你的主執行緒阻塞在那裡等待子執行緒的完成——也不是呼叫Thread.wait()或是Thread.sleep()。替代的方法是,主執行緒應該為子執行緒提供一個Handler,以便完成時能夠提交給主執行緒。以這種方式設計你的應用程式,將能保證你的主執行緒保持對輸入的響應性並能避免由於5秒輸入事件的超時引發的ANR對話方塊。這種做法應該在其它顯示UI的執行緒裡效仿,因為它們都受相同的超時影響。
IntentReceiver執行時間的特殊限制意味著它應該做:在後臺裡做小的、瑣碎的工作如儲存設定或者註冊一個Notification。和在主執行緒裡呼叫的其它方法一樣,應用程式應該避免在BroadcastReceiver裡做耗時的操作或計算。但不再是在子執行緒裡做這些任務(因為BroadcastReceiver的生命週期短),替代的是,如果響應Intent廣播需要執行一個耗時的動作的話,應用程式應該啟動一個Service。順便提及一句,你也應該避免在IntentReceiver裡啟動一個Activity,因為它會建立一個新的畫面,並從當前使用者正在執行的程式上搶奪焦點。如果你的應用程式在響應Intent廣播時需要向用戶展示什麼,你應該使用Notification Manager來實現。
3)增強響應靈敏性
一般來說,在應用程式裡,100到200ms是使用者能感知阻滯的時間閾值。因此,這裡有一些額外的技巧來避免ANR,並有助於讓你的應用程式看起來有響應性。
如果你的應用程式為響應使用者輸入正在後臺工作的話,可以顯示工作的進度(ProgressBar和ProgressDialog對這種情況來說很有用)。
特別是遊戲,在子執行緒裡做移動的計算。
如果你的應用程式有一個耗時的初始化過程的話,考慮可以顯示一個SplashScreen或者快速顯示主畫面並非同步來填充這些資訊。在這兩種情況下,你都應該顯示正在進行的進度,以免使用者認為應用程式被凍結了。
15:59:37 I/ActivityManager(130): ANR in process: com.android.email(last in com.android.email)
=>frameworks\base\services\java\com\android\server\am\ActivityManagerService.java
=>提示輸出cpu資訊
Annotation: keyDispatchingTimedOut
CPU usage:
=>frameworks\base\services\java\com\android\server\ProcessStats.java
=>輸出cpu當前狀態
=>/proc/loadavg 顯示cpu負荷
=>1-分鐘平均負載 / 5-分鐘平均負載 / 15-分鐘平均負載 Load: 4.37 /4.55 / 3.97
=>cpu狀態的時間段
CPU usage from 10987ms to 27ms ago: =>/proc/state讀取cpu的使用情況
=>http://linux.die.net/man/5/proc
=>user
=>kernel
=>iowait
=>irq ->0
=>softirq ->0
=>minor
The number of minor faults the process has madewhich have not required loading a memory page fromdisk. =>major
The number of major faults the process has madewhich have required loading a memory page from disk. system_server: 12% = 4% user + 7% kernel /faults: 1886 minor
m.android.email: 12% = 6% user + 5% kernel /faults: 2716 minor
sensorserver_ya: 7% = 0% user + 7%kernel
breeze.launcher: 3% = 0% user + 3% kernel /faults: 94 minor
ocess.msn.shell: 0% = 0% user + 0% kernel /faults: 38 minor
m.android.phone: 0% = 0% user + 0%kernel
alog: 0% = 0% user + 0% kernel
rpcrotuer_smd_x: 0% = 0% user + 0%kernel
rild: 0% = 0% user + 0% kernel
alog: 0% = 0% user + 0% kernel
events/0: 0% = 0% user + 0% kernel
port-bridge: 0% = 0% user + 0% kernel
TOTAL: 81% = 13% user + 25% kernel + 42% iowait
15:59:37 I/ActivityManager(130): Removing old ANR trace file from/data/anr/traces.txt
Android ANR這個錯誤大家並不陌生,但是從Android2.2開始出錯的ANR資訊會自動上傳給Google進行系統分析改進,當然了你的應用ANR錯誤其實儲存在一個檔案中,在/data/anr/traces.txt檔案中.
下面一起來分析下錯誤吧,第一行為出錯的時間,第二行都會寫上發生ANR的packageName,下文是com.android.systemui這個包,裡面的部分執行緒出了問題,通過下面的的xxx方法以及對應的java檔案,後面的數字為xxx.java檔案的第幾行,是不是很方便呢?
----- pid 125 at 2011-02-22 05:18:01 -----
Cmd line: com.android.systemui
DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x4001f1a8self=0xce48
| sysTid=125 nice=0 sched=0/0 cgrp=defaulthandle=-1345006528
| schedstat=( 981213067 8042604425 151 )
at android.os.BinderProxy.transact(NativeMethod)
atandroid.os.storage.IMountService$Stub$Proxy.isUsbMassStorageConnected(IMountService.java:95)
atandroid.os.storage.StorageManager.isUsbMassStorageConnected(StorageManager.java:385)
atcom.android.systemui.usb.StorageNotification.<init>(StorageNotification.java:71)
atcom.android.systemui.statusbar.policy.StatusBarPolicy.<init>(StatusBarPolicy.java:412)
atcom.android.systemui.statusbar.StatusBarService.onCreate(StatusBarService.java:239)
atandroid.app.ActivityThread.handleCreateService(ActivityThread.java:1920)
atandroid.app.ActivityThread.access$2500(ActivityThread.java:117)
atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:982)
atandroid.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
atandroid.app.ActivityThread.main(ActivityThread.java:3647)
at java.lang.reflect.Method.invokeNative(NativeMethod)
atjava.lang.reflect.Method.invoke(Method.java:507)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(NativeMethod)
"Binder Thread #2" prio=5 tid=8 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40511270self=0x1c1100
| sysTid=153 nice=0 sched=0/0 cgrp=defaulthandle=1141608
| schedstat=( 27181321 64708345 5 )
at dalvik.system.NativeStart.run(NativeMethod)
"Binder Thread #1" prio=5 tid=7 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x405111a8self=0x1349c8
| sysTid=152 nice=0 sched=0/0 cgrp=defaulthandle=1264008
| schedstat=( 31857510 74284459 5 )
at dalvik.system.NativeStart.run(NativeMethod)
"Compiler" daemon prio=5 tid=6 VMWAIT
| group="system" sCount=1 dsCount=0obj=0x4050dd10 self=0x116538
| sysTid=144 nice=0 sched=0/0 cgrp=defaulthandle=982520
| schedstat=( 7319346 103454345 12 )
at dalvik.system.NativeStart.run(NativeMethod)
"JDWP" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0obj=0x4050dc60 self=0x116400
| sysTid=142 nice=0 sched=0/0 cgrp=defaulthandle=986536
| schedstat=( 32876840 393298925 20 )
at dalvik.system.NativeStart.run(NativeMethod)
"Signal Catcher" daemon prio=5 tid=4 RUNNABLE
| group="system" sCount=0 dsCount=0obj=0x4050dba0 self=0x253ab0
| sysTid=141 nice=0 sched=0/0 cgrp=defaulthandle=2439792
| schedstat=( 94447996 796994478 19 )
at dalvik.system.NativeStart.run(NativeMethod)
"GC" daemon prio=5 tid=3 VMWAIT
| group="system" sCount=1 dsCount=0obj=0x4050daf8 self=0x8fd40
| sysTid=128 nice=0 sched=0/0 cgrp=defaulthandle=2439592
| schedstat=( 103352102 819201761 21 )
at dalvik.system.NativeStart.run(NativeMethod)
"HeapWorker" daemon prio=5 tid=2 VMWAIT
| group="system" sCount=1 dsCount=0obj=0x4050da40 self=0xf0c70
| sysTid=127 nice=0 sched=0/0 cgrp=defaulthandle=2439528
| schedstat=( 971134410 6445300652 103 )
at dalvik.system.NativeStart.run(NativeMethod)
二:ANR的常見型別
ANR一般有三種類型:
1:KeyDispatchTimeout(5 seconds) --主要型別
按鍵或觸控事件在特定時間內無響應
2:BroadcastTimeout(10 seconds)
BroadcastReceiver在特定時間內無法處理完成
3:ServiceTimeout(20 seconds) --小概率型別
Service在特定的時間內無法處理完成
三:KeyDispatchTimeout
Akey or touch event was not dispatched within the specified time(按鍵或觸控事件在特定時間內無響應)
具體的超時時間的定義在framework下的
ActivityManagerService.java
//How long we wait until we timeout on key dispatching.
staticfinal int KEY_DISPATCHING_TIMEOUT = 5*1000
四:為什麼會超時呢?
超時時間的計數一般是從按鍵分發給app開始。超時的原因一般有兩種:
(1)當前的事件沒有機會得到處理(即UI執行緒正在處理前一個事件,沒有及時的完成或者looper被某種原因阻塞住了)
(2)當前的事件正在處理,但沒有及時完成
五:如何避免KeyDispatchTimeout
1:UI執行緒儘量只做跟UI相關的工作
2:耗時的工作(比如資料庫操作,I/O,連線網路或者別的有可能阻礙UI執行緒的操作)把它放入單獨的執行緒處理
3:儘量用Handler來處理UIthread和別的thread之間的互動
六:UI執行緒
說了那麼多的UI執行緒,那麼哪些屬於UI執行緒呢?
UI執行緒主要包括如下:
-
Activity:onCreate(), onResume(), onDestroy(), onKeyDown(), onClick(),etc
-
AsyncTask: onPreExecute(), onProgressUpdate(), onPostExecute(), onCancel,etc
-
Mainthread handler: handleMessage(), post*(runnable r), etc
-
other
七:如何去分析ANR
先看個LOG:
04-01 13:12:11.572 I/InputDispatcher( 220): Application is not responding:Window{2b263310com.android.email/com.android.email.activity.SplitScreenActivitypaused=false}. 5009.8ms since event, 5009.5ms since waitstarted
04-0113:12:11.572 I/WindowManager( 220): Input event dispatching timedout sending tocom.android.email/com.android.email.activity.SplitScreenActivity
04-01 13:12:14.123 I/Process( 220): Sending signal. PID: 21404 SIG: 3---發生ANR的時間和生成trace.txt的時間
04-01 13:12:14.123 I/dalvikvm(21404):threadid=4: reacting to signal 3
……
04-0113:12:15.872 E/ActivityManager( 220): ANR in com.android.email(com.android.email/.activity.SplitScreenActivity)
04-0113:12:15.872 E/ActivityManager( 220): Reason:keyDispatchingTimedOut
04-0113:12:15.872 E/ActivityManager( 220): Load: 8.68 / 8.37 / 8.53
04-0113:12:15.872 E/ActivityManager( 220):CPUusage from 4361ms to 699ms ago----CPU在ANR發生前的使用情況
04-0113:12:15.872 E/ActivityManager( 220): 5.5%21404/com.android.email: 1.3% user + 4.1% kernel / faults: 10 minor
04-0113:12:15.872 E/ActivityManager( 220): 4.3%220/system_server: 2.7% user + 1.5% kernel / faults: 11 minor 2 major
04-0113:12:15.872 E/ActivityManager( 220): 0.9%52/spi_qsd.0: 0% user + 0.9% kernel
04-0113:12:15.872 E/ActivityManager( 220): 0.5%65/irq/170-cyttsp-: 0% user + 0.5% kernel
04-0113:12:15.872 E/ActivityManager( 220): 0.5%296/com.android.systemui: 0.5% user + 0% kernel
04-0113:12:15.872 E/ActivityManager( 220): 100%TOTAL: 4.8% user + 7.6% kernel + 87% iowait
04-0113:12:15.872 E/ActivityManager( 220):CPUusage from 3697ms to 4223ms later:-- ANR後CPU的使用量
04-0113:12:15.872 E/ActivityManager( 220): 25%21404/com.android.email: 25% user + 0% kernel / faults: 191 minor
04-0113:12:15.872 E/ActivityManager( 220): 16% 21603/__eas(par.hakan: 16% user + 0% kernel
04-0113:12:15.872 E/ActivityManager( 220): 7.2% 21406/GC: 7.2% user + 0% kernel
04-0113:12:15.872 E/ActivityManager( 220): 1.8% 21409/Compiler: 1.8% user + 0% kernel
04-0113:12:15.872 E/ActivityManager( 220): 5.5%220/system_server: 0% user + 5.5% kernel / faults: 1 minor
04-0113:12:15.872 E/ActivityManager( 220): 5.5% 263/InputDispatcher: 0% user + 5.5% kernel
04-0113:12:15.872 E/ActivityManager( 220): 32%TOTAL: 28% user + 3.7% kernel
從LOG可以看出ANR的型別,CPU的使用情況,如果CPU使用量接近100%,說明當前裝置很忙,有可能是CPU飢餓導致了ANR
如果CPU使用量很少,說明主執行緒被BLOCK了
如果IOwait很高,說明ANR有可能是主執行緒在進行I/O操作造成的
除了看LOG,解決ANR還得需要trace.txt檔案,
如何獲取呢?可以用如下命令獲取
-
$chmod 777 /data/anr
-
$rm /data/anr/traces.txt
-
$ps
-
$kill -3PID
-
adbpull data/anr/traces.txt ./mytraces.txt
從trace.txt檔案,看到最多的是如下的資訊:
-----pid 21404 at 2011-04-0113:12:14 -----
Cmdline: com.android.email
DALVIK THREADS:
(mutexes: tll=0tsl=0 tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1NATIVE
| group="main" sCount=1 dsCount=0obj=0x2aad2248 self=0xcf70
| sysTid=21404 nice=0 sched=0/0cgrp=[fopen-error:2] handle=1876218976
atandroid.os.MessageQueue.nativePollOnce(Native Method)
atandroid.os.MessageQueue.next(MessageQueue.java:119)
atandroid.os.Looper.loop(Looper.java:110)
at android.app.ActivityThread.main(ActivityThread.java:3688)
at java.lang.reflect.Method.invokeNative(Native Method)
atjava.lang.reflect.Method.invoke(Method.java:507)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
at dalvik.system.NativeStart.main(Native Method)
說明主執行緒在等待下條訊息進入訊息佇列
八:Thread狀態
ThreadState (defined at “dalvik/vm/thread.h “)
THREAD_UNDEFINED = -1, /* makes enum compatible with int32_t */
THREAD_ZOMBIE = 0, /* TERMINATED */
THREAD_RUNNING = 1, /* RUNNABLE or running now */
THREAD_TIMED_WAIT = 2, /* TIMED_WAITING in Object.wait() */
THREAD_MONITOR = 3, /* BLOCKED on a monitor */
THREAD_WAIT = 4, /* WAITING in Object.wait() */
THREAD_INITIALIZING= 5, /* allocated, not yet running */
THREAD_STARTING = 6, /* started, not yet on thread list */
THREAD_NATIVE = 7, /* off in a JNI native method */
THREAD_VMWAIT = 8, /* waiting on a VM resource */
THREAD_SUSPENDED = 9, /* suspended, usually by GC or debugger */
九:如何調查並解決ANR
1:首先分析log
2: 從trace.txt檔案檢視呼叫stack.
3: 看程式碼
4:仔細檢視ANR的成因(iowait?block?memoryleak?)
十:案例
案例1:關鍵詞:ContentResolver in AsyncTask onPostExecute, high iowait
Process:com.android.email
Activity:com.android.email/.activity.MessageView
Subject:keyDispatchingTimedOut
CPU usage from 2550ms to -2814ms ago:
5%187/system_server: 3.5% user + 1.4% kernel / faults: 86 minor 20major
4.4% 1134/com.android.email: 0.7% user + 3.7% kernel /faults: 38 minor 19 major
4% 372/com.android.eventstream: 0.7%user + 3.3% kernel / faults: 6 minor
1.1% 272/com.android.phone:0.9% user + 0.1% kernel / faults: 33 minor
0.9%252/com.android.systemui: 0.9% user + 0% kernel
0%409/com.android.eventstream.telephonyplugin: 0% user + 0% kernel /faults: 2 minor
0.1% 632/com.android.devicemonitor: 0.1% user + 0%kernel
100%TOTAL: 6.9% user + 8.2% kernel +84%iowait
-----pid 1134 at 2010-12-17 17:46:51 -----
Cmd line:com.android.email
DALVIK THREADS:
(mutexes: tll=0 tsl=0tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1 WAIT
|group="main" sCount=1 dsCount=0 obj=0x2aaca180self=0xcf20
| sysTid=1134 nice=0 sched=0/0 cgrp=[fopen-error:2]handle=1876218976
at java.lang.Object.wait(Native Method)
-waiting on <0x2aaca218> (a java.lang.VMThread)
atjava.lang.Thread.parkFor(Thread.java:1424)
atjava.lang.LangAccessImpl.parkFor(LangAccessImpl.java:48)
atsun.misc.Unsafe.park(Unsafe.java:337)
atjava.util.concurrent.locks.LockSupport.park(LockSupport.java:157)
atjava.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:808)
atjava.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:841)
atjava.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1171)
atjava.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:200)
atjava.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:261)
atandroid.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:378)
atandroid.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:222)
atandroid.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
atandroid.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
atandroid.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1235)
atandroid.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189)
atandroid.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1271)
atcom.android.email.provider.EmailProvider.query(EmailProvider.java:1098)
atandroid.content.ContentProvider$Transport.query(ContentProvider.java:187)
atandroid.content.ContentResolver.query(ContentResolver.java:268)
atcom.android.email.provider.EmailContent$Message.restoreMessageWithId(EmailContent.java:648)
atcom.android.email.Controller.setMessageRead(Controller.java:658)
atcom.android.email.activity.MessageView.onMarkAsRead(MessageView.java:700)
atcom.android.email.activity.MessageView.access$2500(MessageView.java:98)
atcom.android.email.activity.MessageView$LoadBodyTask.onPostExecute(MessageView.java:1290)
atcom.android.email.activity.MessageView$LoadBodyTask.onPostExecute(MessageView.java:1255)
atandroid.os.AsyncTask.finish(AsyncTask.java:417)
atandroid.os.AsyncTask.access$300(AsyncTask.java:127)
atandroid.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
atandroid.os.Handler.dispatchMessage(Handler.java:99)
atandroid.os.Looper.loop(Looper.java:123)
atandroid.app.ActivityThread.main(ActivityThread.java:3652)
atjava.lang.reflect.Method.invokeNative(Native Method)
atjava.lang.reflect.Method.invoke(Method.java:507)
atcom.android.internal.os.ZygoteIn
原因:IOWait很高,說明當前系統在忙於I/O,因此資料庫操作被阻塞
原來:
finalMessagemessage=Message.restoreMessageWithId(mProviderContext,messageId);if(message==null){
return;
}
Accountaccount=Account.restoreAccountWithId(mProviderContext,message.mAccountKey);
if(account==null){
return;//isMessagingController returns false for null, but let's make itclear.
}
if(isMessagingController(account)){
new Thread(){
@Override
public void run(){
mLegacyController.processPendingActions(message.mAccountKey);
}
}.start();
}
解決後:
newThread() {finalMessagemessage=Message.restoreMessageWithId(mProviderContext,messageId);
if(message==null){
return;
}
Accountaccount=Account.restoreAccountWithId(mProviderContext,message.mAccountKey);
if(account==null){
return;//isMessagingController returns false for null, but let's make itclear.
}
if(isMessagingController(account)) {
mLegacyController.processPendingActions(message.mAccountKey);
}
}.start();
案例2:關鍵詞:在UI執行緒進行網路資料的讀寫
ANRin process: com.android.mediascape:PhotoViewer (last incom.android.mediascape:PhotoViewer)
Annotation:keyDispatchingTimedOut
CPU usage:
Load: 6.74 / 6.89 / 6.12
CPUusage from 8254ms to 3224ms ago:
ovider.webmedia: 4% = 4% user +0% kernel / faults: 68 minor
system_server: 2% = 1% user + 0%kernel / faults: 18 minor
re-initialized>: 0% = 0% user + 0%kernel / faults: 50 minor
events/0: 0% = 0% user + 0%kernel
TOTAL:7% = 6% user + 1% kernel
DALVIKTHREADS:
""main"" prio=5 tid=3 NATIVE
|group=""main"" sCount=1 dsCount=0 s=Yobj=0x4001b240 self=0xbda8
| sysTid=2579 nice=0 sched=0/0cgrp=unknown handle=-1343993184
atorg.apache.harmony.luni.platform.OSNetworkSystem.receiveStreamImpl(NativeMethod)
atorg.apache.harmony.luni.platform.OSNetworkSystem.receiveStream(OSNetworkSystem.java:478)
atorg.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:565)
atorg.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:87)
atorg.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection$LimitedInputStream.read(HttpURLConnection.java:303)
atjava.io.InputStream.read(InputStream.java:133)
atjava.io.BufferedInputStream.fillbuf(BufferedInputStream.java:157)
atjava.io.BufferedInputStream.read(BufferedInputStream.java:346)
atandroid.graphics.BitmapFactory.nativeDecodeStream(Native Method)
atandroid.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
atcom.android.mediascape.activity.PhotoViewerActivity.getPreviewImage(PhotoViewerActivity.java:4465)
atcom.android.mediascape.activity.PhotoViewerActivity.dispPreview(PhotoViewerActivity.java:4406)
atcom.android.mediascape.activity.PhotoViewerActivity.access$6500(PhotoViewerActivity.java:125)atcom.android.mediascape.activity.PhotoViewerActivity$33$1.run(PhotoViewerActivity.java:4558)
atandroid.os.Handler.handleCallback(Handler.java:587)
atandroid.os.Handler.dispatchMessage(Handler.java:92)
atandroid.os.Looper.loop(Looper.java:123)
atandroid.app.ActivityThread.main(ActivityThread.java:4370)
atjava.lang.reflect.Method.invokeNative(Native Method)
atjava.lang.reflect.Method.invoke(Method.java:521)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
atdalvik.system.NativeStart.main(Native Method)
關於網路連線,在設計的時候可以設定個timeout的時間或者放入獨立的執行緒來處理。
案例3:
關鍵詞:Memoryleak/Thread leak
11-1621:41:42.560 I/ActivityManager( 1190): ANR in process:android.process.acore (last in android.process.acore)
11-1621:41:42.560 I/ActivityManager( 1190): Annotation:keyDispatchingTimedOut
11-16 21:41:42.560 I/ActivityManager(1190): CPU usage:
11-16 21:41:42.560 I/ActivityManager( 1190):Load: 11.5 / 11.1 / 11.09
11-16 21:41:42.560 I/ActivityManager(1190): CPU usage from 9046ms to 4018ms ago:
11-16 21:41:42.560I/ActivityManager( 1190): d.process.acore:98%=
97% user + 0% kernel / faults: 1134 minor
11-16 21:41:42.560I/ActivityManager( 1190): system_server: 0% = 0% user + 0% kernel /faults: 1 minor
11-16 21:41:42.560 I/ActivityManager( 1190): adbd:0% = 0% user + 0% kernel
11-16 21:41:42.560 I/ActivityManager(1190): logcat: 0% = 0% user + 0% kernel
11-16 21:41:42.560I/ActivityManager( 1190): TOTAL:100% = 98% user + 1% kernel
Cmdline: android.process.acore
DALVIK THREADS:
"main"prio=5 tid=3 VMWAIT
|group="main" sCount=1 dsCount=0 s=N obj=0x40026240self=0xbda8
| sysTid=1815 nice=0 sched=0/0 cgrp=unknownhandle=-1344001376
atdalvik.system.VMRuntime.trackExternalAllocation(NativeMethod)atandroid.graphics.Bitmap.nativeCreate(Native Method)
atandroid.graphics.Bitmap.createBitmap(Bitmap.java:468)
atandroid.view.View.buildDrawingCache(View.java:6324)
atandroid.view.View.getDrawingCache(View.java:6178)
atandroid.view.ViewGroup.drawChild(ViewGroup.java:1541)
……
atcom.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
atandroid.view.ViewRoot.draw(ViewRoot.java:1349)
atandroid.view.ViewRoot.performTraversals(ViewRoot.java:1114)
atandroid.view.ViewRoot.handleMessage(ViewRoot.java:1633)
atandroid.os.Handler.dispatchMessage(Handler.java:99)
atandroid.os.Looper.loop(Looper.java:123)
atandroid.app.ActivityThread.main(ActivityThread.java:4370)
atjava.lang.reflect.Method.invokeNative(Native Method)
atjava.lang.reflect.Method.invoke(Method.java:521)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
atdalvik.system.NativeStart.main(Native Method)