我可以强制内存清理在C#中? [英] Can I force memory cleanup in C#?

查看:169
本文介绍了我可以强制内存清理在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说C#不释放内存,即使你用它做的时候了。我可以强制C#来释放内存?

I heard that C# does not free memory right away even if you are done with it. Can I force C# to free memory?

我使用的Visual Studio 2008 Express的。请问是怎么回事?

I am using Visual Studio 2008 Express. Does that matter?

P.S。我不与C#和如何管理内存的问题。我只是好奇。

P.S. I am not having problems with C# and how it manages memory. I'm just curious.

推荐答案

吉姆,

您听说过正确。它会定期通过一个称为垃圾收集机制清理内存。你可以迫使垃圾收集通过类似下面的一个电话

You heard correctly. It cleans up memory periodically through a mechanism called a Garbage Collector. You can "force" garbage collection through a call like the one below.

GC.Collect();



我强烈建议你阅读的在垃圾收集这MSDN文章

编辑1:强制在引号。要作为另一个海报效果更加清晰,这真的只是表明它。你真的不能让它发生在特定的时间点。因此,在.NET中的文章链接垃圾收集

EDIT 1: "Force" was in quotes. To be more clear as another poster was, this really only suggests it. You can't really make it happen at a specific point in time. Hence the link to the article on garbage collection in .Net

编辑2:我发现每个人都在这里只提供了一个直接回答你的主要问题。至于你的问题次要。使用Visual Studio 2008 Express将仍然使用.NET Framework,这是执行垃圾收集。所以,如果你曾经升级到专业版,你仍然有相同的内存管理能力/局限

EDIT 2: I realized everyone here only provided a direct answer to your main question. As for your secondary question. Using Visual Studio 2008 Express will still use the .net framework, which is what performs the garbage collection. So if you ever upgrade to the professional edition, you'll still have the same memory management capabilities/limitations.

编辑3:的This维基百科aritcles上终结给出了什么是适当的终结做一些很好的指针。基本上,如果你正在创建一个能够访问关键资源,或者你就吃下这样一个对象,实施IDispose和/或坐的使用的声明优势的对象。使用会自动调用Dispose方法,即使抛出异常。这并不意味着你不需要给运行终结提示...

Edit 3: This wikipedia aritcles on finalizers gives some good pointers of what is appropriate to do in a finalizer. Basically if you're creating an object that has access to critical resources, or if you're consuming such an object, implement IDispose and/or take advantage of the using statement. Using will automatically call the Dispose method, even when exceptions are thrown. That doesn't mean that you don't need to give the run finalizers hint...

这篇关于我可以强制内存清理在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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