清除未使用的对象 [英] Clearing out unused objects

查看:53
本文介绍了清除未使用的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速检查-当我想要摆脱一个对象时,我可以将其分配为null并自动进行垃圾收集吗,还是需要做其他事情?例如:

Quick check - when I want to get rid of an object, can I just assign it to null and it'll get garbage-collected automatically, or is there something else that needs to be done? For example:


            List<RandomClass> TestList = new List<RandomClass>();

            for (int x = 0; x<500000;x++)
                TestList.Add(new RandomClass());

            TestList = null;

这样简单就能清除TestList使用的所有资源吗?


Will simply doing this clear out any and all resources TestList was using?


C#新手,随时随地学习.我可能会问很多跟进的问题,关于已经给出的任何答案,如此合理的警告以及所有.

C# newbie, learning on the go. I will probably ask a lot of followup questions about any answers already given, so fair warning and all.

推荐答案

该对象超出范围,因此,只要不是全局对象,您就不必担心它.
The garbage collector will remove any object when the object goes out of scope, so you don't really need to worry about it, so long as an object that's not global.


这篇关于清除未使用的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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