Skip to content

Instantly share code, notes, and snippets.

@malteb247
Created November 3, 2015 13:41
Show Gist options
  • Save malteb247/8e05e0b28c2eb05818d7 to your computer and use it in GitHub Desktop.
Save malteb247/8e05e0b28c2eb05818d7 to your computer and use it in GitHub Desktop.
Execute in seperate app domain
public sealed class Isolated<T> : IDisposable where T : MarshalByRefObject
{
private AppDomain _domain;
private T _value;
public Isolated()
{
_domain = AppDomain.CreateDomain("Isolated:" + Guid.NewGuid(),
null, AppDomain.CurrentDomain.SetupInformation);
Type type = typeof(T);
_value = (T)_domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
}
public T Value
{
get
{
return _value;
}
}
public void Dispose()
{
if (_domain != null)
{
AppDomain.Unload(_domain);
_domain = null;
}
}
}
@malteb247
Copy link
Author

Sometimes you're forced to use bad third party libraries or buggy, memoryleaking DOT.NET libraries. Executing them in an isolated app domain makes you able to kill to whole domain and free the memory after work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment