Forum: Ruby object.reference_count

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jeff R. (Guest)
on 2006-05-27 20:51
(Received via mailing list)
Is there a way to get the current reference count for an object?
Tim H. (Guest)
on 2006-05-27 21:03
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.
Jeff R. (Guest)
on 2006-05-27 23:38
(Received via mailing list)
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?
Timothy H. (Guest)
on 2006-05-28 00:30
(Received via mailing list)
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?
Jeff R. (Guest)
on 2006-05-28 00:48
(Received via mailing list)
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...
(Guest)
on 2006-05-28 10:47
(Received via mailing list)
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/brow...
Jeff R. (Guest)
on 2006-05-28 13:44
(Received via mailing list)
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/brow...
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
Benjohn B. (Guest)
on 2006-05-28 14:39
(Received via mailing list)
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 :)


[1] Boost and the STL were the best things about C++ for me. They
really transformed my experience with the language.
Robert K. (Guest)
on 2006-05-28 15:31
(Received via mailing list)
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
C Erler (Guest)
on 2006-05-28 19:59
(Received via mailing list)
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 :)
>

For anyone who needs weak references:
http://www.ruby-doc.org/stdlib/libdoc/weakref/rdoc...
Jeff R. (Guest)
on 2006-05-28 20:18
(Received via mailing list)
C Erler wrote:
> For anyone who needs weak references:
> http://www.ruby-doc.org/stdlib/libdoc/weakref/rdoc...
>

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
Minkoo S. (Guest)
on 2006-05-30 15:41
(Received via mailing list)
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.
This topic is locked and can not be replied to.