关闭D垃圾收集器 [英] Turning off the D garbage collector

查看:125
本文介绍了关闭D垃圾收集器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名C ++程序员,他们考虑将D用于我想要玩的个人项目。我想知道是否有办法彻底禁用垃圾回收器,以及这样做的风险是什么。

我知道我可以通过覆盖新的和删除来使用malloc和free来管理自己的内存,但是如果我这样做了,我宁愿垃圾收集器也不会运行

解决方案

要在D2中关闭GC:

  import core.memory; 

void main(string [] args){
GC.disable;
//做东西。
}

如果使用D1 / Phobos:

  import std.gc; 

void main(char [] [] args){
std.gc.disable;
//做东西。

在D1 / Tango中:

  import tango.core.Memory; 

void main(char [] [] args){
GC.disable;
//做东西。





$ b GC可以通过调用GC.enable(D2或D1 /探戈)或std.gc.enable(D1 / Phobos)。这些可以在程序中的任何位置完成。在内部使用计数器,并且为了实际重新启用GC,每次调用disable()时都必须调用enable()一次。

以下是一些不适用于禁用GC,因为它们会导致内存泄漏:


  1. 不要使用数组追加(〜=)运算符,或者使用。 length属性来放大已分配的数组。如果必须重新分配旧数组,那么这些依赖于GC来释放旧数组,因为可能会在程序的其他位置将其别名。
  2. 不要使用内置关联数组。解决这些问题的唯一方法是通过GC。

  3. 大多数Phobos和我相信Tango都是在垃圾收集存在的假设下设计的。如果不使用GC,这些库中的函数可能会严重泄漏内存。

  4. 不禁用禁用GC的D2闭包。 (并不是说你会这样做)。

也就是说,虽然D被设计为可以在GC禁用的情况下使用一些关键的代码片段(实时约束存在的关键片段类型,你可能不应该使用任何形式的malloc,而不是明确设计用于实时计算),但它的设计主要是假设GC将是当下。在你的情况下,你仍然可以使用GC来处理所有的初始化内容等,只有当你点击游戏中实际需要的实时时才会禁用它。



作为一个侧面说明,GC和手动内存管理可以共存于D中,并且在实践中,当优化代码时,手动删除一些具有微不足道的生命周期的大对象可能会导致显着的加速。这可以通过使用delete语句与C ++类似地完成,并且即使启用GC也是安全的。如果您没有实时限制,则可以为您提供GC的大部分优势,包括大部分手动内存管理性能。


I'm a C++ programmer thats considering using D for a personal project I want to play around with. I was wondering if there's a way to completely disable the garbage collector, and what the risks are of doing so.

I know I can manage my own memory by overriding new and delete to use malloc and free, but if I did that I'd rather the garbage collector not run at all.

解决方案

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:

  1. 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.
  2. Do not use builtin associative arrays. The only way to free these is by GC.
  3. 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.
  4. 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.

这篇关于关闭D垃圾收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