Abstract
--------
Garbage collection is a method of managing memory by using a "collector"
library. Periodically, or triggered by an allocation request, the
collector looks for unused memory chunks and recycles them.
This memory allocation strategy has been adapted to C (and C++) by the
library written by Hans J Boehm and Alan J Demers.
Why a Garbage Collector?
-----------------------
Standard C knows only the malloc/calloc/free functions. The programmer
must manage each block of memory it allocates, never forgetting to call
the standard function free() for each block. Any error is immediately
fatal, but helas, not with immediate consequences. Many errors like
freeing a block twice (or more) or forgetting to free an allocated
block will be discovered much later (if at all). This type of bugs are
very difficult to find and a whole industry of software packages
exists just to find this type of bugs.
The garbage collector presents a viable alternative to the traditional
malloc/free "manual" allocation strategies. The allocator of Boehm
tries to find unused memory when either an allocation request is
done, or when explicitely invoked by the programmer.
The main advantage of a garbage collector is that the programmer is
freed from the responsability of allocating/deallocating memory. The
programmer requests memory to the GC, and then the rest is *automatic*.
Limitations of the GC.
---------------------
The GC needs to see all pointers in a program. Since it scans
periodically memory, it will assume that any block in its block list is
free to reuse when it can't find any pointers to it. This means that the
programmer can't store pointers in the disk, or in the "windows extra
bytes", as it was customary to do under older windows versions, or
elsewhere.
This is actually not a limitation since most programs do not write
pointers to disk, and expect them to be valid later...
Obviously, there is an infinite way to hide pointers (by XORing them
with some constant for instance) to hide them from the collector.
This is of no practical significance. Pointers aren't XORed in normal
programs, and if you stay within the normal alignment requirements
of the processor, everything works without any problems.
Performance considerations
--------------------------
In modern workstations, the time needed to make a complete sweep in
mid-size projects is very small, measured in some milliseconds. In
programs that are not real time the GC time is completely undetectable.
I have used Boehm's GC in the IDE of lcc-win32, specially in the
debugger. Each string I show in the "automatic" window is allocated
using the GC. In slow machines you can sometimes see a pause of
less than a second, completely undetectable unless you know that is
there and try to find it.
It must be said too that the malloc/free system is slow too, since at
each allocation request malloc must go through the list of free blocks
trying to find a free one. Memory must be consolidated too, to avoid
fragmentation, and a malloc call can become very expensive, depending
on the implementation and the allocation pattern done by the program.
Portability
-----------
Boehm's GC runs under most standard PC and UNIX/Linux platforms. The
collector should work on Linux, *BSD, recent Windows versions, MacOS X,
HP/UX, Solaris, Tru64, Irix and a few other operating systems. Some
ports are more polished than others. There are instructions for porting
the collector to a new platform. Kenjiro Taura, Toshio Endo, and Akinori
Yonezawa have made available a parallel collector.
Conclusions
-----------
The GC is a good alternative to traditional allocation strategies for C
(and C++). The main weakness of the malloc/free system is that it
doesn't scale. It is impossible to be good at doing a mind numbing task
without any error 100% of the time. You can be good at it, you can be
bad at it, but you can NEVER be perfect. It is human nature.
The GC frees you from those problems, and allows you to conecntrate in
the problems that really matter, and where you can show your strength
as software designer. It frees you from the boring task of keeping track
of each memory block you allocate.
jacob
Oct 11 '06
142 6873
Roland Pibinger wrote:
On Wed, 11 Oct 2006 18:52:54 +0200, jacob navia wrote:
>>Conclusions ----------- The GC is a good alternative to traditional allocation strategies for C (and C++).
GC is incompatible with C++ (destructors) and inappropriate for the
system programming language C (you didn't even mention the huge memory
overhead of GC).
Why should the destructors be touched? They just
do not call free() (delete in C++) and that is it.
The rest of C++ goes on like before.
>
>>The main weakness of the malloc/free system is that it doesn't scale.
It scales when you use appropriate, well-known idioms like symmetric
*alloc and free calls, or high-level solutions like obstacks.
Those solutions are difficult at best.
You need to pass pointers around and store them never forgetting to free
them, etc. Or you impose yourself a HEAVY discipline that cripples your
ability to store pointers freely somewhere to use them as needed.
Example:
Thread A allocates a buffer to display some message. It passes
that message to the thread B, that handles the user interface,
displays strings, asks for input etc. Thread B is not connected
to thread A and calls are asynchronous, using a message passing
interface.
Thread B, then, must free the buffer. But then you must ensure
that all messages are allocated and that no calls are done like:
PostMessageToTh readB("Please enter the file name");
because when thread B attempts to free that it will crash.
Of course YOU know this and YOU will not do this type of call,
but when you were in Arizona in a customer place, programmer Z
had to add a message to the code because customer Y needed a fix and
DID NOT KNOW about your conventions...
Those are examples of *real* life, and software construction is
like that, as you know very well.
It is easy to say here:
"Just use some discipline", but it is MUCH HARDER to keep that
in real life.
>
>>It is impossible to be good at doing a mind numbing task without any error 100% of the time. You can be good at it, you can be bad at it, but you can NEVER be perfect. It is human nature.
You have good tools on most platforms to detect the errors, despite
'human nature'.
>>The GC frees you from those problems, and allows you to conecntrate in the problems that really matter, and where you can show your strength as software designer. It frees you from the boring task of keeping track of each memory block you allocate.
GC handles only one resource, memory. Other resources in a program eg.
file handles, database connections, locks, etc. still need to be
handled by the programmer.
Well, it will not make your coffee anyway :-)
There is a feature of the GC that allows to call a "destructor "
function, when an object will be destroyed. I haven't talked
about it because I consider it dangerous, since it is not really
a destructor, it will not be called when the variable goes out
of scope, but when the GC discovers that it is no longer used, what
can be MUCH later.
If you prefer GC in C, go for it. But your
code becomes dependant on a GC and therefore non-portable and
non-reusable (without that GC).
Best regards,
Roland Pibinger
Well if you feel like, you can always add the calls to free(), but
then, you would see that it is quite a BORING THING TO DO...
jacob navia said:
Bob Martin wrote:
>in 700822 20061011 175810 Richard Heathfield <in*****@invali d.invalid> wrote:
>>>jacob navia said:
Why a Garbage Collector? ----------------------- Standard C knows only the malloc/calloc/free functions.
Quite so. Please move discussions of non-C matters to some other newsgroup where it is topical.
Are you telling Jacob to stay out of your playpen?
I do not know what heathfield has against the GC.
What makes you think I have anything against GC? I have no more objection to
Garbage Collection than I have to tuna, Dusty Springfield, skateboarding,
the tiny little sequins you get on ballgowns, or the Metropolitan District
of South Humberside. None of them happens to be my cup of tea, but I have
no objection to their continued existence. Nor do I have any desire to stop
people talking about them. All I ask is that you do it some place where
they are topical. Automatic garbage collection is no more topical in
comp.lang.c than banana custard or the Falkland Islands.
Why have this limited view of C, where any deviation from
the holy scriptures is considered an heresy?
This is nothing to do with holy scripture, nothing to do with heresy, and
everything to do with topicality.
This group is about discussions of the C language, and
memory allocation strategies are very important.
This group is indeed for discussing the c language, and nuclear defence
strategies are very important. That does not make nuclear defence
strategies topical in comp.lang.c.
Why can't we discuss it here?
You *are* discussing it here. I'm asking you not to, because it's not
topical here. You are free to disagree with me, of course. And I'm free to
think of you as an ignorant bozo with no clue and no brain. Ain't freedom
wonderful?
Because there is no GC in the ISO-Standard?
Nonsense.
Really? Okay, I'll bite - show me where the ISO C Standard defines the
behaviour of GC_malloc.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Richard Heathfield wrote:
You are free to disagree with me, of course. And I'm free to
think of you as an ignorant bozo with no clue and no brain.
Please do not write that heathfield. Mr Thompson will immediately
say that I am insulting you...
jacob navia said:
Richard Heathfield wrote:
>You are free to disagree with me, of course. And I'm free to think of you as an ignorant bozo with no clue and no brain.
Please do not write that heathfield.
Firstly, I didn't say I *do* think of you as an ignorant bozo with no clue
and no brain. I said I'm free to think of you as an ignorant bozo with no
clue and no brain. Whether I *do* think of you as an ignorant bozo with no
clue and no brain depends very much on how long it takes you to grasp the
concept of topicality.
Secondly, you have ignored other people's requests to you not to write some
stuff here in comp.lang.c, so why should anyone pay any attention to your
requests not to write some stuff here in comp.lang.c? You're the "anything
goes" guy, not me, so you should approve that people are free to write
anything they like here, including statements such as "I'm free to think of
you as an ignorant bozo with no clue and no brain".
Note that I maintain the important logical distinction between "I'm free to
think of you as an ignorant bozo with no clue and no brain" and "you're an
ignorant bozo with no clue and no brain". I have said the former but not
the latter, and the former is merely a statement about freedom, not a claim
that you are an ignorant bozo with no clue and no brain - for such a claim
would be in poor taste at best, and I have no desire to make a claim in
poor taste (such as, for example, the claim that you are an ignorant bozo
with no clue and no brain). That would not be polite.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
On Wed, 11 Oct 2006 19:02:28 +0000, Richard Heathfield
<in*****@invali d.invalidwrote:
Whether I *do* think of you as an ignorant bozo with no clue and no brain depends very much on how long it takes you to grasp the concept of topicality.
I can guess, based on the fact that Jacob has been posting off-topic
articles here since at least 2000. IMO, that's quite long enough.
--
Al Balmer
Sun City, AZ
Al Balmer said:
On Wed, 11 Oct 2006 19:02:28 +0000, Richard Heathfield
<in*****@invali d.invalidwrote:
>Whether I *do* think of you as an ignorant bozo with no clue and no brain depends very much on how long it takes you to grasp the concept of topicality.
I can guess,
I couldn't possibly comment. :-)
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
On Wed, 11 Oct 2006 18:52:54 +0200, jacob navia
<ja***@jacob.re mcomp.frwrote:
> Why a Garbage Collector? ----------------------- Standard C knows only the malloc/calloc/free functions.
Which is why your article is off-topic here. If you are proposing that
such a mechanism be added to C, try comp.std.c.
--
Al Balmer
Sun City, AZ
jacob navia wrote:
[inflammatory off-topic crap]
Ok, that's finally enough for a plonk.
Brian
jacob navia wrote:
William Hughes wrote:
>jacob navia wrote:
[...]
>>This is of no practical significance. Pointers aren't XORed in normal programs, and if you stay within the normal alignment requirements of the processor, everything works without any problems.
No, but I might well subtract 1 from my pointers to change the indexing.
This is allowed of course. Your pointer will be within the bounds
of the pointed-to object and that object will NOT be reclaimed since
there is (at least) one pointer to somewhere in it.
You missed William's point entirely.
p = malloc(N * sizeof *p) -1; /* I now have an array indexed by 1 */
Non-standard, but people do it. It means that the pointer is no longer
no longer points in to the object, so the GC will free it.
>I might well pass a pointer to a third party library and forget about it.
Who cares?
The GC will see it anyway, because the foreign library is part
of your executable.
How to you know the library won't do any of the things that break GC?
Such as paging information, including your pointer, out to disk? Or
compressing it? Or subtracting 1 as above to use it as an array indexed
from 1? IIRC this last is possible if it is a library simply
implementing stuff out of Numerical Recipes in C, something which is
quite possible.
>In my view, any risk is too much to solve a relatively minor problem, memory leaks, especially as GC can only defend against true memory leaks.
No, it defends against double free() too, since you never
call free() all the bugs associated with not calling it
or calling it more than once disappear...
Still does not solve all the other problems.
>If I keep allocating memory, remember it (so in theory I could use it), but don't use it GC is not going to help (now if GC could do anything about memory stomps ...) .
Well, if you grab memory and memory and memory and you forget
to use it, if you do not keep any pointers to it nothing will happen:
for (i=0; i<100; i++)
a = GC_malloc(100);
only the last block will be protected from the GC, since there is a
pointer to it (a). All others will be reclaimed since there are
no pointers to them.
You missed the point again. How about if it keeps adding stuff to a tree
and never deletes nodes that are no longer needed? Then there is always
a way to reach the pointers so they never get freed.
How about memory getting tight enough that some of the programs memory
is paged out to a swap file? Then the GC kicks in and forces those pages
to be reloaded forcing other pages to be swapped out and potentially
causing disk thrashing.
Finally, this is not the place for discussing GC as you well know. So I
am unlikely to post further in this thread even to correct any further
errors you make.
--
Flash Gordon
In article <4p************ @individual.net >,
Default User <de***********@ yahoo.comwrote:
>jacob navia wrote:
[inflammatory off-topic crap]
Ok, that's finally enough for a plonk.
About 93% of the "[inflammatory off-topic crap]" was written by
heathfield, so I assume that means you are plonking it. Good show!
Wise choice! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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!
|
by: Ganesh |
last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a
process without have access to the process code.
regards
Ganesh
|
by: Rick |
last post by:
Hi,
My question is.. if Lisp, a 40 year old language supports garbage
collection, why didn't the authors of C++ choose garbage collection for
this language? Are there fundamental reasons behind this? Is it because
C is generally a 'low level' language and they didn't want garbage
collection to creep into C++ and ruin everything?
Just wondering :)
|
by: Ville Voipio |
last post by:
I would need to make some high-reliability software
running on Linux in an embedded system. Performance
(or lack of it) is not an issue, reliability is.
The piece of software is rather simple, probably a
few hundred lines of code in Python. There is a need
to interact with network using the socket module,
and then probably a need to do something hardware-
related which will get its own driver written in
C.
|
by: Bob lazarchik |
last post by:
Hello:
We are considering developing a time critical system in C#. Our tool used in
Semiconductor production and we need to be able to take meaurements at
precise 10.0 ms intervals( 1000 measurement exactly 10 ms apart. In the
future this may decrease to 5ms ). I am concerned that if garbage
collection invokes during this time it may interfere with our measurement
results. I have looked over the garbage collection mechanism and see no...
| |
by: mike2036 |
last post by:
For some reason it appears that garbage collection is releasing an object
that I'm still using. The object is declared in a module and instantiated
within a class that is in turn instantiated by the mainline. The class that
instantiated the object in question is definitely still in existence at the
point garbage collection swoops in and yanks it out from under my processing.
Is there a way to ensure an instantiated object cannot be freed...
|
by: Goalie_Ca |
last post by:
I have been reading (or at least googling) about the potential addition
of optional garbage collection to C++0x. There are numerous myths and
whatnot with very little detailed information.
Will this work be library based or language based and will it be based
on that of managed C++? Then of course there are the finer technical
questions raised (especially due to pointer abuse). Is a GC for C++
just a pipe dream or is there a lot of work...
|
by: Johnny E. Jensen |
last post by:
Hellow
I'am not sure what to think about the Garbage Collector.
I have a Class OutlookObject, It have two private variables.
Private Microsoft.Office.Interop.Outlook.Application _Application = null;
Private Microsoft.Office.Interop.Outlook.NameSpace _Namespace = null;
The Constructor:
public OutlookObject()
|
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?
|
by: pushpakulkar |
last post by:
Hi all,
Is garbage collection possible in C++. It doesn't come as part of
language support. Is there any specific reason for the same due to the
way the language is designed. Or it is discouraged due to
some specific reason. If someone can give inputs on the same, it will
be of great help.
Regards,
Pushpa
|
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...
| |
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,...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
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();...
| |
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...
|
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
| |