garbage collector question
Dave
Dave_member at pathlink.com
Tue Jul 24 09:49:14 PDT 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.
More information about the Digitalmars-d-learn
mailing list