Lambda和内存泄漏:寻找替代方法 [英] Lambda and memory leaks: Looking for alternative approaches

查看:2935
本文介绍了Lambda和内存泄漏:寻找替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:

如果以验证这种事情的能力有经验的程序员向我展示了证明这一点,我将不胜感激方法是内存泄漏的安全。我已经把它引入到我的很多编码工作,但我还是在我的脑海一个小疑问。不幸的是我不够好/不知道这个工具来进行调查。

I would be greatful if an experienced programmer with the ability to verify this sort of thing showed me the proof that this method is safe from memory leaks. I've been introducing it to many of my coding efforts but I still have a small doubt in my mind. Unfortunately I'm not good enough / don't know the tools to investigate it.

原文:

我最近,lambda表达式的一些用法可以创建内存泄漏了解到:

I learned recently that some uses of lambda expressions can create memory leaks:

    ProjectData Project;

    void OnLaunchNewProject()
    {
        NewProjectUI newProjectUI = new NewProjectUI();
        newProjectUI.OnCompleted += (o, e) =>
            {
                Project = newProjectUI.NewProject;
                view.Content = string.Format("Project {0} was created and saved successfully.", Project.Name);
            };
        newProjectUI.OnCancelled += (o, e) => { view.Content = "Operation was cancelled.";};
        view.Content = newProjectUI;
    }



我学会了这种方法在这的博客

我不完全了解的lambda表达式中引用的局部变量的影响,这限制了我的规避问题的能力。

I do not fully understand the impact of referencing local variables in lambda expressions and this limits my ability to circumvent the problem.

之间的的典型的做法和使用拉姆达的,什么是理想的妥协?我喜欢拉姆达的事情是跳过的事件处理程序的参数定义的在我的课(发件人/路由参数)时,我不需要他们。

Between the typical approach and the use of lambda, what's the ideal compromise? The thing I like about lambda is skipping the definition of the EventHandler's arguments in the body of my class (the sender/routed args) when I don't need them.

推荐答案

不幸的是,你提到的博客文章是错误的。没有与lambda表达式的内存泄露一般性问题。在博客的例子中,终结器永远不会被调用,因为笔者绝不会取消该事件的匿名方法。所以.NET运行时认为,该方法还可能会被后来称为的,不能从内存中删除类。

Unfortunately, the blog post you mentioned is wrong. There is not general issue with memory leaks in lambda expressions. In the blog examples, the finalizers are never called because the author never removes the anonymous methods from the event. So the .NET runtime thinks that the method still might be called later and cannot remove the class from memory.

在你的代码,你会某处释放NewProjectUI实例,这是在所有的事件成为unsued当分配的lambda方法也成为未使用的地步。然后在GC可以删除匿名的lambda辅助类和释放内存使用

In your code, you will release the NewProjectUI instance somewhere, and this is the point where all events become unsued and when the assigned lambda method also becomes unused. Then the GC can remove the anonymous lambda-helper-class and free the used memory.

所以,再次:有是在.NET中没有问题使用的lambda局部变量时,表达式。

So, again: There is no issue in .NET when using local variables in lambda expressions.

但是,为了使你的代码更优秀,代码从lambda表达式移动到指定的方法和这些方法添加到事件中。这也使得有可能从事件时,他们不再需要去除方法

But to make your code better, move the code from the lambda expressions to named methods and add these methods to the events. This also makes it possible to remove the methods from the events when they're no longer needed.

这篇关于Lambda和内存泄漏:寻找替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