/// <summary> /// 设置操作系统实际划分给进程使用的内存容量 /// </summary> /// <param name="hProcess">指定一个进程的句柄</param> /// <param name="dwMinimumWorkingSetSize">用于装载最小进程容量的一个变量</param> /// <param name="dwMaximumWorkingSetSize">用于装载最大进程容量的一个变量</param> /// <returns></returns> [DllImport("kernel32.dll")] public static extern int SetProcessWorkingSetSize(IntPtr hProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize); private static readonly Version myVersion = new Version(1, 0); /// <summary> /// 将当前进程的内存占用尺寸设置到最小 /// </summary> /// <returns>0为成功,-1为失败</returns> public static int SetProcessMemoryToMin() { return SetProcessMemoryToMin(System.Diagnostics.Process.GetCurrentProcess().Handle); } /// <summary> /// 将内存占用尺寸设置到最小 /// </summary> /// <param name="SetProcess">需要设置内存使用范围的程序进程句柄,一般为当前进程,如:System.Diagnostics.Process.GetCurrentProcess().Handle</param> /// <returns>0为成功,-1为失败</returns> public static int SetProcessMemoryToMin(IntPtr SetProcess) { GC.Collect(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { return SetProcessWorkingSetSize(SetProcess, -1, -1); } return -1; }
回收前
回收后