Software QA FYI - SQAFYI

Using the Allocation Profiler to Detect Memory Leaks in .NET Applications

By: AutomatedQA

Symptoms As developers, we are often faced with addressing memory leaks in our applications. Despite the fact that the .NET Framework includes automatic memory management, a number of memory allocation issues will remain in your application unless you are careful to avoid them.

A number of instances exist wherein the Garbage Collector in .NET fails to free allocated resources and thus create potential memory leaks. As such, it is critical to understand how Garbage Collection works and how to analyze your code and uncover any problem areas therein.

The article contains a step-by-step analysis of a known memory leak found in Microsoft .NET Framework v. 1.0.3705 via the Allocation Profiler included in AutomatedQA's AQtime 4. The problem appears when you use standard NumericUpDown and DomainUpDown controls in an application. The core of the problem lies with the Garbage Collector's inability to free instances of these classes as well as objects linked to them (for instance a form that contains one of these controls). As you can imagine, a form with a large number of such controls may cause significant memory leaks in any real-world application.

How to Locate Memory Leaks Using the Allocation Profiler Creating a Test Application First, we are going to create a test application that will consist of two forms: Form1 and Form2. Form2, contains NumericUpDown and DomainUpDown controls and is created from the main form (Form 1) by pressing the Show Form button. We'll place two controls (of each type) on Form 2. The source code for both forms is listed below (Example 1 and 2):

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;

namespace WindowsApplication1 { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); }

protected override void Dispose(bool disposing) { if(disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); }

#region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(112, 96); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "Show Form2"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(304, 254); this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1}); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion

[STAThread] static void Main() { Application.Run(new Form1()); }

private void button1_Click(object sender, System.EventArgs e) { Form2 frm = new Form2(); frm.Show(); } } }

Full article...

Other Resource

... to read more articles, visit

Using the Allocation Profiler to Detect Memory Leaks in .NET Applications