Check the high memory usage in. net code)

Source: Internet
Author: User

Haha, I did nothing yesterday. I went around in codeproject and saw this article by chance. I understood it, So I translated it. When I practiced English, I also enhanced my understanding of the article, I found that the re-translation has a better understanding of some details of the article. The following is the translation content. Although the translation is very similar to a primary school student's composition, it is very easy to read. I hope you will give more valuable comments. Thank you.

Original article address:

Http://www.codeproject.com/KB/aspnet/BestPrctice1.aspx#As%20said%20before%20do%20not%20get%20carried%20away%20with%20execution%20time

 

 

Check for high memory usage in. Net code.

 

Introduction

Thank you very much, Mr. Peter sollich.

Use CLR profiler

CLR profiler Function

Do not use CLR profiler in the product or use it as a performance testing tool from the beginning

How to Use CLR profiler?

What problems may occur when using CLR profiler?

A simple example

Use CLR profiler to detect our example

Other simpler methods

Simplify the results using comments

Do not worry too much about execution time

Summary

 

Introduction

Memory consumption in. net is an important factor to reduce performance. Many developers only focus on the execution time, which causes performance bottlenecks for. net applications.
However, the performance issue cannot be clearly determined only by the execution time. Well, the most important question is to understand which function,
How much memory is consumed by the Assembly or class. In this article, we will know how to find out the memory occupied by the function. This topic mainly discusses
Use CLR profiler to learn the best practices for memory allocation.

 

Thank you very much, Mr. Peter Sollich.

Before starting this article, we would like to thank Peter Sollish, CLR performance architect, for writing such a detailed help document. After CLR profiler is installed
Please do not forget to read the Detailed Help document written by Peter Sollich.

Thank you very much. Please let me know if you have read this article.

Use CLR profiler

The CLR profiler tool is provided by Microsoft to detect how memory is allocated in your. net code. You can download it through the following link:
Http://www.microsoft.com/downloads/details.aspx? Familyid = A362781C-3870-43BE-8926-862B40AA0CD0 & displaylang = en

Note: CLR profiler has two versions:. net 1.1 and. net 2.0. For 2.0 CLR profiler, you can use
Http://www.microsoft.com/downloads/details.aspx? Familyid = A362781C-3870-43BE-8926-862B40AA0CD0 & displaylang = en download
Or through http://www.microsoft.com/downloads/details.aspx? Familyid = 86ce6052-d7f4-4aeb-9b7a-94635beebdda & displaylang = en # Overview download version 1.1

Download CLR profiler. After decompression, you can run the program in the Binaries folder.

If you download the CLR profiler of 2.0, it provides both x86 and x64 environments, so be sure to run the correct version.

 

CLR profiler Function

CLR profiler is the best tool for understanding how memory is allocated in. net applications. It has two main functions:
Provides a complete report on memory allocation for. net applications. So you can view the memory allocation reports for each type, function, and assembly.
Provides the time required to call a method.

 

Do not use CLR profiler in the product or use it as a performance testing tool from the beginning

CLR is a injection tool. In other words, it uses its own logic to dump the memory of each function/class/module in the application.
That is, it will interfere with the original program logic. For example, when an application calls function 1 and function 2,
CLR profiler injects heap data into the memory after each function is executed, for example:

In other words, you cannot use CLR profiler to view the running time of a program, because it actually slows down your application to 10 to 100 times.
You will get an incorrect result.

Because it is such an injection tool, it should not be used in the production environment.

First, do not use the CLR profiler tool to analyze your performance problems. It is used only when you find that a function or class has memory problems.
The most suitable method is to use the performance counter to find the method or the function takes a long time, and then use CLR profiler to view the memory usage.

 

How to Use CLR profiler?

 

Download CLR profiler from Microsoft and decompress the file. Open the binariesdirectory in the decompressed folder, find the desired folder, and run 'clrprofiler.exe '. You will see
Such as the interface.

 

First, select the program to be checked. Two things can be detected: one is the memory allocation, and the other is the number of times a method is called.
Select the content you want to check and click "start application ".

After the detection is complete, you can see an overview in the image below. It is a very complex report, and we will look at the simple process through a simple example later.

 

What problems may occur when using CLR profiler?

 

When running CLR profiler, we may encounter some problems. If you see the following interface and the program will not stop, it may be caused by the following two reasons:
You used. net 2.0 but ran CLR profiler 1.1.
You have not registered RrofilerOBJ. dll in GAC

 

A Simple Detection example

 

The example to be tested is very simple. Use a simple button to call two functions "UseSimpleStrings" and "UseStringBuilders ".
Both functions are used to connect strings. One uses the "+" number to connect, and the other uses the StringBuilder class. We cyclically connect 1000 times.

 

