有没有可能的方法来启动“毁灭"? C#中的对象? [英] Is there any possible way to initiate "destroy" object in C#?

查看:106
本文介绍了有没有可能的方法来启动“毁灭"? C#中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都喜欢C#的垃圾收集器.我知道,它非常适合99.999%的应用程序...

但是我在想,是否有可能像在C ++中那样以任何方式手动销毁C#中的对象.我的意思是,它的内存将被释放,(当然,只要没有引用它),就好像GC已收集到该对象一样.

解决方案

这是一个好问题!但是,恐怕没有直接的方法.

从理论上讲,GC使用 Win32 VirtualAlloc 函数保留一段内存,然后释放该段内存通过清除 Win32 VirtualFree 函数将其安装到操作系统中(清除它们的所有对象之后).因此,如果您想继续尝试自己管理细分,则必须付出很多努力,我敢打赌它将超过您整个项目的开发时间...

据我所知,所有内部管理操作都是通过垃圾收集器进行的.因此,想到的最接近的事情是调用Collect方法.请参阅 GC.Collect方法 [
解决方案

It''s a good question! However I''am afraid there is no straightforward methodology.

Theoretically, the GC uses the
Win32 VirtualAlloc function to reserves a segment of memory, and releases segments back to the operating system (after clearing them of any objects) by calling the Win32 VirtualFree function. So if you want to go ahead and try to manage the segments yourself, you''ll have to put so much effort in it, that I''ll bet it will surpass the dev time of your whole project...


As far as I know all housekeeping operations go via garbage collector. So the closest thing that comes to mind is to call the Collect method. See GC.Collect Method[^]

However, it should be needed in very rare occasions since the framework tries to keep enough free memory available.


There is a way to destroy the object in memory as follow.
1. GC.Collect.
2. IDisposable interface.

For memory reclaim we can use both of this but when we use GC.collect in c# application is called bad way implementation

IDisposable is a good way to destroy the object in memory.


这篇关于有没有可能的方法来启动“毁灭"? C#中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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