Is there a way of finding out where my app threw an ANR (Application Not Responding). I took a look at the traces.txt file in /data and I see a trace for my application. This is what I see in the trace.
DALVIK THREADS:
"main" prio=5 tid=3 TIMED_WAIT
| group="main" sCount=1 dsCount=0 s=0 obj=0x400143a8
| sysTid=691 nice=0 sched=0/0 handle=-1091117924
at java.lang.Object.wait(Native Method)
- waiting on <0x1cd570> (a android.os.MessageQueue)
at java.lang.Object.wait(Object.java:195)
at android.os.MessageQueue.next(MessageQueue.java:144)
at android.os.Looper.loop(Looper.java:110)
at android.app.ActivityThread.main(ActivityThread.java:3742)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
at dalvik.system.NativeStart.main(Native Method)
"Binder Thread #3" prio=5 tid=15 NATIVE
| group="main" sCount=1 dsCount=0 s=0 obj=0x434e7758
| sysTid=734 nice=0 sched=0/0 handle=1733632
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #2" prio=5 tid=13 NATIVE
| group="main" sCount=1 dsCount=0 s=0 obj=0x433af808
| sysTid=696 nice=0 sched=0/0 handle=1369840
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #1" prio=5 tid=11 NATIVE
| group="main" sCount=1 dsCount=0 s=0 obj=0x433aca10
| sysTid=695 nice=0 sched=0/0 handle=1367448
at dalvik.system.NativeStart.run(Native Method)
"JDWP" daemon prio=5 tid=9 VMWAIT
| group="system" sCount=1 dsCount=0 s=0 obj=0x433ac2a0
| sysTid=694 nice=0 sched=0/0 handle=1367136
at dalvik.system.NativeStart.run(Native Method)
"Signal Catcher" daemon prio=5 tid=7 RUNNABLE
| group="system" sCount=0 dsCount=0 s=0 obj=0x433ac1e8
| sysTid=693 nice=0 sched=0/0 handle=1366712
at dalvik.system.NativeStart.run(Native Method)
"HeapWorker" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 s=0 obj=0x4253ef88
| sysTid=692 nice=0 sched=0/0 handle=1366472
at dalvik.system.NativeStart.run(Native Method)
----- end 691 -----
How can I find out where the problem is? The methods in the trace are all SDK methods.
An ANR happens when some long operation takes place in the "main" thread. This is the event loop thread, and if it is busy, Android cannot process any further GUI events in the application, and thus throws up an ANR dialog.
Now, in the trace you posted, the main thread seems to be doing fine, there is no problem. It is idling in the MessageQueue, waiting for another message to come in. In your case the ANR was likely a longer operation, rather than something that blocked the thread permanently, so the event thread recovered after the operation finished, and your trace went through after the ANR.
Detecting where ANRs happen is easy if it is a permanent block (deadlock acquiring some locks for instance), but harder if it's just a temporary delay. First, go over your code and look for vunerable spots and long running operations. Examples may include using sockets, locks, thread sleeps, and other blocking operations from within the event thread. You should make sure these all happen in separate threads. If nothing seems the problem, use DDMS and enable the thread view. This shows all the threads in your application similar to the trace you have. Reproduce the ANR, and refresh the main thread at the same time. That should show you precisely whats going on at the time of the ANR
You can enable StrictMode in API level 9 and above.
StrictMode is most commonly used to catch accidental disk or network
access on the application's main thread, where UI operations are
received and animations take place. By keeping your application's main thread
responsive, you also prevent ANR dialogs from being shown to users.
public void onCreate() {
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDeath()
.build());
super.onCreate();
}
using penaltyLog() you can watch the output of adb logcat while you
use your application to see the violations as they happen.
You are wondering which task hold a UI Thread. Trace file gives you a hint to find the task. you need investigate a state of each thread
State of thread
running - executing application code
sleeping - called Thread.sleep()
monitor - waiting to acquire a monitor lock
wait - in Object.wait()
native - executing native code
vmwait - waiting on a VM resource
zombie - thread is in the process of dying
init - thread is initializing (you shouldn't see this)
starting - thread is about to start (you shouldn't see this either)
Focus on SUSPENDED, MONITOR state. Monitor state indicates which thread is investigated and SUSPENDED state of the thread is probably main reason for deadlock.
Basic investigate steps
Find "waiting to lock"
you can find monitor state "Binder Thread #15" prio=5 tid=75 MONITOR
you are lucky if find "waiting to lock"
example : waiting to lock <0xblahblah> (a com.foo.A) held by threadid=74
You can notice that "tid=74" hold a task now. So go to tid=74
tid=74 maybe SUSPENDED state! find main reason!
trace does not always contain "waiting to lock". in this case it is hard to find main reason.
I've been learning android for the last few months, so I'm far from an expert, but I've been really disappointed with the documentation on ANRs.
Most of the advice seems to be geared towards avoiding them or fixing them by blindly looking through your code, which is great, but I couldn't find anything on analyzing the trace.
There are three things you really need to look for with ANR logs.
1) Deadlocks: When a thread is in the WAIT state, you can look through the details to find who it's "heldby=". Most of the time, it'll be held by itself, but if it's held by another thread, that's likely to be a danger sign. Go look at that thread and see what it's held by. You might find a loop, which is a clear sign that something has gone wrong. This is pretty rare, but it's the first point because when it happens, it's a nightmare
2) Main thread Waiting: If your main thread is in the WAIT state, check if it's held by another thread. This shouldn't happen, because your UI thread shouldn't be held by a background thread.
Both of these scenarios, mean you need to rework your code significantly.
3) Heavy operations on the main thread: This is the most common cause of ANRs, but sometimes one of the harder to find and fix. Look at the main thread details. Scroll down the stack trace and until you see classes you recognize (from your app). Look at the methods in the trace and figure out if you're making network calls, db calls, etc. in these places.
Finally, and I apologize for shamelessly plugging my own code, you can use the python log analyzer I wrote at https://github.com/HarshEvilGeek/Android-Log-Analyzer This will go through your log files, open ANR files, find deadlocks, find waiting main threads, find uncaught exceptions in your agent logs and print it all out on the screen in a relatively easy to read manner. Read the ReadMe file (which I'm about to add) to learn how to use it. It's helped me a ton in the last week!
You need to look for "waiting to lock" in /data/anr/traces.txt file
for more details: Engineer for High Performance with Tools from Android & Play (Google I/O '17)
Whenever you're analyzing timing issues, debugging often does not help, as freezing the app at a breakpoint will make the problem go away.
Your best bet is to insert lots of logging calls (Log.XXX()) into the app's different threads and callbacks and see where the delay is at. If you need a stacktrace, create a new Exception (just instantiate one) and log it.
What Triggers ANR?
Generally, the system displays an ANR if an application cannot respond to user input.
In any situation in which your app performs a potentially lengthy operation, you should not perform the work on the UI thread, but instead create a worker thread and do most of the work there. This keeps the UI thread (which drives the user interface event loop) running and prevents the system from concluding that your code has frozen.
How to Avoid ANRs
Android applications normally run entirely on a single thread by default the "UI thread" or "main thread"). This means anything your application is doing in the UI thread that takes a long time to complete can trigger the ANR dialog because your application is not giving itself a chance to handle the input event or intent broadcasts.
Therefore, any method that runs in the UI thread should do as little work as possible on that thread. In particular, activities should do as little as possible to set up in key life-cycle methods such as onCreate() and onResume(). Potentially long running operations such as network or database operations, or computationally expensive calculations such as resizing bitmaps should be done in a worker thread (or in the case of databases operations, via an asynchronous request).
Code: Worker thread with the AsyncTask class
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
// Do the long-running work in here
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
// This is called each time you call publishProgress()
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
// This is called when doInBackground() is finished
protected void onPostExecute(Long result) {
showNotification("Downloaded " + result + " bytes");
}
}
Code: Execute Worker thread
To execute this worker thread, simply create an instance and call execute():
new DownloadFilesTask().execute(url1, url2, url3);
Source
http://developer.android.com/training/articles/perf-anr.html
Basic on #Horyun Lee answer, I wrote a small python script to help to investigate ANR from traces.txt.
The ANRs will output as graphics by graphviz if you have installed grapvhviz on your system.
$ ./anr.py --format png ./traces.txt
A png will output like below if there are ANRs detected in file traces.txt. It's more intuitive.
The sample traces.txt file used above was get from here.
Consider using the ANR-Watchdog library to accurately track and capture ANR stack traces in a high level of detail. You can then send them to your crash reporting library. I recommend using setReportMainThreadOnly() in this scenario. You can either make the app throw a non-fatal exception of the freeze point, or make the app force quit when the ANR happens.
Note that the standard ANR reports sent to your Google Play Developer console are often not accurate enough to pinpoint the exact problem. That's why a third-party library is needed.
not sure if this will help. My issue was the app crashes and freezes my devices then forced it to restart on devices with android 10 but runs fine with the android 6 nothing showed in the logcat. The crashes wasn't easy to reproduce and very unpredictable.
I spent almost 2 wks searching and troubleshooting with ANR but no avail.
Finally syncing the gradle fixed all issues..... rookie mistake.
Hope this would help someone.
my issue with ANR , after much work i found out that a thread was calling a resource that did not exist in the layout, instead of returning an exception , i got ANR ...
For published apps, Google play console itself shows Exact ANRs report like crash report. It will show you all the information which class or Thread is causing ANR, Occurances and all other details.
Your App Dashboard-> Explore Android Vitals Tab -> View Core Vitals Detail (ANR section)-> Select your apk or artifect vesrion -> Crashes and ANR page just select ANRs from drop down. It will list all the ANRs.
You can navigate to specific ANR and check details.
After searching for many solutions for solving ANR crashes on Android, I stumbled onto this link:
https://developer.android.com/studio/debug/bug-report
Connect your android device to a computer with ADB installed
Open terminal
Run the following command (replace "path" with where you want your zip folder saved e.g. ~/folder/report):
adb bugreport
This enabled me to access all ANR (anr* files) bug reports that gave me a full tracebacks under the folder:
FS -> data -> anr -> anr* files
I know this question is probably asked few times but I can not find a valid answer for my case.
In my logcat I get
D/AndroidRuntime﹕ Shutting down VM
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418337c0)
which indicates I have a crash somewhere, but I can not see it in log, there is no more information about the crash.
I have tried
adb server -kill, adb server-start.
I closed the adb from the proccess in Task Manager and closed AndroidStudio and started everything again (I tried restart windows too).
I am using CrashLytics in my application, but I do not receive crash there either, however TestFairy was able to give me finally some information which is application ANR and the next log (sorry about messy log)
java.lang.Object.wait(Native Method)
java.lang.Thread.parkFor(Thread.java:1231)
sun.misc.Unsafe.park(Unsafe.java:323)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:157)
java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:813)
java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:846)
java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1176)
java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:185)
java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:261)
com.google.android.gms.analytics.zzx.zzfj(Unknown Source)
com.google.android.gms.analytics.GoogleAnalytics.zzfj(Unknown Source)
com.google.android.gms.analytics.ExceptionReporter.uncaughtException(Unknown Source)
java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
dalvik.system.NativeStart.main(Native Method)
so I tried to search around and some says it can be a crash in GoogleAnalytics while trying to report another crash. so I disabled this code
UncaughtExceptionHandler myHandler = new ExceptionReporter(
mTracker,Thread.getDefaultUncaughtExceptionHandler(),
getContext().getApplicationContext());
Thread.setDefaultUncaughtExceptionHandler(myHandler);
I know that this issue happens after trying to open a fragment but when I debug the fragment is not started yet.
The problem is I still can not see any more information, so How can I see log or more information? Thanks and sorry for long post.
An ANR will occur if you are running a process on the UI thread which takes a long time, usually around 5 seconds. During this time the GUI (Graphical User Interface) will lock up which will result in anything the user presses will not be actioned. After the 5 seconds approx has occurred, if the thread still hasn't recovered then an ANR dialogue box is shown informing the user that the application is not responding and will give the user the choice to either wait, in the hope that the app will eventually recover, or to force close the app.
The application I am working on, works correctly when the screen is on, and for any length of time, but as soon as the phone goes to sleep and 10 seconds have elapsed, I get an error in my logcat,
I/art Thread[5,tid=318,WaitingInMainSignalCatcherLoop,Thread*=0xaf60e400,peer=0x12c00080,"Signal Catcher"]: reacting to signal 3
I pulled the traces file and tried to understand what was causing this issue and read through androids page on application performance, I have figured out that, it is being caused because a BroadcastReceiver hasn't executed in 10 seconds,
A BroadcastReceiver hasn't finished executing within 10 seconds.
From reading the traces file, I believe it has something to do with the timeout of the screen being broadcast and the action not being completed.
"ActivityManager" prio=5 tid=16 TimedWaiting
| group="main" sCount=1 dsCount=0 obj=0x12d41970 self=0xac884400
| sysTid=859 nice=-2 cgrp=apps sched=0/0 handle=0xb50e9e80
| state=S schedstat=( 443987410679 259509083288 699634 ) utm=18416 stm=25982 core=2 HZ=100
| stack=0xa14fa000-0xa14fc000 stackSize=1036KB
| held mutexes=
at java.lang.Object.wait!(Native method)
- waiting on <0x03423764> (a com.android.server.am.ActivityManagerService$6)
at java.lang.Object.wait(Object.java:422)
at com.android.server.am.ActivityManagerService.dumpStackTraces(ActivityManagerService.java:4969)
- locked <0x03423764> (a com.android.server.am.ActivityManagerService$6)
at com.android.server.am.ActivityManagerService.dumpStackTraces(ActivityManagerService.java:4946)
at com.android.server.am.ActivityManagerService.appNotResponding(ActivityManagerService.java:5187)
at com.android.server.am.BroadcastQueue$AppNotResponding.run(BroadcastQueue.java:171)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
Can someone please point me in the right direction to resolve this issue?
How can I find the root cause of the issue.
What I had tried (in my ignorance) I tried removing all the Broadcast Receivers in the code, that didn't help., obviously.
I got rid of the wakeLocks in an attempt to resolve the issue, but that also didn't help.
Any help is greatly appreciated.
I'm having the same problem here with an operation that should not take so much time, but for some reason, it is.
From the official Android Documentation:
The specific constraint on BroadcastReceiver execution time emphasizes what broadcast receivers are meant to do: small, discrete amounts of work in the background such as saving a setting or registering a Notification. So as with other methods called in the UI thread, applications should avoid potentially long-running operations or calculations in a broadcast receiver. But instead of doing intensive tasks via worker threads, your application should start an IntentService if a potentially long running action needs to be taken in response to an intent broadcast.
http://developer.android.com/training/articles/perf-anr.html
I got mail from a user who has a lot of apps installed that he has problems when my app gathers activity info with this code:
getPackageManager().queryIntentActivities(mAinIntent, 0)
whole source here: https://github.com/ligi/FAST
this is what happens
Caused by: java.lang.RuntimeException: Package manager has died
at android.app.ApplicationPackageManager.queryIntentActivities(ApplicationPackageManager.java:479)
at org.ligi.fast.BaseAppGatherAsyncTask.doInBackground(BaseAppGatherAsyncTask.java:34)
at org.ligi.fast.BaseAppGatherAsyncTask.doInBackground(BaseAppGatherAsyncTask.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more
Caused by: android.os.TransactionTooLargeExceptionTransactionTooLargeException
at android.os.BinderProxy.transact(Native Method)
at android.content.pm.IPackageManager$Stub$Proxy.queryIntentActivities(IPackageManager.java:2230)
at android.app.ApplicationPackageManager.queryIntentActivities(ApplicationPackageManager.java:473)
... 9 more
Log:
0 D: Writing unhandled exception to: /data/data/org.ligi.fast/files/3.7-1364933885194.tracedroid
there seems to be a problem that I am running against the 1mb border, but how to get out of there? How else can I get the needed info? Is there a way to chunk up the data?
I ran into this error a while ago with the same user input. Though my thrown event was different. I ended up catching the exception and reporting back the user gracefully that there were too many apps installed with the ability to handle . Out of several hundred thousand installs I have only seen this error off-handedly less than five times for a project, I know that isn't an excuse but some devices just don't have the heap to handle indexing the intents of each application installed on the device.
I am debugging an Android application and one of the activities just failed silently; it popped off the back stack and I got the previous activity.
I've seen silent failures of this type that can be attributed to memory problems, but in this case I am testing without the debugger attached. The logcat shows virtually no information: after some output from our touch listeners, I get
I/DEBUG(85): debuggerd committing suicide to free the zombie!
I/DEBUG(24919): debuggerd: Jul 8 2011 06:16:01
I/ActivityManager(157): Process com.tse.newsreader (pid 24415) has died.
I/WindowManager(157): WIN DEATH: Window{4108f938 com.tse.newsreader/com.tse.newsreader.activities.StorefrontFragmentActivity paused=false}
com.tse.newsreader is our process.
I would like to know if this is caused by a memory leak somewhere and I was hoping to add an HPROF dump to a global exception handler, somewhat as described in Is there a way to take a memory dump on app crash? but if there is no exception or OutOfMemoryError to catch, I don't see how that will help.
Can anyone suggest how I can get at the cause of these silent failures?
I'm not sure if this is what you mean, but you could consider adding something like bugsense to your app. It will pick up uncaught exceptions.