winform应用程序中常见的内存泄漏问题有哪些?

发布于 2024-12-08 23:58:20 字数 2426 浏览 0 评论 0原文

我尝试着掌握 Ants Memory Profiler。我遇到的问题是我没有一个存在内存泄漏的简单应用程序。

我使用了 Redgate (QueryBee) 的示例,但它是根据我的口味设计的。必须有一个更简单的应用程序来实现这一点。

我试图弥补一个,但它不起作用。它不起作用意味着:我没有内存泄漏。我读到有关调用 ShowDialog 而不处理被调用的表单会导致内存泄漏,但这里的情况并非如此。

我使用 VS2010 和 .NET 4.0

我对非常常见的问题特别感兴趣。

这是我到目前为止所拥有的。你能给我一个内存泄漏吗?:

MainForm

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();
        }
    }
}

Subform

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();
        }


    }
}

I try to get to grasp with Ants Memory Profiler. The problem I have is that I dont have an easy application that has a memory leak.

I used the sample of Redgate (QueryBee), but it was to contreived for my taste. There has to be an easier app for that.

I tried to make one up but it is not working. It is not working means: I dont have a memory leak. I read about calling ShowDialog without disposing the called form would get me a memory leak, but thats not the case here.

I use VS2010 and .NET 4.0

I am especially interested in issues that are very common.

Here is what I have so far.Can you get me a memory leak?:

MainForm

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();
        }
    }
}

Subform

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();
        }


    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕梦 2024-12-15 23:58:20

将其添加到您的 SubForm

public void mouse_handler(object sender, MouseEventArgs e)
{

}

并更改 MainForm 来执行此操作

private void button1_Click(object sender, EventArgs e)
{
     SubForm formToCall = new SubForm();
     this.MouseClick += new MouseEventHandler(formToCall.mouse_handler);
     formToCall.ShowDialog();
}

现在,无论您是否 .Dispose() SubForm 都没关系,您仍然会遇到“泄漏”。您的 MainForm 通过其鼠标事件处理程序无限期地保留对子表单的引用,该处理程序基本上只是接收事件的人员的列表,因为该处理程序永远不会取消注册。

蚂蚁将帮助您追踪这一点,但需要手动进行,它会向您显示仍然存在的对象并从根引用,并且您必须发现这些对象不应在任何地方引用。我相信蚂蚁也可以生成警告等。当它发现已被 .Dispose() 但仍在某处引用的对象时。

Add this to your SubForm

public void mouse_handler(object sender, MouseEventArgs e)
{

}

And change the MainForm to do this

private void button1_Click(object sender, EventArgs e)
{
     SubForm formToCall = new SubForm();
     this.MouseClick += new MouseEventHandler(formToCall.mouse_handler);
     formToCall.ShowDialog();
}

Now it doesn't matter if you .Dispose() the SubForm or not, you will still have a "leak". Your MainForm keeps a reference to the SubForms indefinitely, through its mouse event handler, which is basically just a list of who is to receive the events, since that handler is never de-registered.

Ants will help you track down this, but rather manually, it'll show you objects still being alive and referenced from the root, and you have to discover that these objects should not referenced anywhere. I believe Ants also can generate warnings/etc. when it finds objects that have been .Disposed(), but are still referenced somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文