When you leave, I leave too

There was discussion at work about assembly unloading. In order to get the CLR to unload an assembly, you need to use a fresh AppDomain and only load a given type into that AppDomain. When the AppDomain is unloaded, the Assembly’s reference count is decremented, and the Assembly can be unloaded.

It is fairly easy to use windbg to track the module load and unload events to check this behaviour. We can build an Assembly that we intend to load into a new AppDomain.


We can then write a Main method which creates a new AppDomain and then loads this Assembly in to it.


If we run this executable from Windbg, as long as we have set the event filtering to output module unloads (which isn’t the default behaviour), we see the module is loaded into the AppDomain and then unloaded after the AppDomain is killed.


All as expected, but it is good to check one’s expectations from time to time.

This entry was posted in Computers and Internet. Bookmark the permalink.

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 )

Google+ photo

You are commenting using your Google+ 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