I have an android app, which listens to a socket from the server and retrieves data in json format and save data in the database. Resource cursor adapter is used to display the data in a list. When the app is idle for 10 minutes or more, it is not reponding. Any solutions?
ANR occurs when the main thread is blocked for a few time. Specifically, 5 seconds in an Activity, 10 seconds in a BroadcastReceiver and 20 seconds in a Service. So, to avoid ANR, you need to ensure that you don't do something like these in you UI thread: reading or writing files, connecting the internet, operating databases and so on, which spend a lot of time. So, if you want to do things above, you may start a new thread to do that. Specifically, AsyncTask ThreadHandler and something like that.
I hope this will help you.
ANR occurs when Android detects the system is unable to respond to user input for more than a few seconds.
CursorWindow﹕ Window is full: requested allocation 396488 bytes,
free space 285853 bytes, window size 2097152 bytes
04-01 05:32:34.328 1598-1607/com.inxed W/CursorWrapperInner﹕
Cursor finalized without prior close()
you need to close the cursor.
ANR display if application cannot respond to the user input.According to the Google doc,you should not perform the work on the UI thread. I recommend you to perform network request or long running operation in worker thread instead of UI thread. for more this
It looks like your DB cursor its full. After saving data from the cursor, close it, set it to null and create a new one so no buffers are kept in memory.
this SO answer gives more information
Related
I'm wondering what the possible drawbacks are to running my queries on the main thread. At the moment I am loading data from a database using Room and am using this data to generate graphs for the user.
I have already tried putting my queries into separate threads using a runnable, but I am having issues with the application attempting to use data which has not yet been loaded due to the separate thread not finishing it's operation in time. I understand that I could implement some sort of listener or notification, but even then, the user will have to wait an undetermined amount of time before they may view the content because the data for the graphs that they are wanting to view never loads before the button that loads them is clicked.
I have done quite a bit of research and found that it is unadvisable to put the queries on the main thread because it could possibly hang up the thread for a long time and give a poor user experience. So, assuming that I will never exceed 1400 data members, should I worry about this? Are there any caveats that I have not been informed of?
Edit: I am testing the device on a Samsung SM-J106B which is running Android 6.0.1 and I have not noticed any impact on performance even when loading the max of 1400 data members.
DB queries could take long enough that they would cause the UI to skip frames causing a bad visual experience or worse trigger an app not responding exception.
I have an android app, which listens to a socket from the server and retrieves data in json format and save data in the database. Resource cursor adapter is used to display the data in a list. When the app is idle for 10 minutes or more, it is not reponding. Any solutions?
ANR occurs when the main thread is blocked for a few time. Specifically, 5 seconds in an Activity, 10 seconds in a BroadcastReceiver and 20 seconds in a Service. So, to avoid ANR, you need to ensure that you don't do something like these in you UI thread: reading or writing files, connecting the internet, operating databases and so on, which spend a lot of time. So, if you want to do things above, you may start a new thread to do that. Specifically, AsyncTask ThreadHandler and something like that.
I hope this will help you.
ANR occurs when Android detects the system is unable to respond to user input for more than a few seconds.
CursorWindow﹕ Window is full: requested allocation 396488 bytes,
free space 285853 bytes, window size 2097152 bytes
04-01 05:32:34.328 1598-1607/com.inxed W/CursorWrapperInner﹕
Cursor finalized without prior close()
you need to close the cursor.
ANR display if application cannot respond to the user input.According to the Google doc,you should not perform the work on the UI thread. I recommend you to perform network request or long running operation in worker thread instead of UI thread. for more this
It looks like your DB cursor its full. After saving data from the cursor, close it, set it to null and create a new one so no buffers are kept in memory.
this SO answer gives more information
I have an Android app from which I receive BLE data (every 62ms via notifications). The app can save data via a BufferedWriter to a file. Upon ending the save of large amounts of data, I see an error such as this: GKI_exception out of buffers https://code.google.com/p/android/issues/detail?id=65455 (except my code is not scanning but receiving notifications). I don't see this error for 100s of kB saves, but I see it on 1-2MB saves in logcat, and on >5-6MB saves I need to power cycle the Nexus 7 (the app and BLE become totally unresponsive). I call close() on the BufferedWriter at the end of the save. How do I fix this?
The issue is likely thread related. You need to make sure that the "save" operation (which is writing file data to disk...an operation that takes a long time) is not happening in the same thread context that could block your BLE notifications or a related callback. If you block a thread that keeps a BLE callback method from returning, you will see the Bluetooth stack get starved, which is what that callback usually means.
Simplest thing is to always make sure you write file data in a new background thread (like an AsyncTask, Thread, or IntentService for instance), ensuring that regardless of what thread you were coming from, the long write to flash memory won't block the current context.
My program involves interaction with SQLite in a fairly regular basis, and in the beginning of the app, I call a query
mDatabase.rawQuery("SELECT key,
indice as _id
FROM Dictionary",null);
Strangely the app stalls on executing this line. This does not happen if I am debugging the application, but when I run the app, the control goes from this line and never returns. I have checked this by putting logcat before and after this line.
I have not been able to comprehend this behavior. Can someone help?
P.S. The table Dictionary has over 2000-3000 records.
EDIT:
I have tried calling this from both UI & separate threads. Either ways, the execution stops at this call (for that thread). So when I call it from another thread, though there is no ANR, the call still fails and holds the thread indefinitely.
EDIT2:
This issue does not happen every time I run the application but 5 out of 10 times. And apparently happens more on weaker phones.
Take care of below points.
Make call to query in separate thread other than UI thread.
Cursor at max can hold upto 1MB of data. So query for minimum amount of data.
You should take this off the UI thread. Looks like a heavy call. Anything which takes longer than 5 seconds and stalls the UI thread will trigger an ANR.
Yes piyushnp and abhinav are right. AsyncTask or Thread are better option for getting the details. Show progressbar when doing background processing. And when you get query results display it in activity or do whatver processing you want on query results.
This example simulates your problem. AsyncTask basic Example : AsyncTask
Suppose I have an array of shorts of length 1,000,000 and that I need this to be generated or loaded into RAM (ideally within a few seconds) when my app starts and before I get an activity thread timeout.
I'll then have quick access to its entries during runtime.
How would you go about loading this to memory from file? Loading from txt file? From an SQLite file (with two integer columns in a single table, one for index and the other for value)?
Is it possible for an activity to request a longer idle time before it's deemed to have timed out?
The solution to your problem would be to spawn a thread to do the file reading. This is going to be a high level overview so you can track down the relevant items. It will address item 2 first, then item 1.
First, you need to get your long running processing off the UI thread. The 'idle' time responsiveness issue (application not responding) is because you are tying up the UI thread which is needed to do other actions. The thread cannot be reading a file and drawing the screen at the same time (at least not well).
I've found the easiest way to do this is using the Java ExecutorServices. What you want to do is package the logic for reading the file or database into a java Runnable or Callable and then run that using an ExecutorService. The executor will take care of starting a thread and reclaiming those resource when they are no longer in use.
One key issue to be aware of is that, once you create another thread, you need to be careful when updating the UI. Because ui objects are not thread safe, you can only update the ui from the UI thread (common sense, right?). If you are in an activity, you can do this by calling runOnUiThread() or you can create your own Handler in one of the ACtivity methods called by the UI thread in the first place.
Sam Dufel said:
Hmm... You could really pack that down if you could come with a format to replace all the dummy entries with just a count. Eg, 10,000 zeros get replaced by a pair of flag bytes followed by 10,000
Additionally, you may see increased speed by spawning multiple read threads. If you could break the file up into two files which would allow two threads to execute simultaneously you could see a significant speed up (depending on what else you need to do to the data). A sort of divide an conquer for loading up your data from file. A cursory google search should give you information on how to load shorts from a file. You should most definitely consider using a binary format given your specifications.