1, using the Performance test Tool Dottrace 3.0, it can calculate the code in your program takes up more memory
2. Mandatory garbage collection
3, Multi-dispose,close
4, with a timer, every few seconds call: SetProcessWorkingSetSize (Process.getcurrentprocess (). Handle,-1,-1); see appendix for details.
5, release the time to choose release
6, pay attention to the code when writing less garbage, such as String + string will produce a lot of garbage, you can use Stringbuffer.append
7, this. Dispose (); This. Dispose (True); This. Close (); Gc. Collect ();
8, pay attention to the scope of the variable, specifically, if a variable is only temporary use, do not define a member variable. GC is based on the network to recover resources.
9, detect if there is a memory leak, the details can be see: memory leaks Baidu Encyclopedia
Appendix: Periodic cleanup of garbage collection code:
In the program with a timer, every few seconds to call the function, open Task Manager, you will be surprised to find
#region Memory Recycling
[DllImport ("kernel32.dll", EntryPoint = "setprocessworkingsetsize")]
public static extern int SetProcessWorkingSetSize (INTPTR process, int minSize, int maxSize);
<summary>
Freeing memory
</summary>
public static void Clearmemory ()
{
Gc. Collect ();
Gc. WaitForPendingFinalizers ();
if (Environment.OSVersion.Platform = = PLATFORMID.WIN32NT)
{
SetProcessWorkingSetSize (System.Diagnostics.Process.GetCurrentProcess (). Handle,-1,-1);
}
}
#endregion
C # Performance Optimizations