Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我是 Rx 的新手,我似乎可以观察到缓冲区事件数据。使用 cold observable或replay使数据流即使在很长一段时间后也会发送给后来的观察者。但是什么时候会被垃圾呢?此外,使用 Rx 时应该注意什么内存?
cold observable
replay
垃圾收集遵循 Java 的常规规则:如果有硬引用,它将永远存在。这意味着如果您将 Observable 分配给某个字段,则该 observable 将不符合垃圾回收条件。如果这是 replay() 运算符的结果 - 或者如果 replay() 运算符位于它的上游 - 那么 replay() 运算符中的所有数据也会保存在内存中。
有一些replay变体可以帮助您通过超时或计数来限制保存在内存中的引用对象。
你也可以SoftReference对 observable 本身使用 a 。
SoftReference