. NET performance analysis best practices: how to find out. Net code that uses too much memory (basic)

Source: Internet
Author: User

. NET performance analysis best practices: how to find out. Net with too much memoryCode(Basics)

One of the factors that often affect the performance of. NET applications is that the Code consumes too much memory. Many developers often do not pay attention to performance when writing code, so that the applicationProgramThere are performance bottlenecks everywhere. In many cases, developers always focus on the length of code execution time, while dropping the real performance problem. In this articleArticle, We will find out which functions in the Code consume much memory.

This article is relatively simple. We will introduce the CLR profiler tool in detail.

 

Series of articles:

 . NET performance analysis best practices: how to find out. Net code that uses too much memory (Advanced article

 

 

This topic is as follows:

Basic: describes how to use profiler

Advanced: calling the profiler API

 

Basic: describes how to use profiler

You can download CLR profiler from Microsoft's website. Once the CLR profileris downloaded, We can decompress it and then run the statement clrprofiler.exe in the binfolder ".

First, we will introduce the CLR profiler function.

 

CLR profiler is indeed a good tool. By using it, we can understand how a. NET application uses memory. Basically, its functions can be divided into two categories,:

 

 

1. A complete description of how memory is allocated in A. NET application. Therefore, we can see the memory occupied by each type and method.

2. It tells us how many times a method has been called.

 

Note that you should not reproduce the environment or run CLR profiler on the server site above, because it will seriously affect the impact of the program. For example, if our application contains two methods: fun1 and fun2, when we run CLR profiler, it injects some logic into the application, the following figure shows the problem:

 

 

 

Let's take a look at how to use CLR profiler.

 

First, before using CLR profiler, we need to know: what should we do with this tool? Basically, there are two reasons: Check the memory allocation and usage; check the number of times the method is called.

After crlprofiler.exe is started, select the program to be checked, for example:

 

 

 

At this point, the application starts to run, and we can see some information in profiler, such as the content occupied by the application and the size occupied by each generation of objects, as follows:

 

 

 

In fact, the above application is very simple, just as a demo. The function of the above program is to call two methods in a button event:UsesimplestringsAndUsestringbuilders. The two methods are concatenated strings (this is also often used as an example). Let's splice them into 1000 strings, as shown below:

 

 

 

Usesimplestrings:

 

 

Usestringbuilders:

 

Now, we can try to use profiler to see how much memory each method uses. Click the button in the program to let profiler collect information. Then, we click the "histogram" button of profiler, then we can see the memory usage of each type in the program, as shown below:

 

 

To view the content occupied by each method during running, click "Allocation graph" as follows:

 

 

Through this operation, we can see the following figure:

 

 

At first glance, the interface may be very messy, especially when the application is very large. To better view the method we want to see, right-click the interface, click "find routine", and select the input filter conditions, as shown below:

 

 

In this way, you can quickly locate the problem as follows:

 

 

Double-click the method to further expand and view the details, as shown below:

 

 

In, we only saw the memory usage of the "usesimplestrings" method. Because the information displayed on this interface is rough, we just listed some memory usage methods, you can click "0 (everything)" to view the method, as shown below:

 

 

The figure above shows the memory usage at a glance.

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.