Skip to content

Commit 2ab0299

Browse files
VEX-6365: Improve memory management (#17)
Improve memory management to reduce pressure on low end devices. JIRA: VEX-6365
1 parent aa63361 commit 2ab0299

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ maxBufferMs | number | The default maximum duration of media that the player wil
386386
bufferForPlaybackMs | number | The default duration of media that must be buffered for playback to start or resume following a user action such as a seek, in milliseconds.
387387
bufferForPlaybackAfterRebufferMs | number | The default duration of media that must be buffered for playback to resume after a rebuffer, in milliseconds. A rebuffer is defined to be caused by buffer depletion rather than a user action.
388388
maxHeapAllocationPercent | number | The percentage of available heap that the video can use to buffer, between 0 and 1
389+
minBackBufferMemoryReservePercent | number | The percentage of available app memory at which during startup the back buffer will be disabled, between 0 and 1
390+
minBufferMemoryReservePercent | number | The percentage of available app memory to keep in reserve that prevents buffer from using it, between 0 and 1
389391

390392
This prop should only be set when you are setting the source, changing it after the media is loaded will cause it to be reloaded.
391393

android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ class ReactExoplayerView extends FrameLayout implements
113113
DrmSessionEventListener {
114114

115115
public static final double DEFAULT_MAX_HEAP_ALLOCATION_PERCENT = 1;
116+
public static final double DEFAULT_MIN_BACK_BUFFER_MEMORY_RESERVE = 0;
117+
public static final double DEFAULT_MIN_BUFFER_MEMORY_RESERVE = 0;
116118

117119
private static final String TAG = "ReactExoplayerView";
118120

@@ -161,7 +163,8 @@ class ReactExoplayerView extends FrameLayout implements
161163
private int bufferForPlaybackMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS;
162164
private int bufferForPlaybackAfterRebufferMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS;
163165
private double maxHeapAllocationPercent = ReactExoplayerView.DEFAULT_MAX_HEAP_ALLOCATION_PERCENT;
164-
166+
private double minBackBufferMemoryReservePercent = ReactExoplayerView.DEFAULT_MIN_BACK_BUFFER_MEMORY_RESERVE;
167+
private double minBufferMemoryReservePercent = ReactExoplayerView.DEFAULT_MIN_BUFFER_MEMORY_RESERVE;
165168
private Handler mainHandler;
166169
private Timer bufferCheckTimer;
167170

@@ -449,6 +452,14 @@ public boolean shouldContinueLoading(long playbackPositionUs, long bufferedDurat
449452
if (isHeapReached) {
450453
return false;
451454
}
455+
long usedMemory = runtime.totalMemory() - runtime.freeMemory();
456+
long freeMemory = runtime.maxMemory() - usedMemory;
457+
long reserveMemory = (long)minBufferMemoryReservePercent * runtime.maxMemory();
458+
long bufferedMs = bufferedDurationUs / (long)1000;
459+
if (reserveMemory > freeMemory && bufferedMs > 2000) {
460+
// We don't have enough memory in reserve so we stop buffering to allow other components to use it instead
461+
return false;
462+
}
452463
if (runtime.freeMemory() == 0) {
453464
Log.w("ExoPlayer Warning", "Free memory reached 0, forcing garbage collection");
454465
runtime.gc();
@@ -1622,6 +1633,16 @@ public void setDisableFocus(boolean disableFocus) {
16221633
}
16231634

16241635
public void setBackBufferDurationMs(int backBufferDurationMs) {
1636+
Runtime runtime = Runtime.getRuntime();
1637+
long usedMemory = runtime.totalMemory() - runtime.freeMemory();
1638+
long freeMemory = runtime.maxMemory() - usedMemory;
1639+
long reserveMemory = (long)minBackBufferMemoryReservePercent * runtime.maxMemory();
1640+
if (reserveMemory > freeMemory) {
1641+
// We don't have enough memory in reserve so we will
1642+
Log.w("ExoPlayer Warning", "Not enough reserve memory, setting back buffer to 0ms to reduce memory pressure!");
1643+
this.backBufferDurationMs = 0;
1644+
return;
1645+
}
16251646
this.backBufferDurationMs = backBufferDurationMs;
16261647
}
16271648

@@ -1679,12 +1700,14 @@ public void setHideShutterView(boolean hideShutterView) {
16791700
exoPlayerView.setHideShutterView(hideShutterView);
16801701
}
16811702

1682-
public void setBufferConfig(int newMinBufferMs, int newMaxBufferMs, int newBufferForPlaybackMs, int newBufferForPlaybackAfterRebufferMs, double newMaxHeapAllocationPercent) {
1703+
public void setBufferConfig(int newMinBufferMs, int newMaxBufferMs, int newBufferForPlaybackMs, int newBufferForPlaybackAfterRebufferMs, double newMaxHeapAllocationPercent, double newMinBackBufferMemoryReservePercent, double newMinBufferMemoryReservePercent) {
16831704
minBufferMs = newMinBufferMs;
16841705
maxBufferMs = newMaxBufferMs;
16851706
bufferForPlaybackMs = newBufferForPlaybackMs;
16861707
bufferForPlaybackAfterRebufferMs = newBufferForPlaybackAfterRebufferMs;
16871708
maxHeapAllocationPercent = newMaxHeapAllocationPercent;
1709+
minBackBufferMemoryReservePercent = newMinBackBufferMemoryReservePercent;
1710+
minBufferMemoryReservePercent = newMinBufferMemoryReservePercent;
16881711
releasePlayer();
16891712
initializePlayer();
16901713
}

android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public class ReactExoplayerViewManager extends ViewGroupManager<ReactExoplayerVi
5656
private static final String PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_MS = "bufferForPlaybackMs";
5757
private static final String PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS = "bufferForPlaybackAfterRebufferMs";
5858
private static final String PROP_BUFFER_CONFIG_MAX_HEAP_ALLOCATION_PERCENT = "maxHeapAllocationPercent";
59+
private static final String PROP_BUFFER_CONFIG_MIN_BACK_BUFFER_MEMORY_RESERVE_PERCENT = "minBackBufferMemoryReservePercent";
60+
private static final String PROP_BUFFER_CONFIG_MIN_BUFFER_MEMORY_RESERVE_PERCENT = "minBufferMemoryReservePercent";
5961
private static final String PROP_PREVENTS_DISPLAY_SLEEP_DURING_VIDEO_PLAYBACK = "preventsDisplaySleepDuringVideoPlayback";
6062
private static final String PROP_PROGRESS_UPDATE_INTERVAL = "progressUpdateInterval";
6163
private static final String PROP_REPORT_BANDWIDTH = "reportBandwidth";
@@ -346,6 +348,9 @@ public void setBufferConfig(final ReactExoplayerView videoView, @Nullable Readab
346348
int bufferForPlaybackMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS;
347349
int bufferForPlaybackAfterRebufferMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS;
348350
double maxHeapAllocationPercent = ReactExoplayerView.DEFAULT_MAX_HEAP_ALLOCATION_PERCENT;
351+
double minBackBufferMemoryReservePercent = ReactExoplayerView.DEFAULT_MIN_BACK_BUFFER_MEMORY_RESERVE;
352+
double minBufferMemoryReservePercent = ReactExoplayerView.DEFAULT_MIN_BUFFER_MEMORY_RESERVE;
353+
349354
if (bufferConfig != null) {
350355
minBufferMs = bufferConfig.hasKey(PROP_BUFFER_CONFIG_MIN_BUFFER_MS)
351356
? bufferConfig.getInt(PROP_BUFFER_CONFIG_MIN_BUFFER_MS) : minBufferMs;
@@ -357,7 +362,11 @@ public void setBufferConfig(final ReactExoplayerView videoView, @Nullable Readab
357362
? bufferConfig.getInt(PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS) : bufferForPlaybackAfterRebufferMs;
358363
maxHeapAllocationPercent = bufferConfig.hasKey(PROP_BUFFER_CONFIG_MAX_HEAP_ALLOCATION_PERCENT)
359364
? bufferConfig.getDouble(PROP_BUFFER_CONFIG_MAX_HEAP_ALLOCATION_PERCENT) : maxHeapAllocationPercent;
360-
videoView.setBufferConfig(minBufferMs, maxBufferMs, bufferForPlaybackMs, bufferForPlaybackAfterRebufferMs, maxHeapAllocationPercent);
365+
minBackBufferMemoryReservePercent = bufferConfig.hasKey(PROP_BUFFER_CONFIG_MIN_BACK_BUFFER_MEMORY_RESERVE_PERCENT)
366+
? bufferConfig.getDouble(PROP_BUFFER_CONFIG_MIN_BACK_BUFFER_MEMORY_RESERVE_PERCENT) : minBackBufferMemoryReservePercent;
367+
minBufferMemoryReservePercent = bufferConfig.hasKey(PROP_BUFFER_CONFIG_MIN_BUFFER_MEMORY_RESERVE_PERCENT)
368+
? bufferConfig.getDouble(PROP_BUFFER_CONFIG_MIN_BUFFER_MEMORY_RESERVE_PERCENT) : minBufferMemoryReservePercent;
369+
videoView.setBufferConfig(minBufferMs, maxBufferMs, bufferForPlaybackMs, bufferForPlaybackAfterRebufferMs, maxHeapAllocationPercent, minBackBufferMemoryReservePercent, minBufferMemoryReservePercent);
361370
}
362371
}
363372

0 commit comments

Comments
 (0)