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 memory leak when processing locally stored events #334

Merged
merged 2 commits into from Apr 23, 2024

Conversation

eofs
Copy link
Contributor

@eofs eofs commented Apr 23, 2024

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 the Data is actually a bridge to the NSData class, and NSData 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

Copy link
Contributor

@bsneed bsneed left a comment

Choose a reason for hiding this comment

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

Thanks @eofs !

@bsneed bsneed merged commit 4a744ef into segmentio:main Apr 23, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants