The Inquisitive Coder - Davy Brion’s Blog

Thinking outside of the box

Archive for the 'Memory Management' Category


Compiler Enforced Disposal?

Posted by Davy Brion on 16th November 2008

I stumbled upon a blog post by Kim Hamilton on When To Call Dispose. The most interesting part about the post is that this is from someone who works on the Base Class Libraries team. The following part is especially striking:

A recent internal email thread unearthed extreme differences of opinion about when Dispose(void) should be called on an IDisposable. The rival suggestions were:

1. Always call Dispose

2. Avoid calling Dispose; rely on cleanup at finalization

This led to a long discussion and a realization that — while it seems like we’ve said everything there is to say about Dispose — it’s time for some more Dispose guidance.

I don’t know about you guys, but the thought of Base Class Library developers not being sure on when Dispose should be called is something that makes me quite uncomfortable. If anyone should know, it’s these guys, right?

Another interesting quote from the post:

Whatever distinction we eventually use, API docs should explicitly call out IDisposables that must be Disposed.

Great… now we are supposed to rely on the MSDN docs to find out which IDisposables need to be disposed and which ones don’t.

I kinda like the IDisposable pattern, but it’s truly a shame that there are so many types that implement the IDisposable interface without really needing to implement it. I’ve always considered the IDisposable interface to be a contract which states “if you use me, you must dispose me”. For certain types, this is true. Unfortunately, for other types that implement the interface it’s not true even though the IDisposable documentation is pretty clear about this:

When calling a class that implements the IDisposable interface, use the try/finally pattern to make sure that unmanaged resources are disposed of even if an exception interrupts your application.

In a perfect .NET world, there would be no types implementing IDisposable without actually requiring disposal (IMHO). Again, i think the interface is a contract that must be followed by everyone who makes use of it. Then again, it does feel pretty pointless to dispose types of which you know that it really doesn’t make a difference.

The introduction of explicit Code Contracts in .NET 4.0, got me thinking about trying to make disposal of types that really require it enforced by the compiler. What i have in mind is not possible with the Code Contracts in .NET 4.0, but it’s something that i think would be a good addition to the .NET Framework.

Suppose we could do the following:

    [RequiresDisposal]

    public class MyDisposableType : IDisposable

    {

        public void DoSomething()

        {

            // does something really cool and/or important

        }

 

        public void Dispose()

        {

            // this would contain an important disposable implementation

        }

    }

Basically just a type which implements IDisposable, and there’s an attribute that specifies that the disposal of this type is really required. The following code should then fail to compile:

        private void SomeMethod()

        {

            new MyDisposableType().DoSomething();

        }

Whereas this code would compile without errors:

        private void SomeMethod()

        {

            using (var disposableType = new MyDisposableType())

            {

                disposableType.DoSomething();

            }

        }

It’s just a thought and obviously, this isn’t possible right now. But i think it would be great if it were possible. We could do all sorts of things with this approach… for instance, if an object owns a reference to an IDisposable type which has the [RequiresDisposal] attribute, the compiler could enforce that the object owning the reference must implement IDisposable and have the [RequiresDisposal] attribute as well. I think we’d end up with a system were types that really require disposal can’t be used without properly disposing them.

Thoughts?

Posted in Memory Management | 10 Comments »

Do Not .Dispose In The PreRender Event

Posted by Davy Brion on 1st October 2008

We had this weird issue with a project at work… sometimes, the web application would just completely hang for a couple of minutes. A coworker started looking at the problem, and he quickly found out that in some cases, calls to the WCF service would just completely block. Even when debugging the web application and the WCF service on our development machines, so it wasn’t an issue of throughput or concurrency or anything like that on the server.

We were clueless as to why the calls were actually blocking. We had a few scenarios that would usually (but not always) trigger the bug, but it also occurred in other situations, seemingly at random. So my coworker spent many hours trying to find out why it was happening. After a lot of debugging on his part, he found out that sometimes our WCF proxies weren’t being disposed, which left Channels to the service open which caused new proxies to block whenever the number of allowed concurrent channels was reached. My first reaction was “huh, how could that be? i’m disposing of them in the page’s PreRender event…”.

First of all, i’m far from an ASP.NET guru, so those of you who do know a lot about it probably already understand the problem. I figured that the PreRender event would always occur, and that it would be the best place to dispose the WCF proxy. That turned out to be a two-for-one brain fart on my part. First of all, the PreRender event is obviously not fired when you do a Server.Redirect or Server.Transfer, so whenever we navigated to another page, we did not dispose our WCF proxy. Secondly, Page inherits IDisposable, which i completely missed apparently. So, had i simply overridden the Dispose method and dispose the proxy in there before calling base.Dispose() and then we would’ve never had this problem to begin with.

