Recently I noticed a small bug in my code which uses Reactive Extensions. I was subscribing to Timer but I never disposed my subscription. This resulted in a memory leak.
I created snippet which highlights this danger:
while (true)
{
Observable.Timer(TimeSpan.Zero, TimeSpan.FromMinutes(1)).Subscribe(Console.WriteLine);
}
Is this normal behaviour?
Shouldn’t scheduler hold weak reference to timer to get it garbage collected when subscribers lost connection with the rest of the app?
This is normal, and is a feature.
The semantics for Subscribe() are listen forever, or until Disposed() or OnCompleted(), or OnError(), which ever comes first.