# Doubt about GC

**URL:** <https://rubytalk.org/t/doubt-about-gc/18441>\
**Category:** ruby-talk\
**Created:** [16 May 2005 19:02 UTC](https://rubytalk.org/t/doubt-about-gc/18441 "2005-05-16T19:02:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Eustaquio\_Rangel\_de](https://avatars.discourse-cdn.com/v4/letter/_/c4cdca/32.png) [@\_Eustaquio\_Rangel\_de](https://rubytalk.org/u/_Eustaquio_Rangel_de)\
**Post date:** [16 May 2005 19:02 UTC](https://rubytalk.org/t/doubt-about-gc/18441/1 "2005-05-16T19:02:23Z")

</div>

Hello there!

I have this program:

- -------------------------------------------------------------------------  
class Car  
&nbsp;&nbsp;attr\_reader :number  
&nbsp;&nbsp;def initialize(n)  
&nbsp;&nbsp;&nbsp;&nbsp;@number=n  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Creating car no. #{n}:#{object\_id}"  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;"Car no. #{@number}"  
&nbsp;&nbsp;end  
end

def makeCar(n)  
&nbsp;&nbsp;c = Car.new(n)  
end

def listCars  
&nbsp;&nbsp;puts "\nListing cars:"  
&nbsp;&nbsp;ObjectSpace.each\_object(Car) {|o| puts "#{o}:#{o.object\_id}"}  
&nbsp;&nbsp;puts  
end

c1 = makeCar(1)  
makeCar(2)  
makeCar(3)

listCars()  
makeCar(4)  
makeCar(5)

listCars();

puts "Firing gc."  
GC.start  
listCars();  
- -------------------------------------------------------------------------

When I run, I get:

Creating car no. 1:-604870360  
Creating car no. 2:-604870370  
Creating car no. 3:-604870410

Listing cars:  
Car no. 3:-604870410  
Car no. 2:-604870370  
Car no. 1:-604870360

Creating car no. 4:-604870500  
Creating car no. 5:-604870490

Listing cars:  
Car no. 4:-604870500  
Car no. 5:-604870490  
Car no. 3:-604870410  
Car no. 2:-604870370  
Car no. 1:-604870360

Firing gc.

Listing cars:  
Car no. 4:-604870500  
Car no. 1:-604870360

My doubt is why Car no. 4 is still there, after running gc. There is no  
reference to it outside the function, right? So it should not have be  
sweeped by GC?

Thanks!

- ----------------------------  
Eustáquio "TaQ" Rangel  
[eustaquiorangel@yahoo.com](mailto:eustaquiorangel@yahoo.com)  
[http://beam.to/taq](http://beam.to/taq)  
Usuário GNU/Linux no. 224050

---

<div class="post-metadata">

**Author:** ![Jason\_Foreman](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@Jason\_Foreman](https://rubytalk.org/u/Jason_Foreman)\
**Post date:** [16 May 2005 19:16 UTC](https://rubytalk.org/t/doubt-about-gc/18441/2 "2005-05-16T19:16:27Z")

</div>

You script returns the expected result for me (only car 1). WinXP, Ruby 1.8.2.

I'm guessing garbage collection is happening asynchronously, and that  
your listCars executed before car 4 was collected? Try sleep(2) in  
between GC.start and listCars to see if that changes the result.

This is only a guess, and could be very wrong. I have not looked at  
the Ruby source to verify how garbage collection works.

Jason

> **···**
>
> On 5/16/05, Eustaquio Rangel de Oliveira Jr. \<eustaquiorangel@yahoo.com\> wrote:
> 
> > -----BEGIN PGP SIGNED MESSAGE-----  
> > Hash: SHA1
> > 
> > Hello there!
> > 
> > I have this program:
> > 
> > - -------------------------------------------------------------------------  
> > class Car  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :number  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(n)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@number=n  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Creating car no. #{n}:#{object\_id}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def to\_s  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Car no. #{@number}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > def makeCar(n)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = Car.new(n)  
> > end
> > 
> > def listCars  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "\nListing cars:"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectSpace.each\_object(Car) {|o| puts "#{o}:#{o.object\_id}"}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts  
> > end
> > 
> > c1 = makeCar(1)  
> > makeCar(2)  
> > makeCar(3)
> > 
> > listCars()  
> > makeCar(4)  
> > makeCar(5)
> > 
> > listCars();
> > 
> > puts "Firing gc."  
> > GC.start  
> > listCars();  
> > - -------------------------------------------------------------------------
> > 
> > When I run, I get:
> > 
> > Creating car no. 1:-604870360  
> > Creating car no. 2:-604870370  
> > Creating car no. 3:-604870410
> > 
> > Listing cars:  
> > Car no. 3:-604870410  
> > Car no. 2:-604870370  
> > Car no. 1:-604870360
> > 
> > Creating car no. 4:-604870500  
> > Creating car no. 5:-604870490
> > 
> > Listing cars:  
> > Car no. 4:-604870500  
> > Car no. 5:-604870490  
> > Car no. 3:-604870410  
> > Car no. 2:-604870370  
> > Car no. 1:-604870360
> > 
> > Firing gc.
> > 
> > Listing cars:  
> > Car no. 4:-604870500  
> > Car no. 1:-604870360
> > 
> > My doubt is why Car no. 4 is still there, after running gc. There is no  
> > reference to it outside the function, right? So it should not have be  
> > sweeped by GC?
> > 
> > Thanks!
> > 
> > - ----------------------------  
> > Eustáquio "TaQ" Rangel  
> > eustaquiorangel@yahoo.com  
> > [The easiest way to create, record and stream live video - Beings](http://beam.to/taq)  
> > Usuário GNU/Linux no. 224050  
> > -----BEGIN PGP SIGNATURE-----  
> > Version: GnuPG v1.2.7 (GNU/Linux)
> > 
> > iD8DBQFCiO4mb6UiZnhJiLsRAmJ7AJ9M8a1CsY/ksAW1fThdmIpeLbB7gwCcCz5w  
> > +sh/g0F1Vork+DKH/xRj+9M=  
> > =Tm+7  
> > -----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Kent\_Sibilev1](https://avatars.discourse-cdn.com/v4/letter/k/9d8465/32.png) [@Kent\_Sibilev1](https://rubytalk.org/u/Kent_Sibilev1)\
**Post date:** [16 May 2005 19:20 UTC](https://rubytalk.org/t/doubt-about-gc/18441/3 "2005-05-16T19:20:36Z")

</div>

When I run it, I get:

Creating car no. 1:20722128  
Creating car no. 2:20722116  
Creating car no. 3:20722068

Listing cars:  
Car no. 3:20722068  
Car no. 2:20722116  
Car no. 1:20722128

Creating car no. 4:20721960  
Creating car no. 5:20721972

Listing cars:  
Car no. 4:20721960  
Car no. 5:20721972  
Car no. 3:20722068  
Car no. 2:20722116  
Car no. 1:20722128

Firing gc.

Listing cars:  
Car no. 1:20722128

Kent.

> **···**
>
> On 5/16/05, Eustaquio Rangel de Oliveira Jr. \<eustaquiorangel@yahoo.com\> wrote:
> 
> > -----BEGIN PGP SIGNED MESSAGE-----  
> > Hash: SHA1
> > 
> > Hello there!
> > 
> > I have this program:
> > 
> > - -------------------------------------------------------------------------  
> > class Car  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :number  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(n)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@number=n  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Creating car no. #{n}:#{object\_id}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def to\_s  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Car no. #{@number}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > def makeCar(n)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = Car.new(n)  
> > end
> > 
> > def listCars  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "\nListing cars:"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectSpace.each\_object(Car) {|o| puts "#{o}:#{o.object\_id}"}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts  
> > end
> > 
> > c1 = makeCar(1)  
> > makeCar(2)  
> > makeCar(3)
> > 
> > listCars()  
> > makeCar(4)  
> > makeCar(5)
> > 
> > listCars();
> > 
> > puts "Firing gc."  
> > GC.start  
> > listCars();  
> > - -------------------------------------------------------------------------
> > 
> > When I run, I get:
> > 
> > Creating car no. 1:-604870360  
> > Creating car no. 2:-604870370  
> > Creating car no. 3:-604870410
> > 
> > Listing cars:  
> > Car no. 3:-604870410  
> > Car no. 2:-604870370  
> > Car no. 1:-604870360
> > 
> > Creating car no. 4:-604870500  
> > Creating car no. 5:-604870490
> > 
> > Listing cars:  
> > Car no. 4:-604870500  
> > Car no. 5:-604870490  
> > Car no. 3:-604870410  
> > Car no. 2:-604870370  
> > Car no. 1:-604870360
> > 
> > Firing gc.
> > 
> > Listing cars:  
> > Car no. 4:-604870500  
> > Car no. 1:-604870360
> > 
> > My doubt is why Car no. 4 is still there, after running gc. There is no  
> > reference to it outside the function, right? So it should not have be  
> > sweeped by GC?
> > 
> > Thanks!
> > 
> > - ----------------------------  
> > Eustáquio "TaQ" Rangel  
> > eustaquiorangel@yahoo.com  
> > [The easiest way to create, record and stream live video - Beings](http://beam.to/taq)  
> > Usuário GNU/Linux no. 224050  
> > -----BEGIN PGP SIGNATURE-----  
> > Version: GnuPG v1.2.7 (GNU/Linux)
> > 
> > iD8DBQFCiO4mb6UiZnhJiLsRAmJ7AJ9M8a1CsY/ksAW1fThdmIpeLbB7gwCcCz5w  
> > +sh/g0F1Vork+DKH/xRj+9M=  
> > =Tm+7  
> > -----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Lothar\_Scholz2](https://avatars.discourse-cdn.com/v4/letter/l/f14d63/32.png) [@Lothar\_Scholz2](https://rubytalk.org/u/Lothar_Scholz2)\
**Post date:** [16 May 2005 19:38 UTC](https://rubytalk.org/t/doubt-about-gc/18441/4 "2005-05-16T19:38:56Z")

</div>

Hello Eustaquio,

> My doubt is why Car no. 4 is still there, after running gc. There is no  
> reference to it outside the function, right? So it should not have be  
> sweeped by GC?

Unlikely but the stack marking is conservative. Maybe there is  
something on the stack that looks like a pointer to Car no.4. In this  
event Car no.4 will not be deleted.

> **···**
>
> --  
> Best regards, emailto: scholz at scriptolutions dot com  
> Lothar Scholz [http://www.ruby-ide.com](http://www.ruby-ide.com)  
> CTO Scriptolutions Ruby, PHP, Python IDE 's

---

<div class="post-metadata">

**Author:** ![\_Eustaquio\_Rangel\_de](https://avatars.discourse-cdn.com/v4/letter/_/c4cdca/32.png) [@\_Eustaquio\_Rangel\_de](https://rubytalk.org/u/_Eustaquio_Rangel_de)\
**Post date:** [16 May 2005 19:24 UTC](https://rubytalk.org/t/doubt-about-gc/18441/5 "2005-05-16T19:24:01Z")

</div>

Hi.

> You script returns the expected result for me (only car 1). WinXP, Ruby

1.8.2.

Weird! Here I'm running Slackware 10.1-current, Ruby 1.8.2.

> I'm guessing garbage collection is happening asynchronously, and that  
> your listCars executed before car 4 was collected? Try sleep(2) in  
> between GC.start and listCars to see if that changes the result.

No change, even if I sleep more than 2 seconds, but if I make

GC.start  
sleep(1)  
GC.start

it works, only rest Car 1.

> This is only a guess, and could be very wrong. I have not looked at  
> the Ruby source to verify how garbage collection works.

But it's a beggining and prove me that I'm not at least fully crazy ehehe.  
There is something there.

Thanks!

- ----------------------------  
Eustáquio "TaQ" Rangel  
eustaquiorangel@yahoo.com

> **[The easiest way to create, record and stream live video - Beings](https://beings.com)**
>
> Professional live streaming for marketers. Create, record and stream high-quality live video without being an expert.

Usuário GNU/Linux no. 224050

---

<div class="post-metadata">

**Author:** ![\_Eustaquio\_Rangel\_de](https://avatars.discourse-cdn.com/v4/letter/_/c4cdca/32.png) [@\_Eustaquio\_Rangel\_de](https://rubytalk.org/u/_Eustaquio_Rangel_de)\
**Post date:** [16 May 2005 20:19 UTC](https://rubytalk.org/t/doubt-about-gc/18441/6 "2005-05-16T20:19:02Z")

</div>

Hi Lothar!

> Unlikely but the stack marking is conservative. Maybe there is  
> something on the stack that looks like a pointer to Car no.4. In this  
> event Car no.4 will not be deleted.

Kind of some "memory dirty" or something like that?

Thanks!

- ----------------------------  
Eustáquio "TaQ" Rangel  
eustaquiorangel@yahoo.com

> **[The easiest way to create, record and stream live video - Beings](https://beings.com)**
>
> Professional live streaming for marketers. Create, record and stream high-quality live video without being an expert.

Usuário GNU/Linux no. 224050

---

<div class="post-metadata">

**Author:** ![Caio\_Tiago\_Oliveira](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@Caio\_Tiago\_Oliveira](https://rubytalk.org/u/Caio_Tiago_Oliveira)\
**Post date:** [22 May 2005 19:04 UTC](https://rubytalk.org/t/doubt-about-gc/18441/7 "2005-05-22T19:04:28Z")

</div>

Eustaquio Rangel de Oliveira Jr., 16-05-2005 17:19:

> -----BEGIN PGP SIGNED MESSAGE-----  
> Hash: SHA1
> 
> Hi Lothar!
> 
> \> Unlikely but the stack marking is conservative. Maybe there is  
> \> something on the stack that looks like a pointer to Car no.4. In this  
> \> event Car no.4 will not be deleted.
> 
> Kind of some "memory dirty" or something like that?

I'm in such a different hardware and I got a result somehow different in ruby1.8 and ruby1.9.

I've HT enabled and running the code in both versions left cars 1, 4 and 5 after GC.  
If I sleep a lot of time (I've tried uo to 10 sec), in ruby1.8 it still left the three cars and in ruby1.9 only cars 1 and 5.

$ ruby --version  
ruby 1.8.2 (2005-04-11) [i386-linux]

$ruby Car.rb

Creating car no. 1:-604682514  
Creating car no. 2:-604682524  
Creating car no. 3:-604682564

Listing cars:  
Car no. 3:-604682564  
Car no. 2:-604682524  
Car no. 1:-604682514

Creating car no. 4:-604682654  
Creating car no. 5:-604682644

Listing cars:  
Car no. 4:-604682654  
Car no. 5:-604682644  
Car no. 3:-604682564  
Car no. 2:-604682524  
Car no. 1:-604682514

Sleeping...  
Firing gc.

Listing cars:  
Car no. 5:-604682644  
Car no. 1:-604682514

todos@tiago:~/caio/prog/ruby$ ruby1.8 Car.rb  
Creating car no. 1:-604583790  
Creating car no. 2:-604583800  
Creating car no. 3:-604583840

Listing cars:  
Car no. 3:-604583840  
Car no. 2:-604583800  
Car no. 1:-604583790

Creating car no. 4:-604583930  
Creating car no. 5:-604583920

Listing cars:  
Car no. 4:-604583930  
Car no. 5:-604583920  
Car no. 3:-604583840  
Car no. 2:-604583800  
Car no. 1:-604583790

Sleeping...  
Firing gc.

Listing cars:  
Car no. 4:-604583930  
Car no. 5:-604583920  
Car no. 1:-604583790

and  
$ ruby1.9 --version  
ruby 1.9.0 (2005-04-12) [i386-linux]

$ruby1.9 Car.rb  
Creating car no. 1:-604682514  
Creating car no. 2:-604682524  
Creating car no. 3:-604682564

Listing cars:  
Car no. 3:-604682564  
Car no. 2:-604682524  
Car no. 1:-604682514

Creating car no. 4:-604682654  
Creating car no. 5:-604682644

Listing cars:  
Car no. 4:-604682654  
Car no. 5:-604682644  
Car no. 3:-604682564  
Car no. 2:-604682524  
Car no. 1:-604682514

Sleeping...  
Firing gc.

Listing cars:  
Car no. 5:-604682644  
Car no. 1:-604682514