Lesson of the day: no matter how careful you try to be, you can still fuck up pretty bad sometimes :)

Update: i actually used the PreRenderComplete event instead of the PreRender event… still, a bad place to dispose of disposable objects ;)

Posted in ASP.NET, Memory Management, WCF | 1 Comment »

Why you should always unscubscribe event handlers

Posted by Davy Brion on 17th August 2008

Events in .NET are very useful. But if you’re not careful, they might prevent objects from being removed from memory by the Garbage Collector (GC). Let’s call an object that publishes an event a Publisher and an object that subscribes to an event a Subscriber. If a Subscriber subscribes to a Publisher’s event, the Publisher will indirectly hold a reference to the Subscriber (because of how events in .NET are implemented). If the Publisher is an object that will stay around for a long time, this could prevent the Subscriber from being removed from memory when it’s no longer used.

Suppose we have the following Publisher class:

    public class Publisher

    {

        public event EventHandler MyEvent;

 

        public void FireEvent()

        {

            if (MyEvent != null)

            {

                MyEvent(this, EventArgs.Empty);

            }

        }

    }

And then we have a BadSubscriber class:

    public class BadSubscriber

    {

        public BadSubscriber(Publisher publisher)

        {

            publisher.MyEvent += publisher_MyEvent;

        }

 

        void publisher_MyEvent(object sender, EventArgs e)

        {

            Console.WriteLine(“the publisher notified the bad subscriber of an event”);

        }

    }

Notice how the BadSubscriber subscribes to the Publisher’s event, but never unsubscribes from it.

Then we have the GoodSubscriber:

    public class GoodSubscriber : IDisposable

    {

        private readonly Publisher publisher;

 

        public GoodSubscriber(Publisher publisher)

        {

            this.publisher = publisher;

            publisher.MyEvent += publisher_MyEvent;

        }

 

        void publisher_MyEvent(object sender, EventArgs e)

        {

            Console.WriteLine(“the publisher notified the good subscriber of an event”);

        }

 

        public void Dispose()

        {

            publisher.MyEvent -= publisher_MyEvent;

        }

    }

The GoodSubscriber is very similar to the BadSubscriber, but it unsubscribes from the Publisher’s event when it is disposed. Note that the implementation of the IDisposable interface is hardly ideal, but for the purpose of this demo it’s sufficient.

The following code illustrates the problem:

        static void Main(string[] args)

        {

            var publisher = new Publisher();

            var badSubscriber = new BadSubscriber(publisher);

            var goodSubscriber = new GoodSubscriber(publisher);

 

            var badSubscriberRef = new WeakReference(badSubscriber);

            var goodSubscriberRef = new WeakReference(goodSubscriber);

 

            publisher.FireEvent();

 

            badSubscriber = null;

            goodSubscriber.Dispose();

            goodSubscriber = null;

 

            GC.Collect();

 

            Console.WriteLine(goodSubscriberRef.IsAlive ? “good publisher is alive” : “good publisher is gone”);

            Console.WriteLine(badSubscriberRef.IsAlive ? “bad publisher is alive” : “bad publisher is gone”);

 

            publisher.FireEvent();

        }

The output of that code is the following:

the publisher notified the bad subscriber of an event
the publisher notified the good subscriber of an event
good publisher is gone
bad publisher is alive
the publisher notified the bad subscriber of an event

Notice how the GoodSubscriber is removed from memory by the GC and how the BadSubscriber is still in memory even though we no longer have a reference to it and the GC has performed a collection.

So keep in mind that every event handler you subscribe to an event should be properly unsubscribed from the event when your instance is no longer needed. Not doing so might lead to instances not being removed if the event’s publisher has a longer lifetime than your object will have. I’d actually suggest you’d always properly unsubscribe from any event you subscribe to. Even if you know that the Publisher won’t have a longer lifetime than your instance will have, who’s to say some future change in the code won’t extend the lifetime of the Publisher over that of the Subscriber? When that happens, you might end up with a bunch of instances that will remain in memory as long as the Publisher remains in memory.

Posted in Memory Management | 16 Comments »

.NET Memory Management

Posted by Davy Brion on 16th August 2008

Introduction

