473,324 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 3208
Tony Johansson wrote:

[snipped] <g>

There are several ways to implement reference counting. One way is to
create a wrapper type that holds a pointer to the resource and a pointer
to a memory block that holds the reference count. The copy operations on
objects of this type increment the reference count, and the destructor
decrements the reference count. (That's a bit simplified, but good
enough for now) When the reference count goes to zero the memory block
that holds the reference count gets freed and the resource gets freed.
So as long as there's one or more of these wrapper objects around the
resource still exists. When the last one goes away the resource goes
away, too.

That's not the same as garbage collection. A garbage collector is part
of the memory manager. When the application runs out of memory the
garbage collector paws through the application's data structures,
following pointers to see which heap blocks still have live pointers to
them. Blocks that don't have live pointers are garbage, and available
for recycling.

The difference between the two is twofold. First, with reference
counting, the controlled resource is released as soon as the last
reference to it goes away, with garbage collection the memory for the
controlled resource is released when the garbage collector needs it,
which might never happen. Second, with reference counting you have to
worry about circular data structures. For example, suppose a node holds
a reference-counted pointer to another node, and that second node holds
a reference-counted pointer to the first node. Even if all of the
program's references to these two nodes are gone, their reference counts
are both still one, because each holds a pointer to the other. They'll
never get recycled. With a garbage collector this is no problem. Neither
one is reachable from the program's data, so they both get recycled.

As to that mention of string classes, it's not necessarily true. Some
implementations of the standard string class use reference counting, and
some don't.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does...
1
by: ash | last post by:
hi does anyone has any experience with flyweight pattern with refernce counting i want to share objects between multiple clients and want to delete the object from shared pool when the last...
27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
0
by: Kalle Rutanen | last post by:
Hello I implemented reference counting in my program, and found out many problems associated with it. I wonder if the following problems can be solved automatically rather manually ? 1. ...
1
by: Tony Johansson | last post by:
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...
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
1
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
8
by: mathieu | last post by:
Hi there I have implemented a very simple smartpointer class (invasive design). And I was wondering what should be the natural API when using those in a Container. I choose to define the...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.