Android Open cursors when running applyBatch() method
<p>I have an activity that inserts a big number of contacts on an Android device by executing applyBatch() commands on the ContentResolver. I run this application on many Android devices and it seems to work ok except Galaxy S2 I9100 phone (with Android 4.0 version) where after a number of inserts, I get the following exception for each command that is executed thereafter:</p>

<pre><code>E/CursorWindow( 5727): Could not allocate CursorWindow '/data/data/com.android.providers.contacts/databases/contacts2.db' of size 2097152 due to error -12.
E/JavaBinder( 5727): *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
E/JavaBinder( 5727): android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=819 (# cursors opened by pid 5701=819)
E/JavaBinder( 5727):    at android.database.CursorWindow.&lt;init&gt;(CursorWindow.java:104)
E/JavaBinder( 5727):    at android.database.AbstractWindowedCursor.clearOrCreateWindow
(AbstractWindowedCursor.java:198)
E/JavaBinder( 5727):    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:162)
E/JavaBinder( 5727):    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156)
E/JavaBinder( 5727):    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:161)
E/JavaBinder( 5727):    at android.database.AbstractCursor.moveToNext(AbstractCursor.java:209)
E/JavaBinder( 5727):    at com.android.providers.contacts.ContactsProvider2.findGroupByRawContactId(ContactsProvider2.java:3738)
E/JavaBinder( 5727):    at com.android.providers.contacts.ContactsProvider2.addAutoAddMembership(ContactsProvider2.java:3725)
E/JavaBinder( 5727):    at com.android.providers.contacts.ContactsProvider2.insertRawContact(ContactsProvider2.java:3713)
E/JavaBinder( 5727):    at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:3360)
E/JavaBinder( 5727):    at com.android.providers.contacts.AbstractContactsProvider.insert(AbstractContactsProvider.java:108)
E/JavaBinder( 5727):    at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2605)
E/JavaBinder( 5727):    at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
E/JavaBinder( 5727):    at com.android.providers.contacts.AbstractContactsProvider.applyBatch(AbstractContactsProvider.java:203)
E/JavaBinder( 5727):    at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2755)
E/JavaBinder( 5727):    at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:208)
E/JavaBinder( 5727):    at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:179)
E/JavaBinder( 5727):    at android.os.Binder.execTransact(Binder.java:338)
E/JavaBinder( 5727):    at dalvik.system.NativeStart.run(Native Method)
</code></pre>

<p>The PID mentioned in the exception is running the  <strong>android.process.acore</strong> package.</p>

<p>Indeed when I monitor the process while the initial inserts are being perfrormed, I see that this PID is generating logs that mention that "cursors are finalized prior to closing"</p>

<p>Does anyone have a clue about this strange behaviour?</p>

<p>Below is the code that performs the inserts:</p>

<pre><code>ArrayList&lt;ContentProviderOperation&gt; ops = new ArrayList&lt;ContentProviderOperation&gt;();
        ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                .withValue(RawContacts.ACCOUNT_TYPE, "")
                .withValue(RawContacts.ACCOUNT_NAME, "").build());

        ops.addAll(vCardUtility.readVCard(vCard));

        Log.w(TAG, "TRYING TO EXECUTE " + ops.size() + " OPERATIONS");
        r = cr.applyBatch(ContactsContract.AUTHORITY, ops);
</code></pre>

<p>Where readVcard just creates a list of ContentProviderOperation objects to insert the contact details.</p>
