原文:http://stackoverflow.com/questions/13574552/d-programming-without-the-garbage-collector

  1. D can use pretty much any C library, just define the functions needed. D can also use C++ libraries, but D does not understand certain C++ constructs. So... D can use almost as many libraries as C++. They just aren't native D libs.

  2. From D's Library reference.
    Core.memory:

    static nothrow void disable();
    

    Disables automatic garbage collections performed to minimize the process footprint. Collections may continue to occur in instances where the implementation deems necessary for correct program behavior, such as during an out of memory condition. This function is reentrant, but enable must be called once for each call to disable.

    static pure nothrow void free(void* p);
    

    Deallocates the memory referenced by p. If p is null, no action occurs. If p references memory not originally allocated by this garbage collector, or if it points to the interior of a memory block, no action will be taken. The block will not be finalized regardless of whether the FINALIZE attribute is set. If finalization is desired, use delete instead.

    static pure nothrow void* malloc(size_t sz, uint ba = 0);
    

    Requests an aligned block of managed memory from the garbage collector. This memory may be deleted at will with a call to free, or it may be discarded and cleaned up automatically during a collection run. If allocation fails, this function will call onOutOfMemory which is expected to throw an OutOfMemoryError.

    So yes. Read more here: http://dlang.org/garbage.html

    And here: http://dlang.org/memory.html

    If you really need classes, look at this: http://dlang.org/memory.html#newdelete delete has been deprecated, but I believe you can still free() it.

  3. Don't use classes, use structs. Structs are stack allocated, classes are heap. Unless you need polymorphism or other things classes support, they are overhead for what you are doing. You can use malloc and free if you want to.

  4. More or less... fill out the function definitions here: https://github.com/D-Programming-Language/druntime/blob/master/src/gcstub/gc.d . There's a GC proxy system set up to allow you to customize the GC. So it's not like it is something that the designers do not want you to do.

Little GC knowledge here: The garbage collector is not guaranteed to run the destructor for all unreferenced objects. Furthermore, the order in which the garbage collector calls destructors for unreference objects is not specified. This means that when the garbage collector calls a destructor for an object of a class that has members that are references to garbage collected objects, those references may no longer be valid. This means that destructors cannot reference sub objects. This rule does not apply to auto objects or objects deleted with the DeleteExpression, as the destructor is not being run by the garbage collector, meaning all references are valid.

import std.c.stdlib; that should have malloc and free.

import core.memory; this has GC.malloc, GC.free, GC.addroots, //add external memory to GC...

strings require the GC because they are dynamic arrays of immutable chars. ( immutable(char)[] ) Dynamic arrays require GC, static do not.

If you want manual management, go ahead.

import std.c.stdlib;
import core.memory;char* one = cast(char*) GC.malloc(char.sizeof * 8);.
GC.free(one);//pardon me, I'm not used to manual memory management.
//I am *asking* you to edit this to fix it, if it needs it.

why create a wrapper class for an int? you are doing nothing more than slowing things down and wasting memory.

class Foo { int n; this(int _n){ n = _n; } }
writeln(Foo.sizeof);  //it's 8 bytes, btw
writeln(int.sizeof);  //Its *half* the size of Foo; 4 bytes.Foo[] m;// = new Foo[n]; //8 sec
m.length=n; //7 sec minor optimization. at least on my machine.
foreach(i; 0..n)m[i] = new Foo(i);int[] m;
m.length=n; //nice formatting. and default initialized to 0
//Ooops! forgot this...
foreach(i; 0..n)m[i] = i;//.145 sec

If you really need to, then write the Time-sensitive function in C, and call it from D. Heck, if time is really that big of a deal, use D's inline assembly to optimize everything.

