如何获得所有创建的对象 [英] how to get all created objects

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

问题描述

大家好,
创建应用程序后如何在应用程序中获取所有创建的对象?.....
不是:我不知道所创建对象的名称,问题是我需要创建的所有对象

Hi every on
How can i get all created objects in my application after i create it ?.....
not: i do''t know the names of created objects, the thing is i need for all objects i was created

推荐答案

没有这样的东西.当您脱离某个上下文,从而失去对某些参考对象的引用时,这些对象在逻辑上被视为不存在. .NET使用垃圾收集(托管)内存,因此将安排销毁这些对象,并最终回收它们分配的内存.这称为可达性,这是一个很简单的概念.一旦达到对象,就可以防止该对象被破坏.它不必具有名称.一个对象可以作为数组,集合或任何其他容器的成员到达.作为开发人员,您是负责保留引用的人.

请参阅 http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Reachability_of_an_object [ ^ ].

可以使用弱引用引用对象,该引用不会阻止垃圾收集器(GC)收集没有常规(非弱)引用的引用对象.参见:
http://en.wikipedia.org/wiki/Weak_reference [ http://msdn.microsoft.com/en-us/library/system.weakreference.aspx [ ^ ].

顺便说一句,在没有垃圾回收的系统中,也无法访问无法从应用程序代码访问的堆对象.在这样的系统中,仅仅是导致内存泄漏.开发人员负责跟踪在堆上创建的所有对象.例如,在C ++中,可以重写"new"运算符并收集指向已分配内存的指针,但是通常对象的删除是在某个作用域的退出处完成的.

如果要进行软件开发,您确实需要了解这些内容.

—SA
There is no such thing. When you go out of some context so you loose the references to some reference objects, the objects are considered as logically nonexistent. As .NET uses garbage-collected (managed) memory, these objects will be scheduled for destruction and their allocated memory will be eventually reclaimed. This is called reachability, which is a non-trivial concept. The object is protected from destruction as soon as it is reachable. It does not have to have a name. An object can be reachable as a member of array, collection or any other container. You are the one who is responsible for keeping the references, as a developer.

Please see http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Reachability_of_an_object[^].

An object can be referenced using a weak reference which does not prevent a garbage collector (GC) to collect referenced objects having no regular (non-weak) references. See:
http://en.wikipedia.org/wiki/Weak_reference[^].
http://msdn.microsoft.com/en-us/library/system.weakreference.aspx[^].

By the way, in the systems without garbage collection, there is also no access the heap objects not reachable from the application code. In such systems is simply leads to memory leaks. The developer is responsible for keeping track of all objects created on heap. For example, in C++ one can override the "new" operator and collect pointers to the allocated memory, but usually the deletion of objects is done at the exit from some scope.

You really need to understand such things if you want to do software development.

—SA


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

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