我尝试使用 Ants Memory Profiler 来掌握。我遇到的问题是我没有一个简单的应用程序存在内存泄漏。
我使用了 Redgate (QueryBee) 的样本,但这是为了我的口味而设计的。必须有一个更简单的应用程序。
我试图弥补一个,但它不工作。它不起作用意味着:我没有内存泄漏。我读到了调用 ShowDialog 而不处理被调用的表单会让我发生内存泄漏,但这里不是这种情况。
我使用 VS2010 和 .NET 4.0
我对非常常见的问题特别感兴趣。
这是我到目前为止所拥有的。你能给我一个内存泄漏吗?:
主窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MemoryLeakTest
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SubForm formToCall = new SubForm();
formToCall.ShowDialog();
}
}
}
子表单
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace MemoryLeakTest
{
public partial class SubForm : Form
{
public SubForm()
{
InitializeComponent();
}
private void SubForm_Load(object sender, EventArgs e)
{
ArrayList persons = new ArrayList();
for (int i = 0; i <= 50000; i++)
{
var person = new {
Name = "1 SchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorsch",
LastName = "KluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluni",
Age = 50,
City = "LondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondon",
Zip = "223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301",
Index = i
};
persons.Add(person);
}
dataGridView1.DataSource = persons;
}
private void SubForm_FormClosed(object sender, FormClosedEventArgs e)
{
//this.Dispose();
}
}
}