amplitude / Amplitude-Android

Native Android SDK for Amplitude
MIT License
164 stars 90 forks source link

fix: try to reduce sqlite cursor memory usage #375

Closed falconandy closed 1 year ago

falconandy commented 1 year ago

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.

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 2.39.7 :tada:

The release is available on:

Your semantic-release bot :package::rocket: