Thread overview
garbage collector question
Jul 24, 2007
Hoenir
Jul 24, 2007
Hoenir
Jul 24, 2007
janderson
Jul 24, 2007
Dave
July 24, 2007
I'm wondering when and how to "disable" the GC and do the memory mangement yourself?
July 24, 2007
"Hoenir" <mrmocool@gmx.de> wrote in message news:f84sio$22an$1@digitalmars.com...
> I'm wondering when and how to "disable" the GC and do the memory mangement yourself?

import std.gc;
std.gc.disable();


July 24, 2007
Jarrett Billingsley schrieb:
>> I'm wondering when and how to "disable" the GC and do the memory mangement yourself?
> 
> import std.gc;
> std.gc.disable(); 

I don't want to disable it completely. I'm just wondering when it is advantageous to do MM yourself and how to do it.
How to prevent a variable from being deleted by the GC and such stuff.
But thanks for your answer. :)
July 24, 2007
Hoenir wrote:
> Jarrett Billingsley schrieb:
>>> I'm wondering when and how to "disable" the GC and do the memory mangement yourself?
>>
>> import std.gc;
>> std.gc.disable(); 
> 
> I don't want to disable it completely. I'm just wondering when it is advantageous to do MM yourself and how to do it.

I haven't done this in D so I can't comment.

> How to prevent a variable from being deleted by the GC and such stuff.
> But thanks for your answer. :)

import std.gc;
//...
std.gc.disable();

//Do what ever non gc class allocation you want (you'll have to clean these up specifically yourself).

std.gc.enable();

You may want to do this for deterministic destruction, for optimization reason, or if you allocate from some sort of special memory.

-Joel
July 24, 2007
Hoenir wrote:
> Jarrett Billingsley schrieb:
>>> I'm wondering when and how to "disable" the GC and do the memory mangement yourself?
>>
>> import std.gc;
>> std.gc.disable(); 
> 
> I don't want to disable it completely. I'm just wondering when it is advantageous to do MM yourself and how to do it.
> How to prevent a variable from being deleted by the GC and such stuff.
> But thanks for your answer. :)

Hmmm, there are a few cases when the GC will not release memory because of pointer misidentification, and there are a couple of cases where the GC won't 'mark' a pointer.

Also, allocating in a tight loop is mostly faster with MM as it stands right now.

For example:

align(1)
struct PackedStruct
{
    short s;
    char *c;
}
//...
PackedStruct* s;
s = new PackedStruct;
s.c = new char[100]; // GC won't mark s.c unless you manually add it as a root

See more here: http://digitalmars.com/d/garbage.html

For non-OOP, basically you do manual MM the same as C w/ a slightly different syntax.

There's more here: http://digitalmars.com/d/memory.html

Short example to get you started:

;---

import std.c.stdlib;

void main()
{
    const ELEMS = 100;
    int* array = cast(int*)malloc(ELEMS * int.sizeof);
//  ...
    free(array);
    array = null;
}

HTH.