Use AppDomain to load/unload external assemblies

后端 未结 8 2030
遇见更好的自我
遇见更好的自我 2020-12-24 08:39

My scenario is as follows:

  • Create new AppDomain
  • Load some assemblies into it
  • Do some magic with loaded dlls
  • Unload AppDomain to rele
8条回答
  •  萌比男神i
    2020-12-24 09:24

    .Net uses non-deterministic finalization. If you want to see if the memory drops you should do ...

    GC.Collect(); 
    GC.WaitForPendingFinalizers();
    

    ... after the unload. Also unless you have a need to force collection (rather un-likely) you should allow the system to collect on its own. Normally if you feel the need to force collection in production code there is a resource leak typically caused by not calling Dispose on IDisposable objects or for not Releasing unmanaged objects

    using (var imdisposable = new IDisposable())
    {
    }
    //
    var imdisposable = new IDisposable();
    imdisposable.Dispose();
    //
    Marshal.Release(intPtr); 
    //
    Marshal.ReleaseComObject(comObject);
    

提交回复
热议问题