# Question on GC

**URL:** <https://www.ruby-forum.com/t/question-on-gc/95027>\
**Category:** Ruby\
**Created:** [May 19, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/question-on-gc/95027 "2006-05-19T15:59:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![madan\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@madan\_m](https://www.ruby-forum.com/u/madan_m)\
**Post date:** [May 19, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/question-on-gc/95027/1 "2006-05-19T15:59:15Z")

</div>

Hello Rubyists:

Does Ruby’s GC treat a class variable (@@x) as ‘global scope’ or as  
‘local scope’?

Here is my situation:

I have a class variable that holds instances of ‘self’. These objects  
are not referenced any where else; these objects are retrieved using  
its **id** which is stored else where. It is my understanding that  
Ruby’s GC doesn’t see these stored **id** as a reference to the  
objects (since **id** -\> Fixnum).

I don’t want these objects to be garbage collected as long as its  
instance is referred the class variable. Please note that the objects  
are not referenced by other class’s objects.

Example:

class Test  
@@x = []

def initialize()  
# among other thing …  
@@x.push(self) if not @@x.include?(self)  
end

def kill\_self()  
# among other things  
@@x.delete(self)  
end  
end

class TestFactory  
@store = []

def getATest()  
procObj = Proc.new do |id|  
@store.delete(id)  
end

```
  t = Test.new()
  @store.push(t. __id__ )

  ObjectSpace.define_finalizer(t, procObj)
  return t. __id__

```

end

def releaseTest(id)  
\_id2Obj(id).kill\_self() # \<— retrieves Obj from **id**  
end

end

testF = TestFactory.new()  
id = testF.getATest()  
…  
…

# I don’t want ‘t’ to be garbage collected here. GC.disable is not an

option  
…  
…  
testF.releaseTest(id)

**END**

Questions:

(1) Will ‘t’ be garbage collected since the only reference to ‘t’ is  
in @@x, which could go out of scope?  
(2) Is my understanding ‘scope’ correct?

Any help will be greatly appreciated.

Thanks,  
-Madan.

---

<div class="post-metadata">

**Author:** ![madan\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@madan\_m](https://www.ruby-forum.com/u/madan_m)\
**Post date:** [May 19, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/question-on-gc/95027/2 "2006-05-19T16:20:45Z")

</div>

Hi,

In message “Re: Question on GC”  
on Fri, 19 May 2006 22:58:28 +0900, “Madan M.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Does Ruby’s GC treat a class variable (@@x) as ‘global scope’ or as  
|‘local scope’?

Global, in a sense you use.

|(1) Will ‘t’ be garbage collected since the only reference to ‘t’ is  
|in @@x, which could go out of scope?

No, class variables remain as it is, even after its scope ended.

|(2) Is my understanding ‘scope’ correct?

It’s called ‘extent’ in GC field. Class variables have limited scope  
and infinite extent.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![madan\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@madan\_m](https://www.ruby-forum.com/u/madan_m)\
**Post date:** [May 19, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/question-on-gc/95027/3 "2006-05-19T16:33:21Z")

</div>

Thanks, Matz.

-Madan.

---

<div class="post-metadata">

**Author:** ![madan\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@madan\_m](https://www.ruby-forum.com/u/madan_m)\
**Post date:** [May 19, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/question-on-gc/95027/4 "2006-05-19T20:27:43Z")

</div>

Nobu:

The procObj does not refer to object ‘t’. procObj is attached as a  
finalizer to object ‘t’. If the object reference to ‘t’ is not stored  
in the class variable, object ‘t’ will be marked (or should I say  
un-marked) for garbage collection.

Please correct me if I am wrong, or if I am missing the obvious (like  
the reference to object ‘t’ in procObj, which I cant see).

Thanks,  
Madan.

---

<div class="post-metadata">

**Author:** ![madan\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@madan\_m](https://www.ruby-forum.com/u/madan_m)\
**Post date:** [May 19, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/question-on-gc/95027/5 "2006-05-19T18:51:33Z")

</div>

Hi,

At Fri, 19 May 2006 22:58:28 +0900,  
Madan M. wrote in [ruby-talk:193553]:

> end  
> Note that procObj also refers t, so this object will never get  
> collected.

```
  procObj = @store.method(:delete).to_proc

```

is an alternative.

---

<div class="post-metadata">

**Author:** ![madan\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@madan\_m](https://www.ruby-forum.com/u/madan_m)\
**Post date:** [May 19, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/question-on-gc/95027/6 "2006-05-19T21:11:30Z")

</div>

On May 19, 2006, at 11:24 AM, Madan M. wrote:

> > > ```
> > > t = Test.new()
> > > 
> > > ```
> > 
> > is an alternative.
> 
> The procObj does not refer to object ‘t’.

Even the you don’t refer to t by name, procObj’s environment knows  
about t.

> Please correct me if I am wrong, or if I am missing the obvious (like  
> the reference to object ‘t’ in procObj, which I cant see).

You can’t see them, but they are there. It is a feature of Ruby.

$ cat x.rb  
def x  
p = proc { eval(‘p t’) }  
t = 5  
return p  
end

x.call  
$ ruby x.rb  
5

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