Dlang如何禁用垃圾回收(GC)相关推荐

  1. 垃圾回收(GC)浅谈

    关于内存 计算机通过两个机制,去实现内存的高效使用. 第一种机制是虚拟内存.硬盘的容量其实是远远大于内存的(RAM),虚拟内存会在内存不足的时候,把不经常访问的内存的数据写到硬盘里.虽然说硬盘容量比较 ...

  2. 【转】深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing) 第六节 理解垃圾回收GC,提搞程序性能****

    前言 虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC).另外,了解内存管理可以帮助我们理解在每一个程序中 ...

  3. python进阶19垃圾回收GC

    原创博客链接:python进阶19垃圾回收GC 垃圾收集三大手段 一.引用计数(计数器) Python垃圾回收主要以引用计数为主,分代回收为辅.引用计数法的原理是每个对象维护一个ob_ref,用来记录 ...

  4. python垃圾回收离职_垃圾回收gc.md

    垃圾回收gc python的垃圾收回机制不想c和c++是开发者自己管理维护内存的,python的垃圾回收是系统自己处理的,所以作为普通的开发者,我们不需要关注垃圾回收部分的内容,如果想要深层次理解py ...

  5. 垃圾回收GC经典算法

    目录 垃圾回收GC(Garbage Collection) 1.什么是垃圾 2.为什么要有GC 经典的GC算法 1.基本的一些概念 2.标记清除算法(Mark and Sweep) 3.复制法(cop ...

  6. IBM JDK(J9)垃圾回收(GC)策略

    在IBM JDK 1.5之后,采用了如下的垃圾回收GC策略: 针对吞吐量进行优化 -Xgcpolicy:optthruput(可选) 默认策略.对于吞吐量比短暂的 GC 停顿更重要的应用程序,通常使用 ...

  7. .net C# 堆 栈 垃圾回收 GC

    .NET C# .NET C# .NET C# .NET C# .NET C# .NET C# .NET C# 栈 堆 垃圾回收 GC #1 尽管在.NET framework下我们并不需要担心内存管 ...

  8. 第十五章: 菱悦 -垃圾回收GC详解

    第 15章 垃圾回收GC详解 文章目录 第 15章 垃圾回收GC详解 1.System.gc() 的理解 1.1.System.gc() 方法 1.2.不可达对象回收行为 2.内存溢出与内存泄漏 2. ...

  9. java using idispose_c# 垃圾回收(GC)优化

    GC,Garbage Collect,中文意思就是垃圾回收,指的是系统中的内存的分配和回收管理.其对系统性能的影响是不可小觑的.今天就来说一下关于GC优化的东西,这里并不着重说概念和理论,主要说一些实 ...

  10. Golang——垃圾回收GC

    Go 垃圾回收原理 Golang源码探索(三) GC的实现原理 引用计数:对每个对象维护一个引用计数,当引用该对象的对象被销毁时,引用计数减1,当引用计数器为0是回收该对象. 优点:对象可以很快的被回 ...

最新文章

  1. Android开发问题集锦
  2. Android开发环境搭建及常见问题解决方法
  3. vue从入门到精通之进阶篇(二)组件通信:兄弟组件通信
  4. asp.net core mvc View Component 应用
  5. Java垃圾收集器:G1GC何时将CMS强制退出?
  6. 全国计算机准考证打印2015年,甘肃2015下半年全国计算机等级考试准考证打印时间...
  7. JS判断视频Video的播放、暂停、结束完成及获取长度事件监听处理
  8. found.000是什么,能删除吗
  9. 新书推荐 | Java核心技术卷Ⅰ:基础知识(原书第11版)
  10. 台达plc自由口通讯_台达PLC串行通讯及应用案例
  11. JS复制input内容
  12. 2011微软校园招聘笔试题
  13. “百度杯”CTF比赛 十一月场Fuzz
  14. 『与善仁』Appium基础 — 3、移动端测试环境搭建(三)之AVD模拟器安装
  15. 计算机网络学习16:以太网交换机自学习与帧转发流程、生成树协议STP
  16. 细粒度分析综述(Fine-grain image analysis)
  17. linux系统源码安装dosbox,Linux-dosbox使用
  18. 工作票应一式两份 用计算机,工作票应使用统一的票面格式,釆用()、()或()填写,至少一式两份。...
  19. jst数组方法速查手册(记录)
  20. 字符串匹配问题(前缀表实现kmp、bmh、指纹思想)

热门文章

  1. 饭后Android 第二餐-复选框CheckBox+开关按钮Switch+单选按钮RadioButton
  2. 华为奋力前行,以灵活多变的方式突围,成效显著
  3. ijkPlayer点播/直播/VR视频播放器(解码器)研究(2)-Android
  4. from functools import reduce——从典型实例做抓手看reduce函数使用
  5. 基于图像的场景三维建模
  6. 王彦霖艾佳妮婚纱大片,校园牵手漫步,女方秀心形婚戒
  7. smbian c++生成sis文件日记
  8. ubuntu 屏幕亮度无法调节
  9. android camera hal3 新增vendor tag
  10. 《高等代数学》(姚慕生),例1.5.10