I have searched on the internet regarding what an ANR is. And I studied those references as well. But I don't get details regarding a crash in Android.
Can someone tell me the difference between ANR(Android not Responding) and a crash in Android?
ANR stands for Application Not Responding.
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.
A crash is when an exception within the app has been thrown which has not been handled. For example, if you try to set the text of an EditText component, but the EditText is null and there is no try catch statement to catch the exception that your app will crash and will be force closed. The user will not see what caused the crash, they will be shown a dialogue telling that the app has force closed unexpectedly and will give them the option to send a bug report. In this example if you were to look in the bug report you would see the error caused by java.lang.NullPointerException.
ANR (Application Not Responding) is due to handling a long running task in the main (UI) thread. If the main thread is stopped for more than 5 seconds, the user will get an ANR.
Crashes are due to exceptions and errors like NullPointerException, ClassNotFoundException, typecasting or parsing errors, etc. ANR also causes a crash of the application.
Note: Never perform a long-running task on the UI thread.
Reference ANR
ANR and Crash Examples:
This question already has an accepted answer, but I am adding 2 simple examples to understand ANR and Crash better.
ANR:
// this will produce an ANR on your app
int i = 0;
while(true) {
i++;
}
Crash:
// this will crash your app : will produce java.lang.ArithmeticException
int value = 5, i = 0;
int result = value / i;
Application Not Responding (ANR):
ANR will display in the following conditions:
Response to the input event (such as key press or screen touch even) within 5 Sec.
A Broadcast Receiver hasn’t finished executing within 10 Sec.
How to avoid ANRs?
Create a different worker thread for long running operations like database operations, network operations etc.
Reinforce Responsiveness:
In android app usually, 100 to 200 ms is the threshold beyond which user will feel that app is slow. Following are the tips through which we can show application more responsive.
Show progress dialog whenever you are doing any background work and a user is waiting for the response.
For games specifically, do calculations for moves in the worker thread.
Show splash screen if your application has time-consuming initial setup.
Crash:
The crash is unhandled condition into the application and it will forcefully close our application. Some of the examples of crashes are like Nullpointer exception, Illegal state exception etc.
ANR stands for Application Not Responding, which means that your app does not register events on the UI Thread anymore because a long running operation is executed there
ANR: It is called when anything your application is doing in the UI thread that
takes a long time to complete (5 sec approx)
Reference: ANR
Crash: It is called when your Application gets some Error or Exception raised by the DVM
ANR also caused by-
No response to an input event (such as key press or screen touch events) within 5 seconds.
A BroadcastReceiver hasn't finished executing within 10 seconds.
ANR stands for Application Not Responding and its occurs when long operation takes place into Main thread......
Crash are due to exception and error like Nullpoint,
ANR stands for Application Not Responding.
It can occur due to many reasons like if an application blocks on some I/O operation on the UI thread so the system can’t process incoming user input events. Or perhaps the app spends too much time building an elaborate in-memory structure or computing the next move in the UI thread.
Blocking the main thread, won't result in a crash, but a popup will be displayed to let users kills the app after 5 seconds.
But For Crash, the main reason is the human errors.
Most of the time an app crashes is because of a coding/design error made by human
Human Errors
Lack of testing
Null Pointer exception
OutofMemory
Example :
This is common when a programmer makes a reference to an object or variable that does not exist, basically creating a null-pointer error.
If you have a bad connection, that can also make your apps crash. The app could also have memory management problems.
Please see my answer for the type of android specific exception which may cause the crash.
Android Specific Exception
ANR for ex: if You are downloading huge amount data in ui thread, meny other possibilities like insufficient memory etc it will come.. probably it leads to crashes in android , We can't say both are same one follows other
[ANR and Crash Different][1]
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.
ANR: Basically due to a long running task on main thread.
There are some common patterns to look for when diagnosing ANRs:
The app is doing slow operations involving I/O on the main thread.
The app is doing a long calculation on the main thread.
The main thread is doing a synchronous binder call to another process, and that other process is taking a long time to return.
The main thread is blocked waiting for a synchronized block for a long operation that is happening on another thread.
The main thread is in a deadlock with another thread, either in your process or via a binder call. The main thread is not just waiting for a long operation to finish, but is in a deadlock situation.
The following techniques can help you find out which of these causes is causing your ANRs.
CRASH:
Reason for crashs can be many. Some reasons are obvious, like checking for a null value or empty string, but others are more subtle, like passing invalid arguments to an API or even complex multithreaded interactions
This is good article at developer portal.
It gives clarity in detail about ANR.
https://developer.android.com/training/articles/perf-anr.html
I have an Android app that uses a timer to call an AsyncTask every 5-10 seconds (using java.util.Timer and java.util.TimerTask). The AsyncTask gets image data from an Amazon AWS S3 database, and loads an ImageView for the main UI Activity.
This works fine for an hour or two, but then I get a cryptic error message and the app gets killed. The error message comes from "Looper" and says either:
Could not create epoll instance. errno=24
or
Could not create wake pipe
A search on the web seems to indicate the problem may have something to do with file descriptors (too many open file descriptors ?). I've gone through the code, but don't see any place where files, streams, or connections aren't closed.
When the app is killed, logcat has a message from AndroidRuntime that says:
FATAL EXCEPTION: main
Does anyone have a clue about these messages, or how to fix? Thank you!
possible memory leak. Use Leakcanary to detect which exact part of the code is creating this.
I got the same bug in my code when I used an alarm to trigger a task too quickly.
I fixed it by changing my code so that it only got added to the alarm to be run again after the average run time of the method (plus a little extra time just in case). If you add tasks more quickly than they finish executing then you'll fill up the Looper eventually which throws the error.
Essentially all that a Looper is is a queue of things to be run by the thread from my understanding.
Either you can Force close or Wait.
If I press the Wait then everything will be fine. But how do I disable this annoying message, I don't think something wrong but probably takes some more times to init and runs the App, the wait time bit long.
It sounds like what's happening is that you are running a really long computation (for example, fetching something from the internet) on the UI thread. You can get rid of that message by running that computation in a separate thread.
For more information on how to do this, you can refer to this as a starting point: http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html
I'm working on android application which uses WebServices. In that I'm fetchhing images from web and displaying in imageview. It takes time to fetch images from web and sometimes it forcecloses saying "Activity is not responding" and at that time logcat shows warning as
"Key dispatching timed out sending to
com.Test.TestProject/com.Test.TestProject.Activity1
"
I'm not getting why this is happening.Please help me.
Thanks,
Vishakha.
Read the article on painless threading. You are doing the downloading on the UI thread, which will throw ANRs (Activity Not Responding Exceptions) if it hangs for 30 sec or so. You need to do the downloading in a background task, and update the UI on completion.
You should avoid perform long running tasks on the ui thread. Consider using AsyncTask with ProgressDialog bounded.
The error happens because UI Thread is busy with images downloading, so no key events could be dispatched. The os detecs this and closes your app.
I have developed an app to communicate with my own server and published it. However, sometimes the app force closes. I know there is no bug in the code because the app works properly most of the time, but sometimes it is waiting for an answer from the server forever. I think this is due to the fact that so many people are using the app, and the app refreshes every 1 second or so, so this heavy traffic causes the server to take a large amount of time to respond. So how do you take care of such a use case? should I have a use case where if the server does not respond after some time you just stop the app and throw a message saying that the server is not responding or something like that?
Right now, your main application is timing out due to server load. If you put your connection details in a thread, you will be able to avoid having that main thread time out. You can check for updated data from the connection thread (through some shared object) and then present a message to the user if the data has stopped.
It sounds like you have your server communication code within your main Activity. Any code running in this Activity will be run in the main UI thread. If your code sends a request to your server, and is then waiting for a response, the main UI thread is blocked until your server responds. The Android OS recognises that the UI thread has effectively hung, and kills your app.
What you need to do is to separate out the UI code in your Activity from the server communication code. You should move this into an AsyncTask or a new Thread/Handler combination. This will allow the UI to remain responsive even when your server is under load.
Documentation for AsyncTask
Designing for Responsiveness
Android Thread Messaging
Thread example