using Microsoft.Win32; using System; using System.Runtime.InteropServices; namespace Common.system { /// /// 释放内存帮助类 创建人:赵耀 创建时间:2020年8月12日 /// public class FreeMemoryHelper { [DllImport("kernel32.dll")] private static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max); /// /// Close、Hide、最小化页面时调用此方法,释放占用内存并重新分配 将暂时不需要的内容放进虚拟内存,当应用程序重新激活时,会将虚拟内存的内容重新加载到内存。 /// 不宜过度频繁的调用该方法,频繁调用会降低使使用性能。 创建人:赵耀 创建时间:2020年8月12日 /// public static void ReallocateMemory() { GC.Collect(); GC.WaitForPendingFinalizers(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); } } /// /// ---屏蔽DevExpress试用信息 与内存释放无关 /// public static void SetDate() { CreateKey(); RegistryKey currentUser = Registry.CurrentUser; RegistryKey registryKey = currentUser.OpenSubKey("SOFTWARE\\DevExpress\\Components", writable: true); if (registryKey != null) { registryKey.GetValue("LastAboutShowedTime"); string value = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"); registryKey.SetValue("LastAboutShowedTime", value); } currentUser.Dispose(); } // 摘要: 创建键值 private static void CreateKey() { RegistryKey currentUser = Registry.CurrentUser; if (currentUser.OpenSubKey("SOFTWARE\\DevExpress\\Components", writable: true) == null) { RegistryKey registryKey = currentUser.CreateSubKey("SOFTWARE\\DevExpress\\Components"); if (registryKey != null) { registryKey.CreateSubKey("LastAboutShowedTime")?.SetValue("LastAboutShowedTime", DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")); registryKey.CreateSubKey("DisableSmartTag")?.SetValue("LastAboutShowedTime", false); registryKey.CreateSubKey("SmartTagWidth")?.SetValue("LastAboutShowedTime", 350); } } currentUser.Dispose(); } } }