开发者

Android application CursorWindow memory error

I'm currently receiving an "IllegalStateException couldn't init cursor window" after running my application for a few minutes. I start three different services when my application starts, each poll (every 1 sec) a different RESTful web service. After retrieving/parsing the result each service has a ContentProvider they insert the new data into. The ContentProvider then uses setNotificationUri to let the current Activity know there is new data available (if they are subscribed to get updates from that URI).

First question, is this the correct approach to polling a RESTful web service and getting the result to the interested Activities? After reading the answer to this question, it seems like the ContentProvider is unnecessary because everything lives within the same application.

Second question, what could be causing the IllegalStateException? It turns out there is not enough heap allocate the CursorWindow (cursorwindow heap allocation failed). I thought perhaps the problem was when I query the ContentProvider (after getting the notification) it was returning too many results in the cursor. The Activities really only need the last update received so I added an ORDER BY "ID" DESC and limited the result to 1. Therefore the ContentProvider should only be returning 1 result each time. That didn't really make any difference.

Any help with the first question may also fix the second question. I read a few places not to do web service calls within an Activity which led me to using a Service class to poll the web services. Just FYI, this is an internal application that will have only a few clients.

Thanks for any feedback.

Edit: Here is the necessary code for one of the queries:

Cursor geoEllipseDat开发者_如何学Pythonas = managedQuery(GeoEllipseDataProvider.GEO_ELLIPSE_CONTENT_URI,
                projection, null, null, GeoEllipseDataProvider.ID + " DESC");
        boolean dataAvailable = geoEllipseDatas.moveToLast();

if (dataAvailable) {
    // parse the data out of the cursor
    String targetId = geoEllipseDatas.getString(1);
    ...
}


  1. After having gone both approaches (ContentProvider and direct DB access), I personally found that ContentProvider are a lesser source of bugs. They are really easy to access/setup simply be using the content resolver, they force you to actually think about URIs and you'll get all the benefits going with them (like Intent filters on those URIs, ...).

  2. Make sure you are correctly closing the cursors, freeing the memory, ... Hard to say without any code.


From what I understood of the managedQuery use case, you should call it in the onCreate method of your activity. If you call it somewhere else, I think it is safe to call stopManagingCursor before doing another managedQuery (I don't know if that function does it for you automatically, would need to have a look at the sources).

Also, the problem could be in your content provider, that would fire an update on that cursor, that would trigger another query, that would fire an update, that would ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