C # memory usage Problems

Source: Internet
Author: User

Written in C #ProgramEvery time a program is started, it consumes more than 20 mb of memory. Writing a slightly more complex WPF program accounts for more than 100 MB. In contrast, the program written with MFC occupies a relatively small amount of memory, and the memory usage of 20 + MB is relatively large. This once made me quite a headache. After all, memory usage is also a very important indicator to measure program quality.

After reading "C #3.0 in a nutshell", I finally understood what was going on. Garbage Collector does not collect garbage all the time, especially for stack variables like C/C ++, even if the program control has exceeded the scope of these variables, garbage Collector does not immediately recycle space. There are two considerations: on the one hand, since the system has free physical memory, why should we sacrifice some performance to spend the effort to maintain a small memory usage? Isn't it good to make full use of idle physical memory and contribute to performance improvement? From the perspective of lazy calculation, it takes a lot of money to selectively recycle the memory that is no longer needed, and it is very easy to release the memory of the entire process at one time after the program ends. If the system only needs the memory to be recycled when the system actually needs the memory, it is a coincidence that, if the system memory is always abundant, garbage collection is never required, this part of time is saved, and the performance of the program is disguised. However, the worst case is that the system needs the memory during running, and there will be no loss for recycling at that time, but it cannot be a coincidence. (The Idea Of Lazy calculation is also introduced in "more effective tive C ++", which has important applications in many programs or systems, including Linux ).

We can modify some C # programs to verify that CLR does. The following statement shows the actual memory usage of the current program:

  1  String  Procname  =  Process. getcurrentprocess (). processname;
2 Using (Performancecounter PC = New Performancecounter
3 ( " Process " , " Private bytes " , Procname ))
4 Statustextblock. Text = (PC. nextvalue () / 1000 ). Tostring ();

Among them, statustextblock. Text is the text of the status bar, and can also be output in the form of MessageBox.

For an existing WPF program, we can know that its actual memory usage is 52 MB at a certain time, in this case, you can use the task manager of Windows XP to observe that the memory usage is 100 + MB. The excess amount is the space that has been released but has not been recycled. At this time, the system has more than 60% free physical memory, so Garbage Collector has not been recycled. If you keep this program running and open a program that occupies a large amount of memory (such as Visual Studio), you can find that the memory usage of this program has become 52 MB + in task manager, this is an ideal result, indicating that the actual memory occupied by this program will not exceed 53 MB, which proves the above theory to some extent. It also shows that C #'s large memory usage is not a defect, but a reasonable means of using system resources. This is consistent with some Linux enthusiasts: Linux memory usage is larger than Windows XP, and it is a reflection of the design intent of rationally utilizing system resources rather than a bug. (Of course, garbage collector's choice of garbage collection timing is related to many factors, not only by the system's idle memory)

If the Task Manager occupies a large amount of memory, you can use the forced garbage collection method. For the above program, if GC. Collect () is called before statistics; memory is forcibly reclaimed, the memory usage shown in Task Manager is changed to 37 MB. This is strange, and it is actually less than the value obtained by the program's internal statistics. According to Google's results, this may be caused by a problem with the CLR program statistics by the task manager in Windows XP. It is said that the problem has improved in Vista, however, my little broken Pentium M 1.2G + 768 mb ram condition is not confirmed by a conditional experiment.

Of course, even with this optimization, the memory occupied by C # is about 50 MB, which is still different from the C ++ program. This may be determined by the principle of the CLR program running, there are.. NET Framework memory. This article only points out that the memory usage of the C # program may not be as exaggerated as the task manager in Windows XP, and proposes a verification method. As for how to further reduce the memory usage of the C # program, more accurate and reliable memory statistical methods may be required. The updated version of the. NET Framework and a more detailed description of the C # memory allocation principle.

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.