IObservable.Subscribe()中的IDisposable是否包含对源的引用?

时间:2021-11-26 16:59:07

Does an IDisposable from IObservable.Subscribe() hold a reference to the IObservable?

IObservable.Subscribe()中的IDisposable是否包含对IObservable的引用?

If the IDisposable is rooted and can't be garbage collected, does it hold a reference to the subscription and the IObservable?

如果IDisposable是root并且无法进行垃圾回收,它是否包含对订阅和IObservable的引用?

In other words, if the intended lifetime of the IObservable is shorter than the lifetime of the returned IDisposable, can the IObservable be GC'ed?

换句话说,如果IObservable的预期生命周期短于返回的IDisposable的生命周期,IObservable可以进行GC吗?

3 个解决方案

#1


1  

It depends on the implementation. It doesn't have to, but it very well could.

这取决于实施。它没有,但它很可能。

#2


0  

I created some quick test code for this, and it seems that the IDisposable will keep a reference and hold the IObservable in memory. Or at least very well could, as Jason says.

我为此创建了一些快速测试代码,似乎IDisposable将保留一个引用并将IObservable保存在内存中。或者至少非常好,就像贾森所说的那样。

Subject<int> subject = new Subject<int>();
WeakReference wr = new WeakReference(subject);

IDisposable disposable = subject.Subscribe(i => Console.WriteLine("Next: " + i));

subject.OnNext(5);
subject.OnNext(10);
subject = null;

GC.Collect();
Console.WriteLine(wr.IsAlive ? "Subject is still alive." : "Subject is not alive.");

When written as above, the weak reference will still be alive. If I comment out the disposable assignment, then subject gets GC'ed.

如上所述,弱引用仍然存在。如果我注释掉一次性作业,那么主题就会得到GC。

/*IDisposable disposable = */ subject.Subscribe(i => Console.WriteLine("Next: " + i));

#3


0  

Here's a test that seems to show that the observable is GC'ed:

这是一个似乎表明观察者是GC的测试:

var xs = Observable.Timer(TimeSpan.FromSeconds(1.0));
var disposable = xs.Subscribe(x => Console.WriteLine(x));

var wr = new WeakReference(xs);
xs = null;

Thread.Sleep(2000);

Console.WriteLine(wr.IsAlive); // True

GC.Collect();

Console.WriteLine(wr.IsAlive); // False

#1


1  

It depends on the implementation. It doesn't have to, but it very well could.

这取决于实施。它没有,但它很可能。

#2


0  

I created some quick test code for this, and it seems that the IDisposable will keep a reference and hold the IObservable in memory. Or at least very well could, as Jason says.

我为此创建了一些快速测试代码,似乎IDisposable将保留一个引用并将IObservable保存在内存中。或者至少非常好,就像贾森所说的那样。

Subject<int> subject = new Subject<int>();
WeakReference wr = new WeakReference(subject);

IDisposable disposable = subject.Subscribe(i => Console.WriteLine("Next: " + i));

subject.OnNext(5);
subject.OnNext(10);
subject = null;

GC.Collect();
Console.WriteLine(wr.IsAlive ? "Subject is still alive." : "Subject is not alive.");

When written as above, the weak reference will still be alive. If I comment out the disposable assignment, then subject gets GC'ed.

如上所述,弱引用仍然存在。如果我注释掉一次性作业,那么主题就会得到GC。

/*IDisposable disposable = */ subject.Subscribe(i => Console.WriteLine("Next: " + i));

#3


0  

Here's a test that seems to show that the observable is GC'ed:

这是一个似乎表明观察者是GC的测试:

var xs = Observable.Timer(TimeSpan.FromSeconds(1.0));
var disposable = xs.Subscribe(x => Console.WriteLine(x));

var wr = new WeakReference(xs);
xs = null;

Thread.Sleep(2000);

Console.WriteLine(wr.IsAlive); // True

GC.Collect();

Console.WriteLine(wr.IsAlive); // False