Original: http://www.jb51.net/article/56682.htm
Background: Microsoft's. NET FRAMEWORK is now in full swing. However,. NET has always been criticized for "too much appetite", eating memory, although Microsoft claims that the GC function and intelligence are very high, but the memory of the issue of recovery has been plagued, especially the WinForm program, the main reason is because. NET program at startup, it needs to be dynamically compiled and loaded by the JIT, this load will all the required resources are loaded in, many resources are only used when the startup. With the XP system as an example, after the program starts, open the Task Manager, you will see a large amount of memory used, you minimize the program, you will find that the memory of the program used to quickly reduce to a small value, and then restore your program, you will find that memory consumption is up again, but the memory consumption is smaller than when you started it, This is a resource optimization process that the operating system proactively completes. Conclusion and Prospect: the project of the Innovation Design competition is fast to the date of delivery, all say WinForm occupy the memory big, so want to see oneself write the remote shutdown Software (Mail_based_remote_shutdown) that is based on the handset mail to occupy the memory situation, The entire development process itself is also trying to write some of the more beautiful code to reduce the system memory footprint, today looked under, just opened the use of 20M of memory, and then a little increase, and finally to more than 80 m, it is unbearable, each time is written back to find their own code is ugly, the role of the system architect embodies. Here are some online about WinForm how to reduce the system memory consumption of information, for reference, to be updated: 1, using the Performance test Tool Dottrace 3.0, it can calculate the code in your program to occupy more than 2 of the memory, forced garbage collection 3, multi-DISPOSE,CLOSE4, With a timer, call every few seconds: SetProcessWorkingSetSize (Process.getcurrentprocess (). Handle,-1,-1); see appendix for details. 5, the release of the time to choose Release6, pay attention to the code when writing less garbage, such as String + string will produce a lot of garbage, you can use Stringbuffer.append7, this. Dispose (); this. Dispose (True); this. Close ();  GC. Collect (); 8, notice the scope of the variable, specifically, if a variable is only used temporarily, 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: Regular 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
1 #regionMemory Recycling2[DllImport ("Kernel32.dll", EntryPoint ="SetProcessWorkingSetSize")]3 Public Static extern intSetProcessWorkingSetSize (INTPTR process,intMinSize,intmaxSize);4 /// <summary>5 ///Freeing Memory6 /// </summary>7 Public Static voidclearmemory ()8 {9 GC. Collect ();Ten GC. WaitForPendingFinalizers (); One if(Environment.OSVersion.Platform = =platformid.win32nt) A { -App.setprocessworkingsetsize (System.Diagnostics.Process.GetCurrentProcess (). Handle,-1, -1); - } the } - #endregion
C # WinForm application uses large memory solution collation (GO)