Skip to content

Memory leak in Observable.Generate results in StackOverflowException or OutOfMemoryException #103

@Dorus

Description

@Dorus

Observable.Generate leaks a SerialDisposable every iteration. For example:

        var scheduler = Scheduler.Default;
        Observable.Generate<int, int>(
            0
            , i => i < 12000
            , i => i + 1
            , i => i
            , i => scheduler.Now,
            scheduler).Subscribe();

Gives a StackOverflowException instantly. Notice I use scheduler.Now to speed up the problem, using any timespan below 1 ms works, as at that span, Observable.Generate goes to the next iteration instantly. If you use a timespan at or above 1ms, it switches to 10-15ms delays (the precision of Windows timers).

Another example:

        var scheduler = TaskPoolScheduler.Default;
        var theDelay = TimeSpan.FromTicks(1);
        Observable.Generate<Tuple<int, DateTimeOffset>, int>(
            Tuple.Create(0, scheduler.Now),
            _ => _.Item1 < 20000000,
            tup => Tuple.Create(tup.Item1 + 1, tup.Item2.Add(theDelay)),
            tup => tup.Item1,
            tup => tup.Item2,
            scheduler)
            .Subscribe();

Gives a out of memory exception after 8 seconds. If you increase the delay to 1-15ms, i estimate it will run out of memory at after 80-800 hours, however did not have time yet to leave it running for a month, the memory profiler still shows leaked objects.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions