protected void ReleaseMemory() { if (Interlocked.Exchange(ref MemLocker, 1) == 0) { try { //long m = 100 * 1024; //GC.AddMemoryPressure(m); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); //GC.WaitForFullGCComplete(); //GC.Collect(); //GC.RemoveMemoryPressure(m); var proc = Process.GetCurrentProcess(); EmptyWorkingSet(proc.Handle); App.LogInfo("ReleaseMemory..."); } finally { Interlocked.Exchange(ref MemLocker, 0); } } else { Thread.Sleep(20 * 1000); } }
public static void Unload(Sandboxer box) { if (box == null || box._Domain.IsDefaultAppDomain()) { return; } if (_boxes != null) { bool ok = _boxes.Remove(box); #if DEBUG App.LogInfo("Sandbox Ref Decrement {0} {1}", box.Name, ok); #endif } //AppDomain不能在IDisposable中卸载? //延迟卸载确保AppDomain内部执行完毕否则会引发AppDomainUnloadedException new JobTimer(state => { var b = (Sandboxer)state; try { #if DEBUG App.LogInfo("Sandbox Unload {0}", b.Name); #endif AppDomain.Unload(b._Domain); } catch (Exception ex) { App.LogError(ex, "Sandboxer Unload"); #if DEBUG throw; #endif } }, DateTime.Now.AddSeconds(16d)).Start(box); }