A blank winform occupies dozens of megabytes of memory in the task manager, which is really terrible! There are usually three methods:
1. Leave him alone.
CLR & GC automatically manages memory usage and automatically adjusts it according to the current environment parameters to achieve an optimal running efficiency.
2. Set the maximum working set size allowed by the hosting process.
1 process. getcurrentprocess (). maxworkingset = (intptr) (1024*1024*5
1 process. getcurrentprocess (). maxworkingset = (intptr) (1024*1024*5 );
3. Use setprocessworkingsetsize to transfer some physical memory usage to virtual memory.
1 [dllimport ("kernel32.dll")]
2 public static extern bool setprocessworkingsetsize (intptr proc, int min, int max );
3
4 private void button#click (Object sender, system. eventargs E)
5 {
6 setprocessworkingsetsize (process. getcurrentprocess (). Handle,-1,-1 );
7
1 [dllimport ("kernel32.dll")]
2 public static extern bool setprocessworkingsetsize (intptr proc, int min, int max );
3
4 private void button#click (Object sender, system. eventargs E)
5 {
6 setprocessworkingsetsize (process. getcurrentprocess (). Handle,-1,-1 );
7}
Note that the second and second methods may affect program performance to some extent. Set a reasonable working set size, or use setprocessworkingsetsize when the program is started and idle (application. idle). After all, reducing memory usage also has some benefits for system operation.
Example:
1 private void timerjavastick (Object sender, system. eventargs E)
2 {
3 // use the timer to add the current physical memory usage (MB) to the list box.
4 string S = string. Format ("{0}", process. getcurrentprocess (). workingset/1024/1024 );
5 This. listbox1.items. insert (0, S );
6}
7
8 [dllimport ("kernel32.dll")]
9 public static extern bool setprocessworkingsetsize (intptr proc, int min, int max );
10
11 private void button#click (Object sender, system. eventargs E)
12 {
13 // reduce memory usage
14 setprocessworkingsetsize (process. getcurrentprocess (). Handle,-1,-1 );
15
1 private void timerjavastick (Object sender, system. eventargs E)
2 {
3 // use the timer to add the current physical memory usage (MB) to the list box.
4 string S = string. Format ("{0}", process. getcurrentprocess (). workingset/1024/1024 );
5 This. listbox1.items. insert (0, S );
6}
7
8 [dllimport ("kernel32.dll")]
9 public static extern bool setprocessworkingsetsize (intptr proc, int min, int max );
10
11 private void button#click (Object sender, system. eventargs E)
12 {
13 // reduce memory usage
14 setprocessworkingsetsize (process. getcurrentprocess (). Handle,-1,-1 );
15}