diff --git a/src/main/java/io/reactivex/subjects/ReplaySubject.java b/src/main/java/io/reactivex/subjects/ReplaySubject.java index 29634ff1c4..271a1f7b56 100644 --- a/src/main/java/io/reactivex/subjects/ReplaySubject.java +++ b/src/main/java/io/reactivex/subjects/ReplaySubject.java @@ -13,7 +13,6 @@ package io.reactivex.subjects; -import io.reactivex.annotations.Nullable; import java.lang.reflect.Array; import java.util.*; import java.util.concurrent.TimeUnit; @@ -21,7 +20,7 @@ import io.reactivex.Observer; import io.reactivex.Scheduler; -import io.reactivex.annotations.CheckReturnValue; +import io.reactivex.annotations.*; import io.reactivex.disposables.Disposable; import io.reactivex.internal.functions.ObjectHelper; import io.reactivex.internal.util.NotificationLite; @@ -94,8 +93,9 @@ * in a non-blocking and thread-safe manner via {@link #hasValue()}, {@link #getValue()}, * {@link #getValues()} or {@link #getValues(Object[])}. *
- * Note that due to concurrency requirements, a size-bounded {@code ReplaySubject} may hold strong references to more - * source emissions than specified. + * Note that due to concurrency requirements, a size- and time-bounded {@code ReplaySubject} may hold strong references to more + * source emissions than specified while it isn't terminated yet. Use the {@link #cleanupBuffer()} to allow + * such inaccessible items to be cleaned up by GC once no consumer references it anymore. *
+ * By default, live bounded buffers will remember one item before + * the currently receivable one to ensure subscribers can always + * receive a continuous sequence of items. A terminated ReplaySubject + * automatically releases this inaccessible item. + *
+ * The method must be called sequentially, similar to the standard
+ * {@code onXXX} methods.
+ * @since 2.1.11 - experimental
+ */
+ @Experimental
+ public void cleanupBuffer() {
+ buffer.trimHead();
+ }
+
/** An empty array to avoid allocation in getValues(). */
private static final Object[] EMPTY_ARRAY = new Object[0];
@@ -563,6 +581,12 @@ interface ReplayBuffer