我可以在 C# 中强制清理内存吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1852929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I force memory cleanup in C#?
提问by jim
I heard that C# does not free memory right away even if you are done with it. Can I force C# to free memory?
我听说 C# 不会立即释放内存,即使你已经完成了它。我可以强制 C# 释放内存吗?
I am using Visual Studio 2008 Express. Does that matter?
我正在使用 Visual Studio 2008 Express。那有关系吗?
P.S. I am not having problems with C# and how it manages memory. I'm just curious.
PS 我对 C# 以及它如何管理内存没有问题。我只是好奇。
采纳答案by Jason D
Jim,
吉姆,
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();
I strongly recommend you read this MSDN article on garbage collection.
我强烈建议您阅读这篇关于垃圾收集的 MSDN 文章。
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
编辑 1:“Force”用引号引起来。像另一张海报一样更清楚,这实际上只是暗示了它。你不能真正让它在特定的时间点发生。因此,链接到有关 .Net 中垃圾收集的文章
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.
编辑 2:我意识到这里的每个人都只直接回答了您的主要问题。至于你的次要问题。使用 Visual Studio 2008 Express 仍将使用 .net 框架,这是执行垃圾收集的工具。因此,如果您升级到专业版,您仍将拥有相同的内存管理功能/限制。
Edit 3: This wikipedia aritcles on finalizersgives 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 usingstatement. 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...
编辑 3:关于终结器的这篇维基百科文章给出了一些关于终结器中适合做什么的好指示。基本上,如果您正在创建一个可以访问关键资源的对象,或者您正在使用这样的对象,请实现 IDispose 和/或利用using语句。即使抛出异常,Using 也会自动调用 Dispose 方法。这并不意味着你不需要给运行终结器提示......
回答by Johannes Rudolph
回答by ChrisF
You can't forceC# to free memory, but you can requestthat the CLR deallocates unreferenced objects by calling
您不能强制C# 释放内存,但可以通过调用请求CLR 释放未引用的对象
System.GC.Collect();
There is a method WaitForPendingFinalizers
that will "suspend the current thread until the thread that is processing the queue of finalizers has emptied that queue." Though you shouldn't need to call it.
有一种方法WaitForPendingFinalizers
可以“挂起当前线程,直到正在处理终结器队列的线程清空该队列”。虽然你不应该需要调用它。
As the others have suggested head over to the MSDNfor more information.
正如其他人所建议的那样,请前往MSDN获取更多信息。
回答by mrydengren
You might want to have a look at this screencast^from InfoQ.com. It presents the internals of the .NET Garbage Collector
.
你可能想看看这个来自 InfoQ.com 的截屏视频^。它展示了.NET Garbage Collector
.
回答by Anjan Kant
You can clean up your memory by given both methods:
您可以通过两种方法清理内存:
GC.Collect();
GC.WaitForPendingFinalizers();
Read both Reference GC.Collect()and GC.WaitForPendingFinalizers()