清理变量的方法 [英] Cleaning up variables in methods

查看:228
本文介绍了清理变量的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的软件,我的工作一些内存的问题,我希望通过我们写在那里我们在方法的开头声明变量的方法,但是他们没有被清理(丧失或处置)在方法的结尾。

I am having some memory issues in a large piece of software I work on, I am looking through methods we have written where we are declaring variables at the beginning of the method, but they are not being cleaned (nullified or disposed) at the end of the method.

public static bool CheckIsNumber(string x)
{
    int y;
    return(int.TryParse(x, out y));
}



如您在上面看到,永远不会设置为空或处理之类的东西,我一直以为它会被GC回升,而是通过一个内存分析器看后,我不那么肯定了。

as you see above, y is never set to null or disposed or anything like that, I always thought it would be picked up by the GC, but after looking through a memory profiler, I am not so sure anymore.

推荐答案

有一个变量在.NET中走出去的范围和垃圾收集之间的区别。微软列出了三个条件,将触发垃圾收集:

There's a distinction between a variable going out of scope and garbage collection in .NET. Microsoft lists three conditions that will trigger garbage collection:


  1. 该系统具有较低的物理内存

  2. 所使用的分配对象在托管堆中的内存超过了可接受的阈值。

  3. 的GC.Collect的方法被调用。

来源:的 http://msdn.microsoft.com/en-us/library/ee787088(v = vs.110)的.aspx#conditions_for_a_garbage_collection

当我'已经看了看我的.NET应用程序的内存使用情况,他们一般稳步上升到一定程度之后,垃圾收集器收集和内存使用量再次下降。尽我可以告诉大家,这正是它的意思是如何工作的。

When I've looked at memory usage of my .NET apps, they generally rise steadily to a certain point after which the Garbage Collector collects and the memory usage drops again. The best I can tell that's exactly how it's meant to work.

这篇关于清理变量的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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