473,748 Members | 5,242 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3251
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
2184
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 not use garbage collection (as Java does). So if the script runs a loop: for i in range(100): f = Obj(i)
1
2710
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 client deletes a refernce to it
27
5970
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
1752
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. In its member function, an object manages to destroy last reference to itself and thus destructs before the end of the member function.
1
1877
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 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,
4
4196
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) { } double &operator()(int i) { return myX; }
1
2278
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
8
1861
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 following operator in my smartpointer class: .... operator ObjectType * () const
275
12345
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
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.