Turning off the D garbage collector?

To turn off the GC in D2: import core. Memory; void main(string args) { GC. Disable; // Do stuff.

} If using D1/Phobos: import std. Gc; void main(char args) { std.gc. Disable; // Do stuff.

} In D1/Tango: import tango.core. Memory; void main(char args) { GC. Disable; // Do stuff.

} The GC can be reenabled similarly by calling GC. Enable (D2 or D1/Tango) or std.gc. Enable (D1/Phobos).

These can be done at any point in a program. Internally, a counter is used, and to actually reenable GC, you must call enable() once for every time disable() was called Here are some things not to do with GC disabled, because they will cause memory leaks: Do not use the array append ( ~= ) operator, or use the . Length property to enlarge an array that has already been allocated.

These rely on GC to free the old array if it has to be reallocated, since there may be aliasing to it somewhere else in the program Do not use builtin associative arrays. The only way to free these is by GC Most of Phobos and, I believe, Tango, were designed with the assumption that garbage collection is present. Functions in these libraries may leak memory horribly if used w/o GC Do not use D2 closures with GC disabled.(Not that you would anyway, for a game.) That said, while D is designed to be usable with the GC disabled in a few critical pieces of code (the kind of critical pieces where real time constraints exist and you probably shouldn't be using any form of malloc not explicitly designed for real time computing anyhow), it was largely designed with the assumption that GC would be present.

In your case, you can still use GC for all of the initialization stuff, etc. And only disable it when you hit the part of your game that actually needs to be real time As a side note, GC and manual memory management can coexist in D, and in practice, when optimizing code, manually deleting some large objects with trivial lifetimes can result in significant speedups. This can be done similarly to C++, using a delete statement, and is safe to do even if the GC is enabled. When you don't have real time constraints, this gives you most of the benefits of GC with most of the performance of manual memory management.

To turn off the GC in D2: import core. Memory; void main(string args) { GC. Disable; // Do stuff.

} If using D1/Phobos: import std. Gc; void main(char args) { std.gc. Disable; // Do stuff.

} In D1/Tango: import tango.core. Memory; void main(char args) { GC. Disable; // Do stuff.

} The GC can be reenabled similarly by calling GC. Enable (D2 or D1/Tango) or std.gc. Enable (D1/Phobos).

These can be done at any point in a program. Internally, a counter is used, and to actually reenable GC, you must call enable() once for every time disable() was called. Here are some things not to do with GC disabled, because they will cause memory leaks: Do not use the array append ( ~= ) operator, or use the .

Length property to enlarge an array that has already been allocated. These rely on GC to free the old array if it has to be reallocated, since there may be aliasing to it somewhere else in the program. Do not use builtin associative arrays.

The only way to free these is by GC. Most of Phobos and, I believe, Tango, were designed with the assumption that garbage collection is present. Functions in these libraries may leak memory horribly if used w/o GC.Do not use D2 closures with GC disabled.

(Not that you would anyway, for a game. ) That said, while D is designed to be usable with the GC disabled in a few critical pieces of code (the kind of critical pieces where real time constraints exist and you probably shouldn't be using any form of malloc not explicitly designed for real time computing anyhow), it was largely designed with the assumption that GC would be present.In your case, you can still use GC for all of the initialization stuff, etc. And only disable it when you hit the part of your game that actually needs to be real time. As a side note, GC and manual memory management can coexist in D, and in practice, when optimizing code, manually deleting some large objects with trivial lifetimes can result in significant speedups.

This can be done similarly to C++, using a delete statement, and is safe to do even if the GC is enabled. When you don't have real time constraints, this gives you most of the benefits of GC with most of the performance of manual memory management.

8 Thanks for answering the question I actually asked, instead of assuming that I don't know what I'm doing. – BigSandwich Jan 28 '09 at 22:20.

If you want to use malloc and free use std.c.stdlib. GC will never touch these. Std.

Gc has all the stuff you will need for memory management including disable(). GC isn't a bad thing though. Most if not nearly all libraries in D will have somewhere in the code where memory is not explicitly deleted so it won't make you a hero to have it allways off but ok if you have some critical performance requirement.

GC makes everything a lot more productive like array slicing and creating new objects in parameters without having the caller store a reference anywhere to them. Good code is a lot less of it and with GC code becomes a lot smaller.

There are language features that expect a GC, string concatenation and hashtables are a couple. I'm not sure what they all were. – he_the_great Jan 23 '09 at 15:43 One cases where turning off the GC would be valid is where you can show that you won't use up more ram than is to be had.

– BCS Jan 23 '09 at 16:31 I doubt I'll really be doing a lot of array slicing or string manipulation. Is there a list of language features that depend on the GC? Maybe I should just leave it on and then avoid those features.

– BigSandwich Jan 23 '09 at 19:29 @BCS GC is not really about amount of ram used so I would disagree with that yet does provide a nice safety net. – Tim Matthews Jan 23 '09 at 3:05 @BS: I would go with the route of leaving it on and avoiding things that need GC - seems safer than inadvertently leaking small amounts of memory over time. – Software Monkey Jan 23 '09 at 7:35.

The GC can be removed and replaced with a simple wrapper around malloc/free.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions