diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index ea27b947ee..eb4dfa8de3 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -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, @@ -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); } @@ -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); } }