Garbage Collection sure is great, isn’t it? We don’t have to keep track of all the memory we’ve allocated and we don’t need to release that memory when it’s no longer needed. Because that is after all what the Garbage Collector does for us, without us having to worry about it. This is actually a widespread misconception among many .NET developers. It’s true that Garbage Collection makes memory management a lot easier, but we simply can’t rely on it all the time. There are most certainly some things you must always keep in mind when it comes to memory management in .NET.

Different Kinds Of Resources, Different Consequences

In most pieces of code, you use variables. A lot of these variables are references to objects. When your variables go out of scope, they no longer exist. When those variables are references to objects, only your reference is gone and the actual object that was referred to is still in memory somewhere. The Garbage Collector (GC) makes sure that orphaned objects (objects that no longer have a usable reference to them) are removed from memory. This is an automatic process within the .NET runtime. Essentially, the GC periodically performs some checks on objects and if it comes to the conclusion that certain objects are no longer needed, it will remove them from memory. This is a gross oversimplification of how it really works, but that is what it comes down to and that is also how most people think about the GC.

The GC in .NET is very smart and will almost always do a better job of memory management than you will. But there is one huge downside to it: the GC runs periodically and you have no deterministic way of knowing when it will run. Sure, you can instruct the GC to perform a collection but in most cases that will actually have a negative impact on the memory management of your application. I won’t go into the details here, but i will provide a follow-up post on this later on. For now, keep in mind that forcing a garbage collection to occur is really something you should avoid.

So is it really a problem that you don’t know for certain when the GC will run? It depends on which kind of objects that need to be removed from memory. There are basically 2 kinds: managed and unmanaged resources. Managed resources are typical .NET types. Unmanaged resources (also referred to as Native Resources) are things that fall outside of the scope of the .NET managed environment. These are usually things that are available within the Operating System, or that are available through lower-level development API’s (such as the Win32 API for instance). Unmanaged resources can’t be cleaned up automatically by the .NET runtime, so if you use them directly, you are responsible for cleaning up after you’ve used them. Luckily, a lot of managed types are available that take care of the dirty details for you. For instance, if you need to use a file in .NET, you’ll typically use a FileStream instance or something else that easily makes the content of the file available to you, or that easily allows you to write content to a file. The FileStream is a managed type, but it uses unmanaged resources to implement the functionality it offers.

Now think about this: you are using a managed type, so you shouldn’t need to perform any cleanup, right? However, if that managed type uses unmanaged resources then they still need to be cleaned up. And those unmanaged resources should be cleaned up as soon as possible because they can be quite expensive. These types usually offer a way to clean up the unmanaged resources they use in a deterministic manner. They usually implement the IDisposable interface, which exposes a Dispose method. When you call the Dispose method, the unmanaged resources are cleaned up right then and there and then there’s no need to wait for the garbage collector, which again, could be quite expensive when you (either directly or indirectly) have a bunch of unmanaged resources in memory that are waiting to be cleaned up.

Disposable Managed Resources

I call types that implement the IDisposable interface Disposable Managed Resources. They are indeed managed types and thus they are guaranteed to be cleaned up when the GC comes around and notices that instances of these types are no longer needed. However, if you merely trust on the GC to clean up instances of these types you are taking quite a risk. Any expensive resource it may hold might be cleaned up a lot later than it could have been. Which can be very inefficient, and thus, quite costly.

If a type implements the IDisposable interface, it usually has a good reason for doing so (there are of course exceptions to the rule). It is essentially a way of telling consumers of the type that it offers a deterministic way of cleaning up the resources it consumes and i believe you should take advantage of that. If you don’t, you may end up with inefficient memory management, and that’s when people start complaining that Garbage Collection is ‘evil’ or that it ’sucks’. In most cases, people who feel that way simply don’t know how to use it properly. The IDisposable interface and the Dispose Pattern (which we’ll get to in a minute) allow you to avoid most of the issues that are generally attributed to the Garbage Collection in general.

So how do we deal with this? First of all, if a type uses unmanaged resources, it should implement the IDisposable interface using the Dispose Pattern. Secondly, if any of your types use other types that implement IDisposable, it is your responsibility to make sure these types are properly disposed of. You either dispose them when you no longer need them, or you must implement IDisposable and the Dispose Pattern yourself to make sure the Disposable Managed Resources you depend on are indeed properly disposed of. If you need to implement IDisposable, your type effectively becomes a Disposable Managed Resource itself. A lot of people think that this isn’t necessary, but by not doing so they are breaking the contract that the IDisposable interface implies. If a type implements IDisposable then either that type or any other type it may use probably uses unmanaged resources somewhere and you want to see these get cleaned up as soon as possible. Because of that, i believe it’s best to implement IDisposable yourself if you’re holding any reference to a type that also implements it.

