class MemoryClearService
{
private static System.Threading.Timer timer;
public void Start()
{
timer = new Timer(Callback, null, TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(30));
}
static void Callback(object state)
{
MemoryUtil.FlushMemory();
}
}
/// <summary>
/// ref: http://blog.csdn.net/jingang123gz/archive/2008/07/16/2662975.aspx
/// </summary>
class MemoryUtil
{
[DllImport("kernel32.dll")]
public static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);
public static void FlushMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
}