Garbage Collection Unit Test

Unit tests are useful in order to guarantee code works as it is expected to work. Today my spidey sense told me I might have caused a memory leak. Let’s not consider unmanaged code for now, but wouldn’t it be great if you could write a unit test to prove that a particular object can be garbage collected? I found some interesting ideas online, but no reusable solution. What follows is my attempt at an Assert method which can be used during unit testing.

object empty = new object();
AssertHelper.IsGarbageCollected( ref empty );

It works by using a WeakReference, which references an object while still allowing that object to be reclaimed by garbage collection. The object to test is passed using the ref keyword, which allows the Assert method to set it to null. This allows it to be garbage collected.

public static void IsGarbageCollected<TObject>( ref TObject @object )
	where TObject : class
{
	Action<TObject> emptyAction = o => { };
	IsGarbageCollected( ref @object, emptyAction );
}

public static void IsGarbageCollected<TObject>( ref TObject @object, Action<TObject> useObject )
	where TObject : class
{
	if ( typeof( TObject ) == typeof( string ) )
	{
		// Strings are copied by value, and don't leak anyhow.
		return;
	}

	int generation = GC.GetGeneration( @object );
	useObject( @object );
	WeakReference reference = new WeakReference( @object );
	@object = null;

	GC.Collect( generation, GCCollectionMode.Forced );
	GC.WaitForPendingFinalizers();

	Assert.IsFalse( reference.IsAlive );
}

As it turns out I did write a memory leak in the code I wanted to test. 😦 At least I know about it now! As usual, I added the total code and some unit tests to my FCL library.

WARNING: Due to differences when using the Release configuration (I assume compiler optimizations), some of these unit tests will fail. Be sure to run them in Debug!

Author: Steven Jeuris

I have a PhD in Human-Computer Interaction and am currently working both as a software engineer at iMotions and as a postdoc at the Technical University of Denmark (DTU). This blend of research and development is the type of work which motivates and excites me the most. Currently, I am working on a distributed platform which enables researchers to conduct biometric research 'in the wild' (outside of the lab environment). I have almost 10 years of professional software development experience. Prior to academia, I worked for several years as a professional full-stack software developer at a game development company in Belgium: AIM Productions. I liked the work and colleagues at the company too much to give up entirely for further studies, so I decided to combine the two. In 2009 I started studying for my master in Game and Media Technology at the University of Utrecht in the Netherlands, from which I graduated in 2012.

One thought on “Garbage Collection Unit Test”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: