C # WinForm applications reduce system memory consumption Methods [turn]

WinForm program has been a point with a big problem, a long running time, more and more, the Internet recently found a stick, tried the above method, from a process point of view, the memory does go down, and the results were good.

 

Here we compiled some information online about how to reduce the Winform system memory footprint, for reference:

1, the use of performance testing tool dotTrace 3.0, it is possible to calculate your program code that take up more memory
2, mandatory garbage collection
3, more than the Dispose, use Close
4, a timer, a few seconds per call: SetProcessWorkingSetSize (Process.GetCurrentProcess ( ) .Handle, -1, -1); specific appendix.
5, when the release of select Release
6, pay attention to the time of writing the code to produce less garbage, such as String + String will generate a lot of garbage, you can use StringBuffer.append
7, this.Dispose (); this.Dispose (True); the this. use Close (); GC.Collect ();  
8, pay attention to the scope of variables, particularly if only temporary use of a variable, do not define as a member variable. GC is based on the network of relationships to reclaim resources.
9, detects the presence of a memory leak details, please see: Memory Leak Baidu Encyclopedia

Appendix: regular cleaning garbage collection Code:
// in the program with a timer, once every few seconds to call the function, open the Task Manager, you will be surprised to find
#region 内存回收
[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
/// <summary>
/// 释放内存
/// </summary>
public static void ClearMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
App.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
#endregion

 

 

Original: https://www.jb51.net/article/56682.htm

Guess you like

Origin www.cnblogs.com/startdo/p/12001354.html