Fix memory leak when processing locally stored events #334
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We have noticed high RAM usage when the host application is used offline or behind a firewall that blocks the Segment API/CDN domains. The SDK would attempt to upload the data unsuccessfully and retry. On each attempt it would iterate through the locally stored files and read them into memory.
In some cases this caused our application to crash due to OOM.
The Swift code looks correct, and in theory all
Data
instances should deallocate after processing a file. But since theData
is actually a bridge to theNSData
class, andNSData
is an Objecive-C class, some manual memory management is required.Proposed fix
Use autorelease pools to explicitly release allocated memory on each iteration to avoid unnecessary memory consumption.
This fixes the immediate problem we are experiencing. We haven't yet gone through other functions or investigated whether there are other cases where autorelease pools could be used.
Context