# Still trying to grok GC & continuations

**URL:** <https://www.ruby-forum.com/t/still-trying-to-grok-gc-continuations/70065>\
**Category:** Ruby\
**Created:** [February 18, 2006, 12:26pm UTC](https://www.ruby-forum.com/t/still-trying-to-grok-gc-continuations/70065 "2006-02-18T12:26:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christoffer\_LernSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@Christoffer\_LernSSS](https://www.ruby-forum.com/u/Christoffer_LernSSS)\
**Post date:** [February 18, 2006, 12:26pm UTC](https://www.ruby-forum.com/t/still-trying-to-grok-gc-continuations/70065/1 "2006-02-18T12:26:54Z")

</div>

Hello continuation gurus and others!

I still don’t understand why I keep having so many non-gc:ed  
continuations in my code.

Below is some sample code where each coroutine seem intent on hogging  
3 continuations each. I don’t see why I shouldn’t be able to cut that  
number down to a single continuation each.

Cutting and pasting the code below and running it should give a  
printout of

## continuations------------------------------------- wait\<3\> wait\<2\> exit **exit init init Currently using: 6 continuations**

I would prefer to only keep the first 2, but I can’t seem to make  
that happen. _sigh_

/Christoffer

## 8\<----------------------------- ugly test code starts here

class StateLess  
end  
class Continuation  
attr\_accessor :tag  
def to\_s  
“#{tag}”  
end  
end

class TestStateLess \< StateLess  
attr\_reader :value  
def initialize  
@value = “”  
callcc do |continuation|  
continuation.tag = “init”  
@continuation = continuation  
return  
end  
run  
end

def run(\*arg)  
@value += “retry;” while wait(1) !~ /password/  
@value += “passed;”  
wait(2)  
@value +=“passed2;”  
wait(3)  
end

def wait(position)  
@value += “waiting\_at\_#{position};”  
@continuation = nil  
callcc do |continuation|  
continuation.tag = “wait\<#{position}\>”  
@continuation = continuation  
@exit.call  
end  
end

def go(\*arg)  
callcc do |exit|  
exit.tag = “exit\<#{arg}\>”  
@exit = exit  
@continuation.call(\*arg)  
end  
@exit = nil  
end

end  
include ObjectSpace  
def count\_cc  
garbage\_collect  
i = 0  
puts “continuations-------------------------------------”  
ObjectSpace.each\_object(Continuation) {|x| puts x; i += 1 }  
puts “Currently using: " + i.to\_s + " continuations”  
puts “--------------------------------------------------”  
end

require ‘test/unit’  
class Tests \< Test::Unit::TestCase  
def test\_twin  
a = TestStateLess.new  
b = TestStateLess.new  
assert\_equal("", a.value)  
a.go(‘a’)  
assert\_equal(“waiting\_at\_1;”, a.value)  
b.go(‘b’)  
a.go(“passvoid”)  
assert\_equal(“waiting\_at\_1;retry;waiting\_at\_1;”, a.value)  
b.go(“password”)  
assert\_equal(“waiting\_at\_1;passed;waiting\_at\_2;”, b.value)  
a.go(“password”)  
assert\_equal  
(“waiting\_at\_1;retry;waiting\_at\_1;passed;waiting\_at\_2;”, a.value)  
b.go(“spam”)  
assert\_equal  
(“waiting\_at\_1;passed;waiting\_at\_2;passed2;waiting\_at\_3;”, b.value)  
count\_cc  
end  
end
