winform降低功耗总结

这里整理了一些网上关于Winform如何降低系统内存占用的资料,供参考:

1、使用性能测试工具dotTrace 3.0,它能够计算出你程序中那些代码占用内存较多
2、强制垃圾回收
3、多dispose,close
4、用timer,每几秒钟调用:SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);具体见附录。
5、发布的时候选择Release
6、注意代码编写时少产生垃圾,比如String + String就会产生大量的垃圾,可以用StringBuffer.Append
7、this.Dispose();    this.Dispose(True);   this.Close();    GC.Collect();   
8、注意变量的作用域,具体说某个变量如果只是临时使用就不要定义成成员变量。GC是根据关系网去回收资源的。
9、检测是否存在内存泄漏的情况,详情可参见:内存泄漏百度百科

附录:定期清理执行垃圾回收代码:

 1 //在程序中用一个计时器,每隔几秒钟调用一次该函数,打开任务管理器,你会有惊奇的发现
 2  
 3 #region 内存回收
 4 [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
 5 public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
 6 /// <summary>
 7 /// 释放内存
 8 /// </summary>
 9 public static void ClearMemory()
10 {
11 GC.Collect();
12 GC.WaitForPendingFinalizers();
13 if (Environment.OSVersion.Platform == PlatformID.Win32NT)
14 {
15 App.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
16 }
17 }
18 #endregion

            

原文地址:https://www.cnblogs.com/lingbohome/p/4699989.html