[英]How to build a Garbage Collector?
I'm working in Flash, and attempting to use the new "domain memory" available in Flash Player. 我正在Flash中工作,并尝试使用Flash Player中可用的新“域内存”。 This essentially lets you work with memory at a low level, but you have to manage the memory yourself, much like C++, which has no built-in garbage collector.
从本质上讲,这使您可以在较低级别上使用内存,但是您必须自己管理内存,就像C ++一样,后者没有内置的垃圾收集器。 I've built a basic allocator/deallocator, but I need some way to build a Garbage Collector or Reference Counter so I can unallocate unused objects.
我已经构建了基本的分配器/解除分配器,但是我需要某种方式来构建垃圾收集器或引用计数器,以便可以取消分配未使用的对象。 Take the following example:
请看以下示例:
Rect stageRect = new Rect(0, 0, stage.width, stage.height);
// syntax is for understanding only
// actually would allocate memory using my handwritten allocator
I've constructed a new Rect
and stored in a class member var. 我构造了一个新的
Rect
并存储在一个类成员var中。 Now lets say I perform some rectangle math on this object, creating 2 more objects. 现在说我对这个对象执行一些矩形数学运算,再创建两个对象。
Rect quarterRect = stageRect.halfWidth().halfHeight();
As you can see, the Rect
returned by halfWidth
is unused, and can be garbage collected. 如您所见,
halfWidth
返回的Rect
尚未使用,可以进行垃圾回收。
The final rect created by halfHeight
is stored in the var quarterRect
, which I need for later. 由
halfHeight
创建的最终rect存储在var quarterRect
,稍后我需要。
How do I detect such unused objects, and dispose of them accordingly? 如何检测未使用的对象并进行相应处理? I've been reading up on Reference Counting, Smart Pointers , the GC for C++ , but I still can't figure out how to detect when a reference is unused, to decrement the reference count.
我一直在阅读“ 引用计数”,“智能指针 ” 和C ++的GC ,但是我仍然不知道如何检测何时未使用引用以减少引用计数。 Incrementing the ref count is easy : when you set another var to point to this object, ie:
a = stageRect
, should increase the reference count of stageRect
, but how would you know when a
is unused? 递增引用计数很简单:当您设置的其它变种指向这个对象,即:
a = stageRect
,应增加的引用计数stageRect
,但是你怎么知道什么时候a
是未使用的? to decrement the reference count? 减少参考计数? Usually you don't go around setting
a = null
in modern code. 通常,您不需要在现代代码中设置
a = null
。 You just expect the platform to detect its an unused ref and dispose it. 您只希望平台检测到未使用的引用并进行处理。
Well, let's consider this code: 好吧,让我们考虑下面的代码:
int someFunction() { // I have no clue about AS3 syntax, but I suppose it's C-like, right?
Rect a = new Rect(...); // there are no pointers, only references, right?
// ... some other stuff
} // <- what happens here?
What happens at the closing curly bracket? 在大括号附近会发生什么? The
a
variable goes out of the scope. a
变量超出范围。 In C++, when a variable goes out of scope, its destructor is called. 在C ++中,当变量超出范围时,将调用其析构函数。 What happens in AS3 when the variable goes out of the scope?
当变量超出范围时,在AS3中会发生什么? If nothing happens here, nothing that you can trace programmatically... well, then I am afraid that implementing reference-counting is impossible.
如果这里什么也没有发生,那么您就无法以编程方式进行跟踪……好吧,那么恐怕无法实现引用计数。 For reference-counting, you need a way to tell that a reference to your value has disappeared.
对于引用计数,您需要一种方法来告知对您的值的引用已消失。
But is AS3 really have no GC? 但是AS3真的没有GC吗? I can't believe in a scripting languages without GC.
没有GC我无法相信脚本语言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.