[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Doubt about GC

Eustaquio Rangel de Oliveira Jr.

5/16/2005 7:02:00 PM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello there!

I have this program:

- -------------------------------------------------------------------------
class Car
attr_reader :number
def initialize(n)
@number=n
puts "Creating car no. #{n}:#{object_id}"
end
def to_s
"Car no. #{@number}"
end
end

def makeCar(n)
c = Car.new(n)
end

def listCars
puts "\nListing cars:"
ObjectSpace.each_object(Car) {|o| puts "#{o}:#{o.object_id}"}
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
http://b...
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-----


15 Answers

threeve.org

5/16/2005 7:16:00 PM

0

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
> attr_reader :number
> def initialize(n)
> @number=n
> puts "Creating car no. #{n}:#{object_id}"
> end
> def to_s
> "Car no. #{@number}"
> end
> end
>
> def makeCar(n)
> c = Car.new(n)
> end
>
> def listCars
> puts "\nListing cars:"
> ObjectSpace.each_object(Car) {|o| puts "#{o}:#{o.object_id}"}
> 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
> http://b...
> 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-----
>
>

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


Kent Sibilev

5/16/2005 7:21:00 PM

0

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
> attr_reader :number
> def initialize(n)
> @number=n
> puts "Creating car no. #{n}:#{object_id}"
> end
> def to_s
> "Car no. #{@number}"
> end
> end
>
> def makeCar(n)
> c = Car.new(n)
> end
>
> def listCars
> puts "\nListing cars:"
> ObjectSpace.each_object(Car) {|o| puts "#{o}:#{o.object_id}"}
> 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
> http://b...
> 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-----
>
>


Eustaquio Rangel de Oliveira Jr.

5/16/2005 7:24:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
http://b...
Usuário GNU/Linux no. 224050
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFCiPM4b6UiZnhJiLsRAiQ4AJwPMxk7sMOzjdpZYb7GplFiPLTUiACfRkP6
I0xFnyqaZjyYoEUBm0NRxUw=
=jHMR
-----END PGP SIGNATURE-----


Lothar Scholz

5/16/2005 7:39:00 PM

0

Hello Eustaquio,


ERdOJ> My doubt is why Car no. 4 is still there, after running gc. There is no
ERdOJ> reference to it outside the function, right? So it should not have be
ERdOJ> 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.ru...
CTO Scriptolutions Ruby, PHP, Python IDE 's




Eustaquio Rangel de Oliveira Jr.

5/16/2005 8:19:00 PM

0

-----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?

Thanks!

- ----------------------------
Eustáquio "TaQ" Rangel
eustaquiorangel@yahoo.com
http://b...
Usuário GNU/Linux no. 224050
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFCiQAbb6UiZnhJiLsRAkSTAJ9m21h3Dq4EKBhmxA9tQXUxDIqx/gCgtmRL
vbhMVGGS5wH6Kpuab78unx4=
=KwJz
-----END PGP SIGNATURE-----


Caio Tiago Oliveira

5/22/2005 7:04:00 PM

0

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





Erik Setzer

8/19/2013 1:58:00 PM

0

On Sunday, August 18, 2013 7:57:23 PM UTC-4, Samuel Campbell wrote:
> Stopped being as political.

Politics are bullshit and can go to hell. The best decision I've made lately is to just avoid people involved in them, go NPA, and, on the rare occasions I saunter into my political Facebook account, ripping on both sides of the aisle equally for being assholes.

Your life can only go up by ignoring politics. Unless, of course, it's the power structure within the Imperium. Then it's fair game for discussion.

- Erik

(That was my long-winded way of saying, "Dude, I feel ya, and support your decision wholeheartedly.")

rsingers

11/4/2013 9:12:00 AM

0

On Monday, 19 August 2013 11:57:23 UTC+12, Samuel Campbell wrote:
> On Saturday, 17 August 2013 22:36:52 UTC+12, Glassboy wrote:
>
> > So what's up?

>
> Hey Rob, Hobby going stronger than ever before. Stopped being as political.

That's called not being a retard.

So what are all you ladies up to?

Blackheart

11/4/2013 1:05:00 PM

0

On Monday, November 4, 2013 4:12:18 AM UTC-5, Glassboy wrote:
> On Monday, 19 August 2013 11:57:23 UTC+12, Samuel Campbell wrote:
>
> > On Saturday, 17 August 2013 22:36:52 UTC+12, Glassboy wrote:
>
> >
>
> > > So what's up?
>
>
>
> >
>
> > Hey Rob, Hobby going stronger than ever before. Stopped being as political.
>
>
>
> That's called not being a retard.
>
>
>
> So what are all you ladies up to?

Switched from playing Demons in both WFB and 40K to playing Ultramarines (Eventually to be UltraPatriots) in just 40K. Taking a A+/Net+/CCNA certification class. Having a good laugh at the expense of the local Baltimore Ravens fans.

Fern & Sam Campbell

11/4/2013 11:13:00 PM

0

On Monday, 4 November 2013 22:12:18 UTC+13, Glassboy wrote:
> On Monday, 19 August 2013 11:57:23 UTC+12, Samuel Campbell wrote:
>
> > On Saturday, 17 August 2013 22:36:52 UTC+12, Glassboy wrote:
>
> >
>
> > > So what's up?
>
>
>
> >
>
> > Hey Rob, Hobby going stronger than ever before. Stopped being as political.
>
>
>
> That's called not being a retard.
>
>
>
> So what are all you ladies up to?

Playing Warmachine and qualified 6th for WFB NZ Masters which is in the Tron.