title: android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=971 (# cursors opened by this proc=971)
body: <blockquote>
    <p><strong>android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=971 (# cursors opened by this proc=971)</strong></p>
  </blockquote>
  
  <p>Hi I have a controller for querying the database. Code for getting data asynchronously, because I have more than 1000 records.</p>
  
  <pre><code>public void query(final Uri contentUri,
                    final String where,
                    final String[] selectionArgs,
                    final String sortOrder,
                    final int from,
                    final int limit,
                    final AdvancedQueryListener&lt;Cursor&gt; listener) {
      if (listener == null)
          return;
      if (null == SamsungApp.getAppContext())
          return;
      final ContentResolver mContentResolver = SamsungApp.getAppContext().getContentResolver();
      final Handler mHandler = Utility.instantiateHandler(new Handler.Callback() {
          @Override
          public boolean handleMessage(Message message) {
              switch (message.what) {
                  case 1:
                      Cursor cursor = (Cursor) message.obj;
                      listener.onGetResult(cursor);
              }
              return false;
          }
      });
  
      Runnable queryRunner = new Runnable() {
          @Override
          public void run() {
              StringBuilder sqlStatement = null;
  
              if (!TextUtils.isEmpty(sortOrder)) {
                  if (sqlStatement == null)
                      sqlStatement = new StringBuilder();
                  sqlStatement.append(sortOrder);
              }
  
              if (limit &gt; 0) {
                  if (sqlStatement == null)
                      sqlStatement = new StringBuilder();
                  sqlStatement.append(" LIMIT ");
                  sqlStatement.append(limit);
              }
  
              if (from &gt; 0) {
                  if (sqlStatement == null)
                      sqlStatement = new StringBuilder();
                  sqlStatement.append(" OFFSET ");
                  sqlStatement.append(from);
              }
              Cursor cursor = mContentResolver.query(contentUri, null, where, selectionArgs, sqlStatement.toString());
              Message message = mHandler.obtainMessage(1);
              message.obj = cursor;
              mHandler.sendMessage(message);
          }
      };
  
      FixedThreadPoolExecutor.execute(queryRunner);
  }
  </code></pre>
  
  <blockquote>
    <p>And I created a Fixed thread pool to handle all threads in my application. In that, I create Threadpool executor with max 100 concurrent tasks;</p>
  </blockquote>
  
  <pre><code>    FixedThreadPoolExecutor executor = getInstance();
      executor.mThreadPoolExecutor.execute(runnable);
  </code></pre>
  
  <blockquote>
    <p>And My Content provider is given below </p>
  </blockquote>
  
  <pre><code>public boolean close() {
      if (this.isActive) {
          mAppDbHelper.removeConnection();
      }
      int count = mAppDbHelper.getCounter();
      if (count == 0) {
          synchronized (mutex) {
              if (mSqldb.inTransaction()) mSqldb.endTransaction();
              if (mSqldb.isOpen()) mSqldb.close();
              mSqldb = null;
          }
      }
      return (count == 0);
  }
  
  
  public SQLiteDatabase open() {
      if (!this.isActive) {
          mAppDbHelper.addConnection();
      }
      if (mSqldb == null || !mSqldb.isOpen()) {
          synchronized (mutex) {
              mSqldb = mAppDbHelper.getWritableDatabase();
          }
      }
      this.isActive = true;
      return mSqldb;
  }
  
  // 
  @Override
  public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
      final SQLiteDatabase db = open();
      Cursor cursor = null;
      if (db != null) {
          /* SQLiteQueryBuilder helps to construct queries that are more complex than
           * those supported by the SQLiteDatabase functions (for example, sub queries).
           */
          SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
          queryBuilder.setTables(uri.getLastPathSegment()); // Extract table name fromDeviceCursor uri
          cursor = queryBuilder.query(db,
                  projection,
                  selection,
                  selectionArgs,
                  null,
                  null,
                  sortOrder
          );
          if (cursor != null) {
              cursor.setNotificationUri(getContext().getContentResolver(), uri);
          }
      } else {
          throw new DataLayerException(DEBUG_TAG + "SQLiteDatabase is null");
      }
      return cursor;
  }
  </code></pre>
  
  <blockquote>
    <p>And my problem is that every time If querying concurrently I got the issue </p>
  </blockquote>
  
  <pre><code>FATAL EXCEPTION: main
                                   Process: com.samsung.lighting, PID: 4949
                                   android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)
                                       at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
                                       at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:843)
                                       at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
                                       at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
                                       at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:143)
                                       at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:132)
                                       at android.content.ContentResolver.query(ContentResolver.java:512)
                                       at android.content.ContentResolver.query(ContentResolver.java:435)
                                       at com.samsung.lighting.storage.repository.impl.WiSeGalleryRepositaryImpl.getNonSyncedIcon(WiSeGalleryRepositaryImpl.java:278)
                                       at com.samsung.lighting.sync.OfflineSyncManager.syncAddedIcons(OfflineSyncManager.java:194)
                                       at com.samsung.lighting.sync.OfflineSyncManager.startSyncing(OfflineSyncManager.java:164)
                                       at com.samsung.lighting.sync.OfflineSyncManager.onFailure(OfflineSyncManager.java:737)
                                       at com.wise.cloud.group.WiSeCloudGroupManager$9.onFailure(WiSeCloudGroupManager.java:1603)
                                       at com.wise.cloud.queue.WiSeQueueManagement.addToQueue(WiSeQueueManagement.java:173)
                                       at com.wise.cloud.queue.WiSeQueueManagement.initialCheckInToQueueManagement(WiSeQueueManagement.java:117)
                                       at com.wise.cloud.group.WiSeCloudGroupManager.setLogicalGroupAssociations(WiSeCloudGroupManager.java:1662)
                                       at com.samsung.lighting.cloud_interactor.CloudGroupManager.setGroupAssociations(CloudGroupManager.java:131)
                                       at com.samsung.lighting.sync.OfflineSyncManager$7.onGetResult(OfflineSyncManager.java:391)
                                       at com.samsung.lighting.sync.OfflineSyncManager$7.onGetResult(OfflineSyncManager.java:387)
                                       at com.samsung.lighting.storage.repository.impl.WiSeGroupAssociationRepositoryImpl$1.onGetResult(WiSeGroupAssociationRepositoryImpl.java:344)
                                       at com.samsung.lighting.storage.repository.impl.WiSeGroupAssociationRepositoryImpl$1.onGetResult(WiSeGroupAssociationRepositoryImpl.java:330)
                                       at com.samsung.lighting.storage.repository.SamsungContentResolver$1.handleMessage(SamsungContentResolver.java:73)
                                       at android.os.Handler.dispatchMessage(Handler.java:98)
                                       at android.os.Looper.loop(Looper.java:148)
                                       at android.app.ActivityThread.main(ActivityThread.java:5451)
                                       at java.lang.reflect.Method.invoke(Native Method)
                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>
  
  <p><strong>Note</strong> 
  Everybody telling me to close the cursor <code>Cursor.close()</code> like </p>
  
  <p>Everywhere in my application, I put in </p>
  
  <pre><code>try{
      //code 
  }finally{
      if(null!=cursor)
          cursor.close
  }
  </code></pre>
  
  <p>Help me to find out the problem. </p>
  
  <p>Thanks in advance</p>