GC retentive? or "I'm not putting out the garbage!"

R

rickhg12hs

I don't understand some things about the garbage collector ... but I'd
like to.

$ ruby -w -e 'puts "Uncollected Bignums: %d" %
ObjectSpace.each_object(Bignum) {|i| p i}'
Uncollected Bignums: 0

Okay good - no Bignums hanging out.

$ ruby -w -e 'GC.start;puts "Uncollected Bignums: %d" %
ObjectSpace.each_object(Bignum) {|i| p i}'
Uncollected Bignums: 0

This is good too. GC doesn't instantiate Bignums.

$ ruby -w -e 'Integer("8"*50);puts "Uncollected Bignums: %d" %
ObjectSpace.each_object(Bignum) {|i| p i}'
88888888888888888888888888888888888888888888888888
Uncollected Bignums: 1

This seems reasonable - Bignum created and it shows up in ObjectSpace.

$ ruby -w -e 'a=Integer("8"*50);puts "Uncollected Bignums: %d" %
ObjectSpace.each_object(Bignum) {|i| p i}'
88888888888888888888888888888888888888888888888888
Uncollected Bignums: 1

This really makes sense - "a" needs to point to the Bignum.

$ ruby -w -e 'a=Integer("8"*50);GC.start;puts "Uncollected Bignums: %d"
% ObjectSpace.each_object(Bignum) {|i| p i}'
88888888888888888888888888888888888888888888888888
Uncollected Bignums: 1

Makes sense - can't throw away Bignum that "a" is referencing.

$ ruby -w -e 'Integer("8"*50);GC.start;puts "Uncollected Bignums: %d" %
ObjectSpace.each_object(Bignum) {|i| p i}'
88888888888888888888888888888888888888888888888888
Uncollected Bignums: 1

Hmmmm. Why is the Bignum still here?

$ ruby -w -e 'a=Integer("8"*50);a=nil;GC.start;puts "Uncollected
Bignums: %d" % ObjectSpace.each_object(Bignum) {|i| p i}'
88888888888888888888888888888888888888888888888888
Uncollected Bignums: 1

Same here. Why wasn't the Bignum garbage collected?
 
T

ts

r> $ ruby -w -e 'Integer("8"*50);GC.start;puts "Uncollected Bignums: %d" %
r> ObjectSpace.each_object(Bignum) {|i| p i}'
r> 88888888888888888888888888888888888888888888888888
r> Uncollected Bignums: 1

r> Hmmmm. Why is the Bignum still here?

The GC is conservative : it has found, somewhere on the stack, a reference
to the Bignum. This is why it don't remove it.
 
R

rickhg12hs

Thanks for the replies.

I had searched the group threads for GC related posts buts there are so
many of them I couldn't find what I was looking for. Is there a GC FAQ
somewhere with all the info consolidated?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top