By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,719 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,719 IT Pros & Developers. It's quick & easy.

A question about reference counting

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.