background:
Microsoft's .NET FRAMEWORK is in full swing now. However, what .NET has always criticized is that it has "too big appetite" and is eating memory wildly. Although Microsoft claims that GC has high functions and intelligence, memory recycling has always been troubled, especially winform programs. The main reason is that .NET programs need to be dynamically compiled and loaded by JIT when they are started. This loading will load all the required resources, and many resources are only used at startup.
Taking the XP system as an example, after the program is started, open the task manager and you will see that the amount of memory occupied is relatively large. If you minimize the program, you will find that the memory occupied by the program is rapidly reduced to a very small value. If you restore your program, you will find that the memory usage has increased again, but it is still smaller than the memory usage when you first started. This is a resource optimization process, which is completed by the operating system actively.
Conclusion and Outlook:
The project of the Innovation Design Competition is almost at the delivery date. It is said that Winform takes up a lot of memory, so I thought about seeing the memory occupancy of remote shutdown software (Mail_Based_Remote_Shutdown) I wrote based on mobile email. During the entire development process, I also tried to write some beautiful code as much as possible to reduce the system memory usage. Today I looked at it. When I first opened it, I occupied 20M of memory, and then increased it little by little, and finally reached more than 80M. It was really unbearable. Every time I wrote it, I turned around and found that my code was ugly, and the role of the system architect was reflected.
Here are some online information about how Winform can reduce system memory usage for reference:
1. Use the performance testing tool dotTrace 3.0, which can calculate which code in your program consumes more memory.
2. Forced garbage recycling
3. Multiple disposes, close
4. Use timer to call: SetProcessWorkingSetSize(().Handle, -1, -1); see the appendix for details.
5. Choose Release when publishing
6. Pay attention to the fact that the code is written less garbage. For example, String + String will generate a lot of garbage. You can use it.
7、(); (True); (); ();
8. Pay attention to the scope of the variable. Specifically, if a variable is only used temporarily, do not define it as a member variable. GC recycles resources based on the relationship network.
9. Detect whether there is a memory leak. For details, please refer to: Memory leak Baidu Encyclopedia
Appendix: Regularly clean up and execute garbage collection code:
// Use a timer in the program, call the function every few seconds, open the task manager, and you will be surprised to find
#region Memory Recycling
[DllImport("", EntryPoint = "SetProcessWorkingSetSize")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
/// <summary>
/// Free memory
/// </summary>
public static void ClearMemory()
{
();
();
if ( == PlatformID.Win32NT)
{
(().Handle, -1, -1);
}
}
#endregion