Object.reference_count


#1

Is there a way to get the current reference count for an object?


#2

Jeff R. wrote:

Is there a way to get the current reference count for an object?

AFAIK Ruby doesn’t keep track of the reference count. If you’re coming
from Python, keep in mind that Ruby uses a mark-and-sweep GC, which is
fundamentally different from Python’s.


#3

Tim H. wrote:

Jeff R. wrote:

Is there a way to get the current reference count for an object?

AFAIK Ruby doesn’t keep track of the reference count. If you’re coming
from Python, keep in mind that Ruby uses a mark-and-sweep GC, which is
fundamentally different from Python’s.

Aha. Didn’t think about that. Are there callbacks or anything you can
use to implement reference counting without having to use special
methods for referencing an object?


#4

Timothy H. wrote:

Aha. Didn’t think about that. Are there callbacks or anything you
can use to implement reference counting without having to use special
methods for referencing an object?

Why don’t you describe what it is you’re trying to do?

Just wondering…


#5

Jeff R. wrote:

Aha. Didn’t think about that. Are there callbacks or anything you
can use to implement reference counting without having to use special
methods for referencing an object?

Why don’t you describe what it is you’re trying to do?

Just wondering…

if you’re using google groups you need to search the archives of
comp.lang.ruby, not ruby-talk
http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/c9530bf2d5d892a3?tvc=1


#6

Jeff R. wrote:

can use to implement reference counting without having to use special
methods for referencing an object?

Why don’t you describe what it is you’re trying to do?


#7

On 28 May 2006, at 10:41, Jeff R. wrote:

This spawned from a conversation with a friend who was working in C+

  • on a system that is all reference count based. He asked how it
    would work in ruby, and I didn’t know.

Thanks for the help Tim.

When I first came to Ruby, I’d been using C++'s Boost library, and
the smart pointer in that [1]. I noticed that Ruby didn’t have a weak
pointer, and felt I’d find that a problem. I made a lot of use of
weak pointers in manager objects; they’d only manage something for as
long as other people made use of it. I’ve definitely not needed them
at all though, so maybe it’s no big issue :slight_smile:

[1] Boost and the STL were the best things about C++ for me. They
really transformed my experience with the language.


#8

removed_email_address@domain.invalid wrote:

Aha. Didn’t think about that. Are there callbacks or anything you
http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/c9530bf2d5d892a3?tvc=1
Huh? I’m on the mailing list, and in googling around I didn’t find
information on reference counting, which makes sense given the garbage
collector used.

This spawned from a conversation with a friend who was working in C++ on
a system that is all reference count based. He asked how it would work
in ruby, and I didn’t know.

Thanks for the help Tim.

-Jeff


#9

On 28/05/06, Benjohn B. removed_email_address@domain.invalid wrote:

When I first came to Ruby, I’d been using C++'s Boost library, and
the smart pointer in that [1]. I noticed that Ruby didn’t have a weak
pointer, and felt I’d find that a problem. I made a lot of use of
weak pointers in manager objects; they’d only manage something for as
long as other people made use of it. I’ve definitely not needed them
at all though, so maybe it’s no big issue :slight_smile:

For anyone who needs weak references:
http://www.ruby-doc.org/stdlib/libdoc/weakref/rdoc/index.html


#10

2006/5/28, Jeff R. removed_email_address@domain.invalid:

This spawned from a conversation with a friend who was working in C++ on
a system that is all reference count based. He asked how it would work
in ruby, and I didn’t know.

You don’t need that in Ruby. GC will automatically take care of
removing objects that are not reachable any more. For some form of
cache that may release data occasionally you can use WeakReference (an
object reachable through WeakReferences only may be removed). Then
there are finalizers which can be used to execute some action if an
object is removed, but beware they are different than Java finalizers.
Also there are usually better methods of doing cleanup. The most used
idiom for this in Ruby are blocks and use the keyword “ensure” (see
File.open() for a typical example). If you do want to do this
yourself, the idiom goes like this (silly example):

def work
ar = Array.new( 10, 20 )
begin
return yield( ar )
ensure
# silly cleanup
ar.clear
end
end

irb(main):021:0* work {|x| x.inject(0) {|s,y|s+y}}
=> 200

HTH

Kind regards

robert


#11

C Erler wrote:

For anyone who needs weak references:
http://www.ruby-doc.org/stdlib/libdoc/weakref/rdoc/index.html

Aha. This isn’t what I was looking for, but I think it gets you the
same functionality without the book keeping of reference counting.

Thanks,
Jeff


#12

Ruby doesn’t have reference counting mechanism and it is good in itself.
Reference counting can not solve the problem of circular reference. As
an example, consider A -> B -> A situation where ‘A -> B’ means that
A has a a variable that references B.

In the example, A can not be freed because A is reference by B.
B also can not be freed because it is referenced by A. Hence, things
get complicated.

Mark & sweep is an elegant solution to this problem which C++ doesn’t
have (but C++ of MS have garbage collection functionality).

Sincerely,
Minkoo S.