Use tools to improve program performance and stability

Source: Internet
Author: User

The performance mentioned here mainly refers to the running speed, while the stability focuses on Memory leakage. This article mainly describes how to use the performance profiler of vsts to find performance bottlenecks and make general optimization ideas, and use power toys for. Net CF to find. NET mobileProgramThe basic method for Memory Leak points.

Performance

The performance of a program is not good, that is, for a certain amount of input data, the results take too long, the reason is almost poor Algorithm Or poor implementation. The algorithm is relatively easy to query. After all, the time complexity is roughly estimated. For implementation problems, the hypothetical theoretical analysis is obviously useless. Code Troubleshooting is impractical for a slightly larger program, not to mention the fact that sometimes inefficient functions are misused and it is useless to read the code multiple times. From another point of view, the performance of a program on a large scale is generally one or several bottlenecks. In other words, it may take 90% of the time to convert in two functions, so that even if the remaining 10% optimization is good, the program performance will not go up. Therefore, we need a tool to tell us which function the program spends most of its time in, which may be targeted in a hundred or more ways.
Fortunately, vsts (Visual Studio team system) provides a related tool: Performance profiler. The specific usage of msdn is described above, and the WPF code and vs2010 have more and more powerful features that you can learn on your own. However, vs08 does not seem to provide statistical functions for every line of code, and this data is very important. If you want to get it, the method I know now is to just note the line of code and run it to see how different it is from the normal running time... Compile (however, vs2010 seems to provide this function)
After knowing the performance bottleneck of a program, the next step is targeted optimization. Due to experience limitations, my personal current performance problems are mainly caused by two factors: Unnecessary redundant execution and misuse of inefficient functions/classes. The previous one is easy to understand. For example, if a computation is performed, the result is stored in array, however, after a's content was not changed, the same computation was performed. This results in a waste of computing resources. There seems to be no special solution for this type of problem. You can only troubleshoot the code based on the bottleneck. The second problem is that some classes are not clearly focused on usage. For example, setpixel () and getpixel () in GDI + are inefficient, and lockbits () is used () it is more efficient to directly read and write data in memory. For example, the union operation of combinedgeometry is more than 200 times slower than that of geometrygroup. For this type of problem, when you find that a function is taking up too much time, check msdn and check the efficiency description, which will be quickly solved. Here is a previous instance. For more information, see.

Stability

Although. NET Framework provides automatic garbage collection and other functions to prevent memory leakage, we do not need to be as careful as C ++, but the memory leakage problem still exists. The reason is that many resources are not automatically released by. net, such as the GDI handle and the GDI + image handle (which encapsulates the GDI in essence. If these resources are not collected manually in time, system problems may occur. For example, when developing a mobile phone program, you often encounter a situation where the outofmemoryexception is full and the system menu cannot be properly displayed.
Before seeking a solution, we must understand whether C # provides an automated solution for these resources, that is, the idisposable interface. When implementing a class that calls system resources, we should try to use using or try/finally to automatically release the used resources, and also implement the idisposable interface for this class, it is king to block the source of Memory leakage with good habits during coding.
After understanding how C # handles unmanaged resources, we need to know two information to maintain program stability: Is there a memory leak in the current program? Where is the memory leakage? This is especially important for mobile development. Again, we are glad that Microsoft has provided the. NET memory debugging tool for the mobile phone: Power toys for. NET Compact framework. For the general usage, see here.
With power toys, we can easily see which memory leakage occurs. For example, if a problem occurs, it is easy to know that bitmap is not immediately dispose after being created. At the same time, because the Delta value per second is constant to 2, it can be inferred that it is related to an interval> = 500ms timer. With these clues, it is easy to troubleshoot the problem. Because the data is collected in real time, you can determine the approximate code segment based on the time when the leakage occurs.

Figure 1 use. Net CF remote Performance Monitor to determine Memory leakage

In general, this article introduces two tools, which provide some ideas for improving the performance of desktop programs and detecting the memory leakage of mobile programs in windows. The good news is that these jobs all have related tools. Unfortunately, the degree of automation is still not enough. The problem can be solved quickly only when the coding personnel have a strong literacy.

for C #'s lack of technical knowledge on bbs dotnet, this article also seems to show that although C # has a low entry threshold, however, it is not easy to write a good program. The experts can also reach the "Irreplaceable" realm in the company.

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.