如何销毁.Net中的对象 [英] How to destroy objects in .Net

查看:63
本文介绍了如何销毁.Net中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个奇怪的问题,但是我真的很高兴知道答案. 如何销毁.Net中的对象,并将内存分配回堆?

I know this is a strange question, but really I'm very excited to know the answer. How do you destroy Objects in .Net, and give memory back to the heap?

推荐答案

简短回答

System.GC.Collect(); 

应该强制运行垃圾回收,但我听说它更多是一个礼貌的询问功能,实际上可能没有运行它.

is suppose to force the garbage collection to run but I've heard that its more of a polite asking function that may not actually have it run.

长期回答

我相信C#使用垃圾回收,因此充其量您可以要求它进行清理,但实际上它是在自己的时间进行的.如果没有引用,则认为该对象已损坏.

I believe C# uses garbage collection, so at best you can ask it to clean up but it really does it on its own time. An object is considered destroyed when there are no references to it.

例如:

Object myobject = new Object();
Object sameobject = myobject;
myobject = null;
//Garbage collection will not clean it up because sameobject is still pointing to the memory location
sameobject = null;
//Garbage collection can now clean it up

这是有关垃圾收集的精彩文章. .NET使用

Here's a great article on the garbage collection .NET uses

这篇关于如何销毁.Net中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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