没有引用的对象死亡 [英] Object Death without a Reference

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

问题描述

这对你们来说是一个简单的人(对我来说不是那么简单!):

如果在另一个类中的方法中实例化了一个类,但该方法未返回引用,则对象会死吗?换句话说,对象是否在循环结束时提交以进行垃圾回收?

我将一如既往地感谢您的帮助.

Here is a simple one for you guys (well not so simple for me!):

If an class is instantiated in a method within another class but that method does not return a reference, does the object die? In other words, is the object submitted for garbage collection at the close of the loop?

I appreciate the help, as always.

推荐答案

目前暂时不会真正将其提交给垃圾回收.
每当收集器下次运行时,可行进行垃圾回收.
It would not really be submitted to garbage collection right at this time.
It would be viable for garbage collection whenever the collector runs the next time.


GC的想法是:不允许程序引用已构造对象,永远.

当该程序可访问的所有对象引用都丢失时,该对象将成为垃圾回收的主体.但是,它不会立即发生.它由GC根据自己的策略决定".这就是为什么代码不能依赖构造函数调用的顺序的原因.甚至编写构造函数也应该很少见.

人们可能会想到这种机制更狡猾.例如,解决了孤岛"问题.例如,对象A引用对象B,对象B引用对象C,对象C引用A.引用不会丢失,但是如果在给定的Application Domain中运行的代码都无法获取对A,B或C中任何一个的引用,则它们将被销毁.并反正收集了.

—SA
The idea of GC is this: not to allow the program to reference a destucted object, ever.

When all the object references accessible to the program are lost, this object is a subject to Garbage Collection. However, it does not happen immediately. It is "decided" by GC according to its own strategy. That''s why the code cannot rely on the order of constructor calls. Even writing constructors should be quite rare.

The mechanism is more cunning that one may think. For example, the problem of "islands" is resolved. For example, object A references object B, object B references object C and object C references A. References are not lost, but if no code running in the given Application Domain can get reference to any of A, B or C, they are destructed and collected anyway.

—SA


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

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