如何释放内存:
1 [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
2 public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
3
4 /// <summary>
5 /// 释放内存
6 /// </summary>
7 public static void ClearMemory()
8 {
9 GC.Collect();
10 GC.WaitForPendingFinalizers();
11 if (Environment.OSVersion.Platform == PlatformID.Win32NT)
12 {
13 SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
14 }
15 }
如何获取当前应用占用的内存大小:
1 /// <summary>
2 /// 释放内存
3 /// </summary>
4 public static void ClearMemory()
5 {
6 //获得当前工作进程
7 Process proc = Process.GetCurrentProcess();
8 long usedMemory = proc.PrivateMemorySize64;
9 if (usedMemory > 1024 * 1024 * 20)
10 {
11 GC.Collect();
12 GC.WaitForPendingFinalizers();
13 if (Environment.OSVersion.Platform == PlatformID.Win32NT)
14 {
15 SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
16 }
17 }
18 }