Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Dorus opened this issue Jun 2, 2015 · 5 comments

Comments

@Dorus
Copy link

Dorus commented Jun 2, 2015

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.

@baltie
Copy link

baltie commented Jun 30, 2015

I can confirm that this is a problem. I just tracked down a memory leak in our application to Observable.Generate(), seems to be the exact same issue as reported here.

@Dorus Dorus changed the title StackOverflowException in Observable.Generate Memory leak in Observable.Generate results in StackOverflowException or OutOfMemoryException Jul 1, 2015
@raffaeler
Copy link

+1 I had the same problem with Generate and timer below 1ms.
It generates a chain of disposable[] which points to the previous one creating a concatenation so long that both VS2015 debugger and windbg + sos crash if the dump is moderately long.

@danielcweber
Copy link
Collaborator

@Dorus @baltie @raffaeler Can you confirm this is still a problem with the current preview of Rx 4.0? I can't reproduce it any more.

@akarnokd
Copy link
Collaborator

On Windows with preview3, net45, console app, the code above works for me with all the standard scheduler types offered by Scheduler.

@akarnokd
Copy link
Collaborator

Should be fixed in #1067.
Related: #1065

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants