473,804 Members | 3,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When to use a garbage collector?

Hello,
traditionally, in C++, dynamically allocated memory has been
managed explicitly by calling "delete" in the application code.

Now, in addition to the standard library strings, containers, and
auto_ptrs, gurus suggest that may be better to use a reference-counted
smart pointer, or a garbage-collector.

But in which cases it is better to use one technique and in which cases
another? IOW, which is the design criterion?

And if, after having completed a working system, a technique would
result more performing than another, or better for other reasons, is it
advisable to change the memory management strategy of that working system?

--
Carlo Milanesi
http://digilander.libero.it/carlmila
Jun 27 '08
46 2196
In article <MP************ ************@ne ws.sunsite.dk>,
jc*****@taeus.c om says...

[ ... ]
That's the whole point of using an exception: delaying handling of the
error until you reach a point at which it's apparent how it should be
handled.
Pardon me -- it's not really the _whole_ point, but merely part of the
point. Nonetheless, it's an important part of the point...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #21
On 11 kesä, 20:33, Jerry Coffin <jcof...@taeus. comwrote:
Oh my. In most cases you can do something reasonably productive when an
exception gets thrown.
Like what? If the object can't be constructed then there is
something seriously wrong and the program can't obviously
recover from that.
Even in the worst case, you probably want to
catch it and print out the most meaningful error message you can
"Error: object can't be constructed."
Jun 27 '08 #22
Carlo Milanesi wrote:
Hello,
traditionally, in C++, dynamically allocated memory has been managed
explicitly by calling "delete" in the application code.

Now, in addition to the standard library strings, containers, and
auto_ptrs, gurus suggest that may be better to use a reference-counted
smart pointer, or a garbage-collector.

But in which cases it is better to use one technique and in which cases
another? IOW, which is the design criterion?
For scarce resources, like resource handles and large memory blocks, use
reference counting. For routine memory allocations, use gc.

And if, after having completed a working system, a technique would
result more performing than another, or better for other reasons, is it
advisable to change the memory management strategy of that working system?
Using gc effectively entails using different coding/design techniques,
so it is not practical to try to shift the design after the fact.

-------------
Walter Bright
Digital Mars
http://www.digitalmars.com
C, C++, D programming language compilers
Jun 27 '08 #23
Krice wrote:
On 11 kesä, 20:33, Jerry Coffin <jcof...@taeus. comwrote:
>Oh my. In most cases you can do something reasonably productive
when an exception gets thrown.

Like what? If the object can't be constructed then there is
something seriously wrong and the program can't obviously
recover from that.
The task being performed might fail, but not the whole server.
>
>Even in the worst case, you probably want to
catch it and print out the most meaningful error message you can

"Error: object can't be constructed."
"Query result too large - please use a more specific selection"
Bo Persson
Jun 27 '08 #24
Jerry Coffin wrote:
[ ... ]
Very interesting. Are there nice readings in the inet about this topic,
which include some C++ snippets for everday usage?
-- Maik
Jun 27 '08 #25
"Krice" <pa****@mbnet.f iwrote in message
news:4f******** *************** ***********@z72 g2000hsb.google groups.com...
On 11 kesä, 20:33, Jerry Coffin <jcof...@taeus. comwrote:
Oh my. In most cases you can do something reasonably productive when an
exception gets thrown.
Like what? If the object can't be constructed then there is
something seriously wrong and the program can't obviously
recover from that.
Not always true. Lets say that the object requested a synchronization
resource from the OS, but failed because too many of them were in use my the
program. Well, the exception handler could run some "purge" procedures to
try an rid itself of cached unnecessary state, ect... and defer trying again
to a later time. On a server, if an object fails construction during the
initialization process of a new connection, the exception handler could send
a message to the client application saying something like "Server overload;
try again later.". I could go on and on. The main point is that an
application can attempt to mutate the conditions which caused the exception
and defer trying again until things settle down. Sometimes, you can recover
from exceptions quite nicely.

One example in C... If malloc returns NULL, what do you do? Well, you could
try something like:

http://groups.google.com/group/comp....7956d31e6ca5cc

;^)

Jun 27 '08 #26
On Jun 11, 1:50*pm, "Bo Persson" <b...@gmb.dkwro te:
Krice wrote:
On 11 kesä, 20:33, Jerry Coffin <jcof...@taeus. comwrote:
Oh my. In most cases you can do something reasonably productive
when an exception gets thrown.
Like what? If the object can't be constructed then there is
something seriously wrong and the program can't obviously
recover from that.

The task being performed might fail, but not the whole server.
Yes, I think this sort of situation should be considered:

AccountBase
|
Account1_1
|
Account1_2

If the program is expecting to receive an Account1_2 object
over a network and a read fails/times out, I want to have the
object preserved as an Account1_1 if that much of the object
was successfully received. In baseball sometimes a hit is more
than a single, but not a double. If a runner tries to turn it
into a double he's thrown out. A double is better than a
single, but a single is much better than an out.

Brian Wood
Ebenezer Enterprises
www.webEbenezer.net
Jun 27 '08 #27
In article <ex************ *************** ****@ram.dialup .fu-berlin.de>,
ra*@zedat.fu-berlin.de says...

[ ... ]
Ada83, and possibly already Algol68 and Clu had exceptions,
but I do not know who first came up with this concept.
Maybe it is inspired from processors, which might raise
interrupts, error signals or traps to invoke handlers.
PL/I had them in 1964. BASIC has had some exception handling-like stuff
(e.g. on error goto) for quite a while as well, though I'm not sure
whether it was in the original 1964 version or not.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #28
In article <4f6f494b-3511-4d36-89ac-2f0d32f49877
@z72g2000hsb.go oglegroups.com> , pa****@mbnet.fi says...
On 11 kesä, 20:33, Jerry Coffin <jcof...@taeus. comwrote:
Oh my. In most cases you can do something reasonably productive when an
exception gets thrown.
Like what? If the object can't be constructed then there is
something seriously wrong and the program can't obviously
recover from that.
Maybe it can and maybe it can't. It depends entirely on what kind of
object you're trying to create and how important it is to the program.
In some cases it truly is crucial to the program, and all you can do is
print out the error message and quit. In other cases, it may be purely
cosmetic, and the program can do its real work without it. In still
other cases, there may be _other_ things that are more or less cosmetic
that can be discarded when/if necessary to allow the real work to
finish.

Just for example, I've written quite a few programs that have some sort
of windowed interface. Many of them display some sort of bitmap in the
background -- but if the system runs low of memory, they can discard the
bitmap, and get along with a plain grey background for a while.
Even in the worst case, you probably want to
catch it and print out the most meaningful error message you can
"Error: object can't be constructed."
Usually you can come up with something more meaningful than that -- at
the very least, something about the type of object whose construction
failed, and the operation(s) being attempted when the failure happened.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #29
On Jun 11, 9:37 pm, Krice <pau...@mbnet.f iwrote:
On 11 kesä, 20:33, Jerry Coffin <jcof...@taeus. comwrote:
Oh my. In most cases you can do something reasonably
productive when an exception gets thrown.
Like what? If the object can't be constructed then there is
something seriously wrong and the program can't obviously
recover from that.
It depends why the object can't be constructed. There are a lot
of things one can recover from.
Even in the worst case, you probably want to catch it and
print out the most meaningful error message you can
"Error: object can't be constructed."
Respond to the request with an "insufficie nt resources" error
(in case of bad_alloc, for example)?

Like most things, exceptions have a cost---in particular, they
do disrupt program logic. There are some specific cases,
however (not many, really, but although few, they occur fairly
frequently) in which the cost of the alternatives is higher. If
you run out of memory processing a request in a server, you
can't just bring the server down; you have to respond with an
"insufficie nt resources" error (and continue to accept simpler
requests). And there's no way to check before constructing an
object that it will have sufficient memory, so you have no
choice but to detect the error in the constructor.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #30

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

Similar topics

1
2339
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and then throws them away and then monitors the garbage collection and store statistics on it, preferably in C#. I want to know what is the longest period of time that an application may lock up while garbage collection is processing. Thanks!
4
2045
by: Pedro Miguel Carvalho | last post by:
Greetings. I'm creating a project that as a intricate relation between object kind of like a set where each object in the set can be connect to a subset of object of the set and objects not in the set can connect to objects in the set. Every object inherits a interface to allow for a mark and sweep garbage collector (GC) and my implementation works correctly and efficiently but only in a single thread. How can I garbage collect in a...
10
2056
by: pachanga | last post by:
The Hans-Boehm garbage collector can be successfully used with C and C++, but not yet a standard for C++.. Is there talks about Garbage Collector to become in the C++ standard?
6
5174
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different documents (txt, MS Office files, pdf, pictures,…) from inside my app. It must start the file with the adequate external program (Notepad, MS Office programs, Acrobat Reader, some Picture viewer,... ) and be notified when this programs closes the...
4
1701
by: R. MacDonald | last post by:
Hello, all, I have a .NET application (VB) that passes the address of a delegate to unmanaged code in a DLL. The unmanaged code then uses the delegate as a call-back. This seems to work fine, but now I am worried about garbage collection. I am concerned that the location of the delegate might be altered as a result of other (unused) objects being garbage collected. This would probably cause undesirable results when the unmanaged DLL...
13
3821
by: Mingnan G. | last post by:
Hello everyone. I have written a garbage collector for standard C++ application. It has following main features. 1) Deterministic Finalization Providing deterministic finalization, the system can manage resources as well as objects. The programming style is clear and easy, conforming to RAII (Resource Acquisition Is Initialization) idiom of C++ programmers. The memory usage is very efficient, acyclic garbage is
44
8285
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable End Class now where would I properly dispose of this? In the finalize method? I am loading the data for the table in a subroutine that is executed at form load, of course all the commands tied to it are wrapped in using blocks, but
350
11944
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
0
2201
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below). Traditionally I think of a classic structure as simply an object where every member is public. But with...
0
9585
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
10586
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
10323
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
10082
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...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
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...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2997
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.