www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - garbage collector question

reply Hoenir <mrmocool gmx.de> writes:
I'm wondering when and how to "disable" the GC and do the memory 
mangement yourself?
Jul 24 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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();
Jul 24 2007
parent reply Hoenir <mrmocool gmx.de> writes:
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. :)
Jul 24 2007
next sibling parent janderson <askme me.com> writes:
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
Jul 24 2007
prev sibling parent Dave <Dave_member pathlink.com> writes:
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.
Jul 24 2007