简体   繁体   English

使用重播(selectorFoo)但不发布(selectorFoo)时的OOM

[英]OOM when using replay(selectorFoo) but not publish(selectorFoo)

This crashes with OOM: 这会因OOM而崩溃:

Flowable.range(1, 5000)
        .map(__ -> new byte[1024 * 1024])
        .replay(
          fb ->
            fb.take(1)
              .concatMap(__ -> fb)
          ,1
        )
        .count()
        .toFlowable()
        .blockingSubscribe(c -> System.out.println("args = [" + c + "]"));

This is, I think because replay is holding to the emissions from upstream, even though I would have thought that the 1 buffer sizse hint would make it not to.... what am I missing? 我想这是因为replay一直在保持上游的排放,尽管我本来以为1缓冲区sizse提示会使它不...。我错过了什么?

This doesn't crash: 这不会崩溃:

Flowable.range(1, 5000)
        .map(__ -> new byte[1024 * 1024])
        .publish(
          fb ->
            fb.take(1)
              .concatMap(first -> fb.startWith(first))
          ,1
        )
        .count()
        .toFlowable()
        .blockingSubscribe(c -> System.out.println("args = [" + c + "]"));

But I am not sure if I am guaranteed that I will get ALL the emissions from upstream like that... 但是我不确定我是否能保证会像这样从上游获得所有排放物...

I've investigated this and found the cause of the issue: a bug in replay in RxJava 2. 我对此进行了调查,发现了问题的原因:RxJava 2中的replay错误。

What happens is that replay holds references to 2 subscribers, one for take and the other for the concatMap 's inner consumer in a local variable, thus there is a GC root from the main thread to the defunct take still referencing the very first item. 什么情况是, replay持有引用2个用户,一个是take ,另一个为concatMap在一个局部变量的内部消费,从而有来自主线程已不存在的一个GC根take还是引用的第一个项目。 Since the bounded replay uses a linked list, this very first item then keeps referencing the newer and newer items via its "next" links and ends up exhausting the memory. 由于有界重放使用链表,因此该第一项将继续通过其“下一个”链接引用更新的项目,并最终耗尽内存。

publish doesn't keep references to old values thus this is not an issue there. publish不会保留对旧值的引用,因此这不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM