Using Perfview to diagnose problems with. Net GC

Source: Internet
Author: User

PerfView Overview:

Perfview is a tool software that can help you analyze CPU and memory problems. It is very lightweight and does not invade the diagnostic procedure, which has little effect on the diagnostic procedure.

Visual Studio's own performance analysis capabilities are well-diagnosed in terms of CPU usage, time consumption, and memory allocation, but Perfview can provide richer diagnostic analysis information.

In this article, I will use Perfview to show you the following features:

    • The frequency of GC recovery and the time it takes to recycle;
    • Gets the cause of the large object assignment;
    • The memory is occupied by who;
    • Compares which managed object increases the fastest.

Test program

Now we're going to have a program that will lead to a memory leak, to make sure that using Perfview can achieve the effect we expect. It is a WinForm application with the following background code:

     Public Partial classForm1:form {Privatelist<int[]> arrays =Newlist<int[]>(); Random Random=NewRandom ();  PublicForm1 () {InitializeComponent (); Thread Thread=NewThread (Start); Thread. IsBackground=true; Thread.        Start (); }        Private voidStart (Objectobj) {             while(true)            {                int[] A =New int[Random. Next (90000,100000)]; Arrays.                Add (a); Thread.Sleep (Ten); }        }    }

Using Perfview for tracking

To open Perfview, you will see the following window:

Perfview's user manual is integrated in this program and you can access it from the menu bar.

Then click on the Menu "Collect-->collect" to collect data to analyze the resulting diagnostic results:

No need to modify any initialization configuration, click "Start Collection" button, Perfview will begin to collect all the process event data.

After a few 10 seconds, you can click on the "Stop Collection" button, Perfview will stop collecting and generate the diagnostic file "PerfViewData.etl.zip":

Get GC Stats

Double-clicking the "gcstats" report will pop up a window with each process GC information displayed in the window to find our test program.

For the test program we will get the following summary information table:

Further down, the reason for GC triggering is also shown:

As shown, the collection of this GC occurs because of the assignment of large object.

Gets the reason that caused the large object assignment

From the Perfview main interface, double-click to open the GC Heap Alloc Stacks window, and then double-click the process of the test program, and the window that pops up will display the stack information in the order of memory allocations from the largest to the smaller:

Perfview will classify all large object assignments under the Largeobject node, and double-click the node to see the following information:

Note: If you see "other<<clr?>>" in the interface shown, you can right-click on it and then click "Lookup Symbols" to get the functionality name of the CLR and windows.

The main columns in this section are described below:

inc%: Indicates the percentage of all records allocated by the allocated bytes of the object;

INC: The total number of bytes allocated for this object;

INC Ct: The number of times the object was allocated.

As can be seen, the large object is a Int32 array from the Start method, Perview accurately diagnose the effect we expect.

Who caused the memory leak

Perfview can view the path to the memory-consuming object through heap dump.

From the main interface, click the menu item "Memory-->tale Heap Snapshot", as shown in the popup window:

Find our test program and select it, then click on the "Dump GC Heap" button, then click the "CLose" button after a few seconds, and a ". Gcdump" file will be generated at the end.

Double-click to open the "windowsformsapplication1.gcdump" window, which appears as follows:

The prefview accurately diagnoses that the static variables occupies memory.

Use two heap dumps to see how the object's memory is changing

In the case where the application is running continuously, take the Heap sanpshot two times to ensure that the file names generated two times are inconsistent. Also open the windows of these two. gcdump files, which can be compared with the data of another window by using the Diff menu item function of either window.

Using Perfview to diagnose problems with. Net GC

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.