Skip to content

Commit

Permalink
VEX-6030: Further prevent memory crashes (24i#15)
Browse files Browse the repository at this point in the history
This PR is intended to prevent memory crashes when the free memory runs out

Jira: VEX-6030
https://jira.tenkasu.net/browse/VEX-6030

Velocity PR: crunchyroll/velocity#2043

By forcefully calling the garbage collector the memory crashes, specially on lower resolutions (<720p) are gone on devices with 2GB of ram or less and Android 6, also testing 3GB Android 7 with this patch, so far is working as intended

Reviews
Major reviewer (domain expert): @nickfujita
Minor reviewer: @jctorresM
  • Loading branch information
gabriel-rivero authored Nov 17, 2021
2 parents 292b539 + ab202c9 commit aa63361
Showing 1 changed file with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ private void reLayout(View view) {

private class RNVLoadControl extends DefaultLoadControl {
private int availableHeapInBytes = 0;
private Runtime runtime;
public RNVLoadControl(DefaultAllocator allocator, int minBufferMs, int maxBufferMs, int bufferForPlaybackMs, int bufferForPlaybackAfterRebufferMs, int targetBufferBytes, boolean prioritizeTimeOverSizeThresholds, int backBufferDurationMs, boolean retainBackBufferFromKeyframe) {
super(allocator,
minBufferMs,
Expand All @@ -433,6 +434,7 @@ public RNVLoadControl(DefaultAllocator allocator, int minBufferMs, int maxBuffer
prioritizeTimeOverSizeThresholds,
backBufferDurationMs,
retainBackBufferFromKeyframe);
runtime = Runtime.getRuntime();
ActivityManager activityManager = (ActivityManager) themedReactContext.getSystemService(themedReactContext.ACTIVITY_SERVICE);
availableHeapInBytes = (int) Math.floor(activityManager.getMemoryClass() * maxHeapAllocationPercent * 1024 * 1024);
}
Expand All @@ -447,6 +449,11 @@ public boolean shouldContinueLoading(long playbackPositionUs, long bufferedDurat
if (isHeapReached) {
return false;
}
if (runtime.freeMemory() == 0) {
Log.w("ExoPlayer Warning", "Free memory reached 0, forcing garbage collection");
runtime.gc();
return false;
}
return super.shouldContinueLoading(playbackPositionUs, bufferedDurationUs, playbackSpeed);
}
}
Expand Down

0 comments on commit aa63361

Please sign in to comment.