Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive Extensions memory usage

I have the following code in a WPF application using Reactive Extensions for .NET:

public MainWindow()
{
    InitializeComponent();

    var leftButtonDown = Observable.FromEvent<MouseButtonEventArgs>(this, "MouseLeftButtonDown");
    var leftButtonUp = Observable.FromEvent<MouseButtonEventArgs>(this, "MouseLeftButtonUp");

    var moveEvents = Observable.FromEvent<MouseEventArgs>(this, "MouseMove")
        .SkipUntil(leftButtonDown)
        .SkipUntil(leftButtonUp)
        .Repeat()
        .Select(t => t.EventArgs.GetPosition(this));

    moveEvents.Subscribe(point =>
    {
        textBox1.Text = string.Format(string.Format("X: {0}, Y: {1}", point.X, point.Y));
    });
}

Will there be a steady increase of memory while the mouse is moving on this dialog?

Reading the code, I would expect that the moveEvents observable will contain a huge amount of MouseEventArgs after a while? Or is this handled in some smart way I'm unaware of?

like image 733
OneOfAccount Avatar asked Jun 14 '10 20:06

OneOfAccount


Video Answer


1 Answers

No, there shouldn't be a steady increase in memory usage - why would there be? The events are basically streamed to the subscriber; they're not being buffered up anywhere.

The point of Rx is that events are pushed to the subscriber, who can choose what to do with them. It's not like adding events to a list which is then examined later.

(There are ways of buffering events in Rx, but you're not using them as far as I can tell.)

like image 152
Jon Skeet Avatar answered Nov 15 '22 00:11

Jon Skeet