为什么这个 GC 系统不好?
我想到了一个我看不出有什么重大缺陷的 GC,我想知道为什么它没有被更广泛地使用,或者为什么我没有听说过它的使用。
该系统是:
- 所有对象都附有一个 4 字节 unsigned int 计数器。 (可能是 2 个字节,我不知道。
- 每当构造一个对象时,它的计数器就会从 1 开始。
- 当一个对象作为参数发送给函数时,它的计数器就会递增。
- 当一个对象到达它所在的点时,它 的计数器就会递增。不再在函数中使用(可能是作用域的最末端),
- 当对象的计数器达到零时,它被删除,因为在代码中没有引用它的
边缘情况 。计数器出现故障?有哪些缺点和优点?
提前感谢您的帮助。
I have thought of a GC which I can't see major flaws with, and I am wondering why it isn't used more prevalently, or why I haven't heard of its use.
The system is:
- All objects have a 4 byte unsigned int counter attached to them. (Could be 2 bytes, I don't know.
- Whenever an object is constructed, its counter is started at 1.
- When an object is sent to a function as a parameter, its counter is incremented.
- When an object reaches the point where it is no longer used within a function (Could be the very end of scope) its counter is decremented.
- When the object's counter reaches zero, it is deleted, as at no position in code is it referenced.
Is there a fringe case in which the counter becomes faulty? What are the disadvantages, and the advantages?
Thank you in advance for your assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它被称为引用计数。我建议阅读维基百科文章,因为它涵盖了优点和缺点。
Its called reference counting. I would suggest reading the wikipedia article as it covers the advantages and disadvantages.