The Dispose Pattern

Implementing the IDisposable interface can be as easy as merely providing a public Dispose method where you perform your cleanup. That’s really not a good way of doing it though, as it could lead to a bunch of other problems which might actually make the situation worse. The best way to implement the IDisposable interface is to implement the Dispose Pattern, which looks like this:

    public class MyDisposableClass : IDisposable

    {

        private bool disposed = false;

 

        public void Dispose()

        {

            Dispose(true);

            // prevent this object from being placed in the finalization queue if some

            // derived class provides a finalizer

            GC.SuppressFinalize(this);

        }

 

        protected virtual void Dispose(bool disposing)

        {

            if (!disposed)

            {

                if (disposing)

                {

                    // dispose all Disposable Managed Resources here

                }

 

                // dispose all Unmanaged Resources here

 

                // set disposed to true to prevent the code above from being

                // executed a second time

                disposed = true;

            }

        }

    }

If you don’t need a finalizer it’s best not to provide one (we’ll discuss finalizers later on). If you do need one, you would implement it like this:

        public ~MyDisposableClass()

        {

            Dispose(false);

        }

So what’s so good about the Dispose pattern? Well, we make a clear distinction between what should happen when the object is disposed through the Dispose method, or when it’s being cleaned up by the finalizer. When the clean up occurs through a call to Dispose, it calls the protected virtual Dispose method with the disposing parameter set to true. When this parameter is true, you should call the Dispose method of each Disposable Managed Resource you’re holding a reference to. Outside of the if-statement, you should clean up each unmanaged resource you may be holding.

If your class (or a derived class) implements a finalizer method, it should call the Dispose method with the disposing parameter set to false. The reason for this is that the order in which objects are finalized is not specified. If you’re not careful, you could accidentally call the Dispose method of a Disposable Managed Resource which may have already been finalized as well. This would cause an exception and a finalizer method should never ever throw an exception because that could keep other objects from being finalized.

If you can, try to put this pattern into a reusable base class and make your Disposable Managed Resource holding types inherit from this. An example of this approach can be found here.

Using Disposable Managed Resources

In a lot of cases, you will use a Disposable Managed Resource within the scope of one method. In that case, you certainly don’t need to implement IDisposable. There are two things to keep in mind though. If you receive the Disposable Managed Resource as a method parameter, you are not responsible for disposing it! Somebody else created it, let them take care of the disposal. Things will most likely go wrong if you dispose objects that have been giving to you by somebody else. So in this case, you would just use the object and not worry about it any further:

        public void DoSomethingInteresting(MyDisposableClass disposableResource)

        {

            // … some code could go here

            disposableResource.DoWhateverItIsThatMakesYouSoSpecial();

            // … some code could go here

 

            // we reach the end of the method and we haven’t Disposed the

            // Disposable Managed Resource because it wasn’t our responsibility

        }

However, if you create the object yourself, then you are responsible for getting rid of it properly. In that case, use a using block to make sure the object is disposed of even in case of unhandled exceptions:

        public void DoSomethingInteresting()

        {

            // … some code could go here

            using (var myDisposableResource = new MyDisposableClass())

            {

                myDisposableResource.DoWhateverItIsThatMakesYouSoSpecial();

            }

            // … some code could go here

 

            // we reach the end of the method and we properly Disposed the

            // Disposable Managed Resource because it was our responsibility

        }

In case you don’t know, the using-block guarantees that the Dispose method will be called when we leave the scope of the block. It can get pretty tricky sometimes if you’re passing the Disposable Managed Resources to other objects that will hold a reference to them which will remain in use after you’ve left the using block. In this scenario, it’s possible that the object you’ve passed the Disposable Managed Resource to will try to invoke methods on the disposed instance. Also a situation you most certainly want to avoid because it will either cause unexpected behavior or even unhandled exceptions. The unhandled exceptions are easy to figure out, but the unexpected behavior might be difficult to debug. The key in this scenario, is to figure out a way to not call Dispose on the resource until the other object is done with it. Sometimes it might be enough to put your using block on a slightly higher level, other times you’ll have to keep a reference to the resource yourself, which effectively means you’ve now become an owner of the Disposable Managed Resource.

Owning Disposable Managed Resources

