Summary of C WinForm applications reducing system memory footprint

  • 2020-11-18 06:24:42
  • OfStack

Background:

Microsoft's.NET FRAMEWORK is now in full swing. But. NET 1 person of straight for what "appetite is too big," binge eating memory, though Microsoft says GC function and intelligence is very high, but the recovery memory problems, 1 straight exist, especially winform program, the main reason is because. NET program at startup, is needed by the JIT dynamic compilation and load, the load will bring all the required resources are loaded in, is only starts with many resources.

XP system as example, the program starts, open the task manager, will see the amount of the amount is larger, minimize the program, you will find the program memory quickly reduced to a very small value, return your program, you will find that the memory footprint is rising again, but the memory footprint than when you just start the value is small, this is a process of resource optimization, the process is operating system actively.

Conclusion and Prospect:

Innovation design competition of the project is close to the date of delivery, all say Winform large memory, so think about look at writing your own mobile email based remote shutdown software (Mail_Based_Remote_Shutdown) memory, throughout the development process in myself try to write one more beautiful code to reduce system memory footprint, look at today, just take up 20 M memory when it is open, and then a little increase, and finally to the more than 80 M, really can't stand, every time is wrote later come back to find your own code is ugly, This is where the role of the system architect comes in.

Here is a collation of some online information on how Winform reduces the system memory footprint for your reference:

1. Use the performance test tool dotTrace 3.0 to figure out which code in your program takes up more memory
2. Mandatory garbage collection
3, dispose, close
4. Use timer, call SetProcessWorkingSetSize(Process.GetCurrentProcess ().Handle, -1, -1) every few seconds; See appendix for details.
5. Select Release for release
6, pay attention to the code to create less garbage, such as String + String will generate 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 variables. Specifically, do not define a variable as a member variable if it is only used temporarily. GC recycles resources based on relationships.
9. Check whether there is a memory leak, please refer to Memory leak Baidu Encyclopedia for details

Appendix: Periodically clean and perform garbage collection code:


// In the program 1 A timer, called every few seconds 1 Next, open the task manager, and you'll be amazed
 
#region Memory recovery
[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
/// <summary>
/// Free memory
/// </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


Related articles: