Re: Garbage collection



"Bartc" <bc@xxxxxxxxxx> wrote in message news:Z%tQj.15440$yD2.11636@xxxxxxxxxxxxxxxxxxxxxxxxxxxx

"jacob navia" <jacob@xxxxxxxxxx> wrote in message news:fusi33$e0n$1@xxxxxxxxxxx

In an interviw with Dr Dobbs, Paul Jansen explains which languages are gaining in popularity and which not:

<quote>
DDJ: Which languages seem to be losing ground?

PJ: C and C++ are definitely losing ground. There is a simple explanation for this. Languages without automated garbage collection are getting out of fashion. The chance of running into all kinds of memory problems is gradually outweighing the performance penalty you have to pay for garbage collection.
<end quote>

lcc-win has been distributing a GC since 2004.

I've little idea about GC or how it works (not about strategies and things but how it knows what goes on in the program).

What happens in this code fragment:

void fn(void) {
int *p = malloc(1236);
}

when p goes out of scope? (Presumably some replacement for malloc() is used.)

A compiler can optmize this away. Even if you did:

extern fne(void* const);

void fn(void) {
void* const buf1 = malloc(1236);
if (buf1) {
void* const buf2 = malloc(1236);
if (buf2) {
L1: fne(buf1);
L2: fne(buf2);
}
}
}


If compiler can detect that 'fne()' does not cache a pointer somewhere, it can possibly detect that 'buf1' goes out of scope at 'L2'. The GC would need to run a cycle to act on this information. It would be nice if a compiler could inject calls to free:


void fn(void) {
void* const buf1 = malloc(1236);
if (buf1) {
void* const buf2 = malloc(1236);
if (buf2) {
L1: fne(buf1);
Inject: free(buf1);
L2: fne(buf2);
Inject: free(buf2);
}
}
}


And the compiler creates a new file, and I can inspect the calls to free to see if the compiler did a good job before I compile it. Would that be feasible?




What about a linked list when it's no longer needed? How does GC know that, do you tell it? And it knows where all the pointers in each node are. I can think of a dozen ways to confuse a GC so it all sounds like magic to me, if it works.

Some GC implementations that will actually scan the stack of all application threads for pointers into its managed heaps, and attempt to follow all the links to NULL. If there are no global pointers to an object and no local pointers in any threads stack, well, it means that its has been rendered into a persistent quiescent state.

.



Relevant Pages

  • Re: Scheme and libraries -- piggyback on Java?
    ... pointers to be assigned to other pointer types without casting. ... I suppose I did know at one time that void* was convertible without ... I also just realized that I haven't used an actual C compiler since ...
    (comp.lang.scheme)
  • Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!
    ... Best regards ... So now that I have gotten it to work in the VC++ compiler, ... mayby my compiler doesn't support pointers to pointers! ... from void* to other pointer types. ...
    (microsoft.public.vc.language)
  • Re: Is __int64 dead?
    ... Note also that while the compiler can implement native types efficiently, ... It makes very little sense to anyone on an architecture on which pointers ... Well, all pointers are convertible to void*, so make INT_PTR an integer ...
    (microsoft.public.vc.mfc)
  • Re: /DELAYLOAD Linux counterpart for shared objects
    ... A big disadvantage is that the compiler, ... pointers and makes sure the library is there). ... void, ... ub2 charset, ub2 ncharset); ...
    (comp.os.linux.development.apps)
  • Re: fields for methods?
    ... void A::foo{ ... but only by a compiler that is allowed to ... int static_instance i = 0; ... it totally breaks the idea of encapsulation, which is the reason a lot ...
    (comp.programming)