【发布时间】:2012-07-05 16:49:07
【问题描述】:
我写了一个非常简单的编译器,将我的源语言翻译成字节码,这段代码由虚拟机处理(作为一个简单的堆栈机器,所以 3 + 3 将被翻译成
push 3
push 3
add
现在我在垃圾收集方面很挣扎(我想使用引用计数)。 我知道它的基本概念,如果分配了引用,则该对象的引用计数器会增加,如果它离开范围,则会减少,但我不清楚的是 GC 如何释放获得的对象传递给函数...
这里有一些更具体的例子来说明我的意思
string a = "im a string" //ok, assignment, refcount + 1 at declare time and - 1 when it leaves scope
print(new Object()) //how is a parameter solved? is the reference incremented before calling the function?
string b = "a" + "b" + "c" //dont know how to solve this, because 2 strings get pushed, then concanated, then the last gets pushed and concanated again, but should the push operation increase the ref count too or what, and where to decrease them then?
如果有人能给我提供实现引用计数的教程的链接,或者如果有人以前遇到过这个问题,我会很高兴帮助我解决这个非常具体的问题(我的问题是我不明白何时增加、减少引用或在哪里计数已存储)
【问题讨论】:
标签: garbage-collection implementation reference-counting