Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: try to reduce sqlite cursor memory usage #375

Merged
merged 2 commits into from
Jul 7, 2023

Conversation

falconandy
Copy link
Contributor

@falconandy falconandy commented Jul 6, 2023

https://github.com/amplitude/Amplitude-Android/blob/main/CHANGELOG.md#2100-oct-12-2016

## 2.10.0 (Oct 12, 2016)
Catch and handle CursorWindowAllocationException thrown when the SDK is querying from > the SQLite DB when app memory is low.
If the exception is caught during initialize, then it is treated as if initialize was > never called.
If the exception is caught during the uploading of unsent events, then the upload is deferred to a later time.

https://github.com/amplitude/Amplitude-Android/blob/main/src/main/java/com/amplitude/api/AmplitudeClient.java#L444

} catch (CursorWindowAllocationException e) {  // treat as uninitialized SDK
      logger.e(TAG, String.format(
              "Failed to initialize Amplitude SDK due to: %s", e.getMessage()
      ));
      client.apiKey = null;
}

https://github.com/amplitude/Amplitude-Android/blob/main/src/main/java/com/amplitude/api/AmplitudeClient.java#L2187

} catch (CursorWindowAllocationException e) {
     // handle CursorWindowAllocationException when fetching events, defer upload
     uploadingCurrently.set(false);
      logger.e(TAG, String.format(
         "Caught Cursor window exception during event upload, deferring upload: %s",
          e.getMessage()
      ));
}

New logic was added recently (IdentifyInterceptor.transferInterceptedIdentify) that can throw CursorWindowAllocationException - the exception is not caught/handled in this case. I believe, we can’t defer transferInterceptedIdentify call (similar to If the exception is caught during the uploading of unsent events, then the upload is deferred to a later time).

To reduce cursor memory usage now each event is loaded from sqlite database with separate query/cursor. Hope it helps.

Currently my environment doesn't work with Amplitude-Android (build/test) - so I didn't test properly (CI tests only).

Some unused code is removed.

@falconandy falconandy marked this pull request as ready for review July 6, 2023 17:14
Copy link
Contributor

@justin-fiedler justin-fiedler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @falconandy. See questions.

@justin-fiedler justin-fiedler linked an issue Jul 6, 2023 that may be closed by this pull request
@justin-fiedler justin-fiedler merged commit 77e508d into main Jul 7, 2023
@justin-fiedler justin-fiedler deleted the AMP-79645-cursor-windiow-allocate-crash branch July 7, 2023 16:48
github-actions bot pushed a commit that referenced this pull request Jul 7, 2023
## [2.39.7](v2.39.6...v2.39.7) (2023-07-07)

### Bug Fixes

* try to reduce sqlite cursor memory usage ([#375](#375)) ([77e508d](77e508d))
@github-actions
Copy link

github-actions bot commented Jul 7, 2023

🎉 This PR is included in version 2.39.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash: Could not allocate CursorWindow
2 participants