fix: implement memory-based throttle to prevent OOM during downloads#2786
Merged
fire-light43 merged 2 commits intoMay 12, 2026
Merged
Conversation
fire-light43
approved these changes
May 12, 2026
Contributor
fire-light43
left a comment
There was a problem hiding this comment.
Very well made! Thank you for the contribution.
I reduced the limit to 50MB to be on the safer side, it's a soft limit after all.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Viewed AndroidManifest.xml:9-18
Here is a detailed description you can use for the fix: implement memory-based throttle to prevent OOM during downloads Pull Request:
Description: Implement Memory-Based Throttle to Prevent OOM During Downloads
This PR addresses reported crashes (rebooting into safe mode) that occur during large downloads, particularly on newer Android versions like Android 16.
The Problem
CloudStream uses a parallel downloading strategy to maximize speed. However, previously there was no limit on how far ahead the network jobs could download compared to what was successfully written to the disk.
If the disk write speed was slower than the network speed (common with slow SD cards or high-speed fiber), or if a specific segment was delayed while subsequent segments were downloaded successfully, the app would buffer the out-of-order data in RAM. For large files, this buffer could grow to several hundred megabytes, triggering an OutOfMemory (OOM) error and crashing the app.
The Solution
Instead of an unbounded lookahead, this PR implements a memory-based throttle that monitors the gap between downloaded data and written data.
Key Changes:
metadata.bytesDownloaded - metadata.bytesWritten. This represents the exact amount of data currently held in the RAM buffer (pendingData).fileMutex.withLockto ensure data consistency.delayis executed outside the lock to prevent blocking other critical operations or causing deadlocks.DownloadManager.kt.Results
AI Usage Disclosure
This fix was developed with AI assistance to identify the root cause of the memory leak in the parallel download queue. Following review from
@fire-light43, the implementation was refined to use the existingbytesDownloadedandbytesWrittenmetadata for a more lightweight and robust solution.