Private void usingsimplestrings ()
{
String strsimplestrings = "";
For (INT I = 0; I <1000; I ++)
{
Strsimplestrings = strsimplestrings + "test ";
}}

 

 

Use stringbuilder class connection

 

Private void usingstringbuilders ()
{
Stringbuilder strbuilder = new stringbuilder ();
For (INT I = 0; I <1000; I ++)
{
Strbuilder. append ("test ");
}
}

 

 

Both functions are called through the button time:

 

Private void btndoprofiling_click (Object sender, eventargs E)
{
Usingsimplestrings ();
Usingstringbuilders ();
}

 

 

 

Use CLR profiler to detect our example

 

Next we will use profiler to check the memory occupied by the function in our example. Click Start application ",
Select an application, click the button in the application, and close the application. A summary window is displayed.

Click the bar chart to view the memory allocation for each type. I know this is messy. So we don't care about it. O (partition _ partition) O

 

If you have no function, you can click "Allocation graph ". It will tell you how much memory each function has allocated.
However, this report is very complicated, because there are many functions in it, it is difficult for us to locate our two functions "usingstringbuilders" and "usingsimplestrings"

 

To simplify the image above, right-click some filters. We use "find Route" to filter unnecessary data. Event name of the input button
From this event, we can find the two called functions.

After searching, move the image to, as shown in. Double-click the highlighted "btndoprofiling_click" box.

After double-clicking, you can see the details shown in. Now it's better, but the second function is gone and only the "usesimplestrings" function is displayed.
This is because this report is rough, so select 0 (everything) in the "Detail" area to see all functions.

Now you can see other functions. What is 26bytes? It is only an extra operation on the string when the function is executed. So we can ignore it.
Let's take a look at "UseSimpleStrings" and "UseStringBuilders ". You can see that the simple string uses 3.8 M. StringBuilder only uses 26kb
Therefore, StringBuilder saves more memory than a simple string connection.

 

That was a tough way any easy way)

The above method is troublesome. If you have 1000 functions, you need to check the memory occupied by these functions. It is very impossible to look at every call graph and find out your function.

The best way is to export a detailed report to an excel file and analyze the data. Therefore, click "call tree" in "view"

Click "call tree" and the following dialog box is displayed. Click "all function" in the view ". Click Save in File to Save all functions as a CSV File.

After the export is successful, you can easily locate your method or function and view the allocated memory.

 

Simplify the results using comments

 

If you know which method you want to check, you can start profiler when calling the method. In other words, you can start CLR profiler in the application.
To enable profiler in C # code, you must first Add a reference to "CLRProfilerControl. dll. You can find the dll in the folder where the profiler program is located.
In this way, you can directly call profiler in your code, as shown in the following code segment. We can use profiler before calling two string connection functions.
Disable profiler after the function is called.

 

Private void btndoprofiling_click (Object sender, eventargs E)
{
Clrprofilercontrol. logwriteline ("entering loop ");
Clrprofilercontrol. allocationloggingactive = true;
Clrprofilercontrol. callloggingactive = true;

UsingSimpleStrings ();
UsingStringBuilders ();

CLRProfilerControl. AllocationLoggingActive = false;
CLRProfilerControl. CallLoggingActive = false;
CLRProfilerControl. LogWriteLine ("Exiting loop ");
CLRProfilerControl. DumpHeap ();
}

 

 

Now we use CLR profiler to start the program. Because we have enabled profiling in the code, we will remove the Profiling active option.

 

At this time, you can see that the bar chart data is relatively small, you will see that it only records the "System. String" and "System. Text. StringBuilder" two types of memory allocation.

 

However, when you see the distribution chart, you will find it very concise. Previously, the messy view disappeared and replaced it with a simple and centralized image. Remember to click "Everything" to view all functions.

 

Do not worry too much about execution time

 

On the Summary page, you can see a "comments" button. Click this button to view the start time and end time of the program. Please do not care about this time record, just like
As we mentioned earlier, an intrusive tool returns incorrect execution time results.

Entering loop (1.987 secs)
Exiting loop (2.022 secs)

Summary

CLR profiler can be used to view the memory allocation of functions, classes, and Assembly to evaluate program performance.
It should not be used in the production environment.
It should not be the preferred tool for performance detection. We should first select the performance counter to obtain the method for a long execution time, and then use CLR profiler to find out the real cause.
You can use the bar chart to view the memory allocation and the function call chart to view the memory occupied by the function.
If you know which function to detect, you can start profiler in the program

All in all, as a tool to view memory allocation, no other tool can match CLR profiler.

Http://www.codeproject.com/KB/aspnet/BestPrctice1.aspx#As%20said%20before%20do%20not%20get%20carried%20away%20with%20execution%20time

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.