Re: OT:C/C++ Opinion Poll
- From: "Wilco Dijkstra" <Wilco_dot_Dijkstra@xxxxxxxxxxxx>
- Date: Sun, 02 Dec 2007 13:44:13 GMT
"Darin Johnson" <darin@xxxxxxx> wrote in message
news:493753a3-c4f4-4a2d-97a2-b915f9c69119@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Nov 30, 3:40 pm, CBFalconer <cbfalco...@xxxxxxxxx> wrote:
I.e. programmer action is always needed. This means that the only
advantage to GC is that you set the pointer to NULL, rather than
passing it to free().
The word "only" is a bit too strong. Maybe it seems that
way with Java or similar things. But the original point of
GC was to first remove the memory allocation/deallocation
abstraction entirely in higher level languages. There is not
even any assignment to null needed to cause something
to be collected, just stop referring to the object (assign
something else to the variable, return from the routine that
was referring to it, etc). Languages like Smalltalk and
Lisp don't even have pointers, and some functional
languages with GC don't even have assignment...
Removing references is what was meant. This is done differently in
various languages. In imperative languages you'd need to assign
NULL to an object reference in order to remove it, in Lisp you'd need
to unlink a cell from a list etc.
If programmer action is required, then either the GC
is broken (ie, another attempt to use reference counting)
or the language well suited for GC. If an object is
unused, then a good GC will be able to collect it.
A GC can only collect objects that are not referenced, not objects that
are referenced but never used again. In order to collect these objects
you have to explicitly remove references to them. Once we're talking
about complicated datastructures with many references, you effectively
have to do all collection manually.
In other words, a GC can only automatically collect a subset of the calls
to free. Even replacing all calls to free with a null pointer assignment
doesn't make a GC as powerful as malloc/free...
Second, GC can give a performance boost. Most good
garbage collectors need to move memory and do this
in a way that also happens to locality of reference. That
is caching and swapping are more efficient. With
generational scavangers there can be less time CPU
spent doing GC over the life of a program than would
be spent in explicit alloc/free. Fragmentation is
eliminated as well.
A moving generational GC is indeed a lot better than the mark&sweep
style (which effectively uses malloc) but has higher runtime overhead. For
example every pointer assignment has to update the state of the object, and
compiler optimization is often severely constrained. GC's typically have a
higher per-object overhead than malloc, and need more memory to allow
for less frequent collections, so I'd find it hard to believe you get significant
locality benefits.
The snags though are that the run time system for a good GC
system requires virtual memory support, and of course a
language with a high level abstraction. C++ is a low level
language, so GC doesn't really fit (though some people have
tried to retrofit GC which seems a bit misguided I think).
Agreed. If you do use GC, you need to design the whole system (language,
runtime, OS integration) around it, or it'll be far from optimal.
Wilco
.
- Follow-Ups:
- Re: OT:C/C++ Opinion Poll
- From: Pertti Kellomäki
- Re: OT:C/C++ Opinion Poll
- From: Dombo
- Re: OT:C/C++ Opinion Poll
- References:
- Re: OT:C/C++ Opinion Poll
- From: Jyrki Saarinen
- Re: OT:C/C++ Opinion Poll
- From: Wilco Dijkstra
- Re: OT:C/C++ Opinion Poll
- From: CBFalconer
- Re: OT:C/C++ Opinion Poll
- From: Darin Johnson
- Re: OT:C/C++ Opinion Poll
- Prev by Date: Re: 'Simple' stack problem
- Next by Date: Re: Explanation of the whole 300 mA thing
- Previous by thread: Re: Garbage Collection in C or C++ (was: OT:C/C++ Opinion Poll)
- Next by thread: Re: OT:C/C++ Opinion Poll
- Index(es):
Relevant Pages
|