Use ClrProfiler to debug memory-string operations

Source: Internet
Author: User
ClrProfiler is an open-source tool developed by Microsoft for developers to view and optimize the. net underlying memory allocation.

This article uses several examples to illustrate some problems in development. First, let's take a look at the configuration options:

View

Description

Histogram Allocated Types

This gives you a high-level view that specifies the object type (allocation size) that is allocated within the lifecycle of your application. This view also displays the objects allocated to the large object heap (the object exceeds 85KB.

This view allows you to click each part of the icon to see which methods are assigned with which objects.

Histogram Relocated Types

Displays the objects that have been allocated (locations) by GC (re-) because they survive after a GC (that is, they are also referenced by others)

Objects By Address

Provides an image of heap hosting at a specific time (at a given time)

Allocation Graph

Shows how the called stacks are separated by graphs. You can use the following views:

* View the cost of each alloction)

* Isolate the unexpected allocation

* Find out the possible method for Excessive allocation

Assemby, Module, Function, and Class Graph

There are four very similar views. They allow you to see which methods involve the Assembly, class, module, and method.

Heap Graph

Displays all objects on the managed stack and their associated information.

Call Graph

This allows you to see which methods are called and how often they are called.

You can use this chart to explore the call costs of the class library and determine the demand for calling this method and which methods are called.

Time Line

Display the redistribution of GC in your application (that is, the objects that are clearly identified ). Use this view:

* Study GC Behavior

* Determine the number of GC occurrences (0, 1, 2) and occurrence frequency in the three generations.

* Determine which objects survive after GC and are promoted to the next generation.

You can select the time point or interval (intervals) and right-click it to display the memory allocated in the interval period.

Call Tree View

Provides a view of the execution of your application, which is based on text by age. You can use this view:

* View the allocated type and their size.

* Check which assemblies are loaded due to method calls.

* Use of finalizer and their execution count

* Where the indenied method does not use "close" or "dispose", leading to bottlenecks.

* Unexpected distribution of analysis.

1. Who is faster in string and StringBuilder?

First, establish a test environment and perform 100 million connections to strings.

Using System;

Using System. Collections. Generic;

Using System. Text;

 

Namespace StringTest

{

/// <Summary>

/// Test String

/// </Summary>

Class Program

{

Static void Main (string [] args)

{

String str = string. Empty;
For (int k = 0; k <10000; k ++)

Str + = k. ToString ();

}

}

}

 

 

 

A total of about 119 MB of memory is allocated, and o-generation recovery is performed. Next, let's look at the memory allocation of strings,
Click Allocation Graph in, for example:

 
We can see that about MB of memory is used in ConCat.

Let's take a look at StringBuilder's 10000 series connections.

 
The total allocated memory is 540,146 Final Heap Type is also 540,146, which means that the same object is not recycled by GC.

 

Then,

Conclusion: Through ClrProfiler, we can clearly obtain the differences between the two from the experiment.

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.