谁在C#中调用gc.collect()?它是CLR吗? [英] Who calls gc.collect() in C#? Is it a CLR?

查看:136
本文介绍了谁在C#中调用gc.collect()?它是CLR吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Who calls GC.Collect() in C#? is it a CLR?





我尝试过的事情:



我试图找到GC.Collect()的工作但我找不到任何好文章。请帮助理解GC的调用者.Collect()



What I have tried:

I have tried to find the working of GC.Collect() but I didnt find any good article. Please help to understand the caller of the GC.Collect()

推荐答案

引用:

谁在C#中调用GC.Collect()?它是CLR吗?

Who calls GC.Collect() in C#? is it a CLR?



我(你的代码)调用 GC.Collect(),我相信。



GC.Collect Method (系统) [ ^ ]

何时调用GC.Collect() - Rico Mariani的Performance Tidbits [ ^ ]


It's you (your code) who calls GC.Collect(), I belive.

GC.Collect Method (System)[^]
When to call GC.Collect() – Rico Mariani's Performance Tidbits[^]


实际上,你这样做:它刚刚在幕后完成。

创建类的新实例时,需要在堆上分配空间。分配系统寻找一个合适的大块内存来使用,如果没有找到,它会调用垃圾收集器尝试释放一些。

如果你考虑一下,这是唯一合理的时间 - 你需要记忆,所以必须花时间才能找到它!



这就是为什么使用Dispose非常重要(或使用块)以确保释放宝贵的资源:如果您在完成文件流时没有处置文件流,那么文件将一直保持打开状态应用程序关闭或GC被内存耗尽而调用 - 这通常是人们为什么会出现文件使用中错误并且无法解决使用它的原因!
In effect, you do: it's just done behind the scenes.
When you create a new instance of a class, the space needs to be allocated on the heap. The allocation system looks for a suitable "chunk" of memory to use, and if it doesn't find one, it calls the garbage collector to try and free some up.
If you think about it, it's the only sensible time to do it - you need the memory, so that's when the time must be taken to find it!

That's why it's very important to use Dispose (or a using block) to ensure that precious resources are released: if you don't Dispose of a file stream when you are finished with it, then the file remains open until the app closes or the GC is called by you running out of memory - this is very often why people get "file in use" errors and can't work out what is using it!


这篇关于谁在C#中调用gc.collect()?它是CLR吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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