If you need to hold a reference to a Disposable Managed Resource, you are either the owner of the object or at least someone who owns a stake in the lifetime of the object. If you are the owner, and you do not pass the Disposable Managed Resource to any other object then the solution is easy: implement IDisposable. If you do pass the Disposable Managed Resource to other objects, then it can get tricky. Will they only use it to perform some action or will they hold a reference to it? If they hold a reference to it, it means that they too have a stake in the lifetime of the resource. Depending on the implementation of the other types, they may or may not call the Dispose method of the instance you created. Which could cause unexpected behavior or exceptions in your code. Make sure you are prepared for that if you’re passing these objects around. But you should definitely dispose of them in your own Dispose method.

If you didn’t create the Disposable Managed Resource, but you do need to hold a reference to it, then you’ve got some questions to answer. Does the type which provided you with the instance expect you to be solely responsible for the lifetime of the object? In case of factory classes or factory methods, the answer is usually ‘yes’. If you’ve been given the instance by something that will probably make use of that same instance, i think it’s better not to dispose it. Some people will probably disagree, but i don’t think it’s my responsibility to dispose objects that i didn’t create, unless the instance was given to me by a factory class or a factory method. I would expect that the real owner of the resource would dispose of it. Still, it’s a difficult call to make.

Finalizers

Finally, i’d like to add a few words about Finalizers in .NET. A finalizer is similar to a destructor in C++, in that it is the code that is run when your object is being removed from memory. The big difference is that you never know for sure when it will be run, which is why the IDisposable interface and the Dispose pattern was created. There are a lot of misconceptions going around about finalizers, so keep the following comments in mind.

First of all, finalizers only make sense when you have direct references to unmanaged resources. If you have unmanaged resources, implement IDisposable and provide a finalizer that calls the Dispose overload with the disposing parameter set to false. This should really be the only code inside your finalizer method.

Finalizers also come with a performance penalty. When finalizable objects are created, pointers to these objects are added to the finalization queue of the GC. These finalizable objects are collected less frequently because the GC performs finalizations through its finalization queue. This finalization process is not executed as frequently as regular garbage collections because it’s quite costly. So not only are finalizable objects cleaned up less frequently, it takes more processing power to do so. So if you have a lot of finalizable instances, your code’s performance can easily be impacted because of this.

Conclusion

I hope i made it clear that there is a lot more to automatic memory management in .NET than simply relying on the GC to take care of all of the details for you. There is a lot more stuff that i didn’t cover in this post (it’s long enough already) but most .NET developers will probably get by pretty well with the advice given in this post. It’s not an easy subject to explain so i hope everything was clear :)

Posted in Memory Management, Patterns, Performance, Software Development | 3 Comments »

Disposing of the IDisposable implementation

Posted by Davy Brion on 17th June 2008

Everytime i need to implement the IDisposable interface i have to lookup the recommended way of doing so. That in itself is a bad sign, so i figured i might as well get rid of this by putting the implementation in a reusable base class, based on the officially recommended way:

    public abstract class Disposable : IDisposable

    {

        private bool disposed;

 

        public void Dispose()

        {

            Dispose(true);

            GC.SuppressFinalize(this);

        }

 

        protected void Dispose(bool disposing)

        {

            if (!disposed)

            {

                if (disposing)

                {

                    DisposeManagedResources();

                }

 

                DisposeUnmanagedResources();

                disposed = true;

            }

        }

 

        protected void ThrowExceptionIfDisposed()

        {

            if (disposed)

            {

                throw new ObjectDisposedException(GetType().FullName);

            }

        }

 

        protected abstract void DisposeManagedResources();

        protected virtual void DisposeUnmanagedResources() {}

    }

So now i can simply inherit from Disposable, and i just need to implement the two abstract methods. Here’s a made up example to illustrate this:

    public class MyExpensiveResource : Disposable

    {

        private FileStream fileStream;

        private MemoryStream memoryStream;

 

        public MyExpensiveResource(string path)

        {

            fileStream = new FileStream(path, FileMode.Open);

            memoryStream = new MemoryStream();

        }

 

        public void DoSomething()

        {

            ThrowExceptionIfDisposed();

 

            // … something

        }

 

        protected override void DisposeManagedResources()

        {

            if (fileStream != null) fileStream.Dispose();

            if (memoryStream != null) memoryStream.Dispose();

        }

    }

Obviously, you can’t use the Disposable base class if you’re already inheriting from another base class so in that case you’d still have to implement the IDisposable interface.

Update: Here’s a thread-safe version of this idea.

Posted in Memory Management, Patterns, Performance | 17 Comments »