469,903 Members | 2,006 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

A question about reference counting

Hello Experts!

I reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that I don't understand
completely.

It says
"A garbarage collector, such as the one used in Java, maintains a record of
whether or not
an object is currentlys being used. An unused object is tagged as garbage,
which means
that it can be collected and returned to the pool of available memory. One
simple
technique used to implement a garbage collector is called reference
counting: Multiple objects share a single representation that keeps track of
the number of objects currently in use.
Reference counting is useful in everyday programming; for example, you
can use a string class, in which multiple objects can share the same
representation."

Now to my first question what does it mean with this sentence "Multiple
objects share a single representation that keeps track of the number of
objects currently in use."
Does it mean that many object share another object called X and in this
another object X is a reference counter that count the number of object
reference this object X.

Now to my second question what does it mean with this sentence "you
can use a string class, in which multiple objects can share the same
representation". This second question is the last part of sentence
"Reference counting is useful in everyday programming; for example, you
can use a string class, in which multiple objects can share the same
representation."

Many thanks

//Tony

Aug 14 '05 #1
1 1661
Tony Johansson wrote:
Now to my first question what does it mean with this sentence "Multiple
objects share a single representation that keeps track of the number of
objects currently in use."
That's a bit sloppily worded.
Does it mean that many object share another object called X and in this
another object X is a reference counter that count the number of object
reference this object X.
Yes. You have one object that contains the actual data and some number of
references to it. The object maintains a reference count that contains the
number of references that refer to that object. When the reference count
reaches 0, there is no reference to the object anymore, so it can't be used
anymore (since the references are the only way of accessing the object),
which means it can be destroyed.
Now to my second question what does it mean with this sentence "you
can use a string class, in which multiple objects can share the same
representation". This second question is the last part of sentence
"Reference counting is useful in everyday programming; for example, you
can use a string class, in which multiple objects can share the same
representation."


It means the same thing. You have something like:

MyString a = "Hello";
MyString b = a;

Now there are two MyString objects, but those are only references to the
actual string data, which is shared between both objects. Instead of
copying the string data, b just refers to the same data as a, and the
reference count is incremented when b is created.

Aug 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Elbert Lev | last post: by
5 posts views Thread by John Marshall | last post: by
1 post views Thread by ash | last post: by
3 posts views Thread by lallous | last post: by
6 posts views Thread by Johnny Hansen | last post: by
1 post views Thread by Tony Johansson | last post: by
23 posts views Thread by Ray | last post: by
1 post views Thread by oec.deepak | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.