Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7527301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:04:34+00:00 2026-05-30T04:04:34+00:00

I am trying to make sure instances of a class using Rx don’t leak.

  • 0

I am trying to make sure instances of a class using Rx don’t leak. I have reduced the problem down to the bare essentials and it looks like the Window function is key to the problem.

Consider the following:

<!-- language: c# -->

using System;
using System.Reactive.Linq;

class Program
{
    static void Main(string[] args)
    {
        var foo = new Foo();
        Console.WriteLine("Press any key...");
        Console.ReadKey(true);

        foo = null;
        GC.Collect();

        Console.WriteLine("Press any key...");
        Console.ReadKey(true);
    }
}

public class Foo
{
    private event EventHandler MyEvent;

    public Foo()
    {
        var subs = Observable.FromEventPattern(
            e => MyEvent += e, e => MyEvent -= e);

        // (1) foo is never GC'd
        subs.Window(TimeSpan.FromSeconds(1)).Subscribe();

        // (2) foo is GC'd
        //subs.Window(TimeSpan.FromSeconds(1));

        // (3) foo is GC'd
        // subs.Window(1);
    }

    ~Foo()
    {
        Console.WriteLine("Bye!");     
    }
}

When I apply the Window function with a TimeSpan opening selector and subscribe to it, (1) foo is never GC’d.

If I don’t subscribe (2), or I use a different opening selector (3), then it is.

Also, if I use a cold observable as the source then foo is GC’d regardless.

Why is the Window function with a TimeSpan special, and how do I make sure foo will be GC’d when using it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:04:35+00:00Added an answer on May 30, 2026 at 4:04 am

    That looks correct to me.

    You are missing the allocation of the subscription to an IDisposable field. You dont dispose of the subscription, and you dont call GC.WaitForPendingFinalizers();

    You can fix the test up with the following:

    public class Foo : IDisposable
    {
        private event EventHandler MyEvent;
        private readonly IDisposable _subscription;
    
        public Foo()
        {
            var subs = Observable.FromEventPattern(
                e => MyEvent += e, 
                e => MyEvent -= e);
    
            // (1) foo is never GC'd
            //subs.Window(TimeSpan.FromSeconds(1)).Subscribe();
            _subscription = subs.Window(TimeSpan.FromSeconds(1)).Subscribe();
    
            // (2) foo is GC'd
            //subs.Window(TimeSpan.FromSeconds(1));
    
            // (3) foo is GC'd
            // subs.Window(1);
        }
    
        public void Dispose()
        {
            _subscription.Dispose(); 
        }
    
        //TODO: Implement Dispose pattern properly
        ~Foo()
        {
            _subscription.Dispose();
            Console.WriteLine("Bye!");     
        }
    }
    

    The test can now become

    //foo = null;   //This will just change our reference, the object sill lives and has stuff happening with timers etc..
    foo.Dispose();  //Dispose instead of killing reference
    
    
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();
    

    I hope that helps. Also check out the Lifetime Management post on my intro to Rx blog series.

    UPDATE: My online book at IntroToRx.com replaces the blog series. Most relevant here seems to be the Lifetime management chapter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make sure instances of a certain class get released, by using
I finished my small application and I am trying to make sure I have
I am trying to download a CSV file using HttpResponse to make sure that
I have been trying to test my application to make sure that all the
I'm trying to make sure that I don't leave any loose ends open in
I'm trying to make sure my web application is always centered on the screen
I'm trying to make sure that my Managed to Unmanaged calls are optimized. Is
I am trying to make sure all my inputs are secure, protecting the server
I'm still picking up ObjC and I'm just trying to make sure I understand
I'm trying to check to make sure an item is visible before I start

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.