Memory optimization software uses a method

Source: Internet
Author: User

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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.