title: Reloading assemblies in .NET without creating a new AppDomain
body: <p>I am working on a game engine + editor in C#. The idea is that the user writes all his game code in a project that the editor will then load, so it can list the user-defined classes, as well as instantiate them in a scene editor.</p>
  
  <p>Messing around with the concept of (re)loading a DLL, I can load the DLL, instantiate and invoke methods on a class defined in an external assembly, rebuild the external assembly, and load it again in the host, without restarting the host. And this work (atleast it looks like it), however it is not freeing up memory from the previously loaded assembly.</p>
  
  <p>Loader class:</p>
  
  <pre><code>public class Loader
  {
      // This DLL contains an implementation of ILoadable - ILoadable is declared in a shared project.
      private const string AsmPath = @"D:\Dev\AsmLoading\AsmLoadingImpl\bin\Debug\AsmLoadingImpl.dll";
  
      public ILoadable GetExternalLoadable()
      {
          var asmBytes = File.ReadAllBytes(AsmPath);
          Assembly asm = Assembly.Load(asmBytes, null);
          var loadableInterface = typeof(ILoadable);
          var loadableImpl = asm.GetTypes().First(loadableInterface.IsAssignableFrom);
          return (ILoadable)Activator.CreateInstance(loadableImpl);
      }
  }
  </code></pre>
  
  <p>Running the <code>GetExternalLoadable()</code> 2 or 3 times, the Task Manager reveals a ~1mb increase in RAM usage in my host program, repeating the same action will increase it further, without it ever decreasing.</p>
  
  <p>Is there any way to work around this? I know Unity is doing similar things, except they actually compile the external assembly themselves, but the Unity editor does not consume additional memory when I trigger the recompilation a few times.</p>
  
  <p>So, what I am trying to accomplish is <strong>"live reloading"</strong> of external assemblies.</p>