原文:http://stackoverflow.com/questions/13574552/d-programming-without-the-garbage-collector
-
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.
-
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.
-
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.
-
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.
相关推荐
Java垃圾回收GC机制 Java垃圾回收GC机制是Java虚拟机(JVM)中的一种自动内存管理机制,它主要是为了解决Java程序中的内存问题。GC机制的出现使得Java程序员不再需要手动管理内存,提高了开发效率和程序的稳定性。 ...
C#的垃圾回收机制(GC)是.NET框架中一个至关重要的特性,它负责自动管理和释放内存,从而减轻程序员的工作负担。GC的主要目标是回收不再使用的对象所占用的内存,以防止内存泄漏。在这个机制下,程序员无需手动管理...
2. Compact(压缩):回收垃圾后,将存活对象移动到堆的起始位置,使其连续排列,以便更有效地利用内存空间。 在.NET中,GC执行过程会暂停所有线程,确保内存状态的一致性。它会查找各种类型的根,包括全局变量、...
Java虚拟机(JVM)的垃圾回收(GC)机制是Java程序高效运行的关键部分,它自动管理内存,释放不再使用的对象以避免内存泄漏。本文主要探讨JVM堆内存的结构和GC的工作原理,以及如何进行性能调优。 JVM堆是Java应用...
Java垃圾回收GC.xmind
Java垃圾回收(GC)机制是Java语言管理内存的自动化机制,它能够自动释放不再使用的内存空间,从而避免内存泄漏和程序崩溃等问题。在介绍Java GC机制之前,我们首先要了解垃圾回收的目的和意义。在任何程序中,内存...
### Java GC垃圾回收调优指南 #### 概述 在Java开发过程中,垃圾回收(Garbage Collection, GC)是管理内存资源的关键技术之一。合理的GC配置可以显著提高应用程序的性能和稳定性。本指南旨在帮助开发者深入理解...
Java垃圾回收(GC)机制是Java编程语言中的一个重要特性,它自动管理程序的内存空间,负责识别并清除不再使用的对象,以防止内存泄漏。本文将深入探讨Java GC的工作原理、类型、过程以及优化策略。 一、Java垃圾...
在.NET Framework环境中,虽然开发人员不必直接管理内存和处理垃圾回收(Garbage Collection, GC),但了解这些底层机制对于优化应用程序至关重要。这不仅能提升程序的性能,还能帮助开发者更好地理解程序变量的行为...
NETC#栈堆垃圾回收GC借鉴.pdf
Java垃圾回收(Garbage Collection, 简称GC)是JVM(Java Virtual Machine)管理内存的重要机制。在Java应用程序运行过程中,如果发现并回收不再使用的对象,可以防止内存泄漏,确保程序稳定运行。当我们遇到应用...
垃圾回收(Garbage Collection, GC)是指在程序运行过程中自动检测并释放那些不再被使用的对象所占用的内存空间的过程。这一过程通常是由语言的运行环境(如Java虚拟机)自动完成的,而不需要程序员手动干预。 **2....
GC 按代回收垃圾也是出于性能考虑的;通常的对象都会在 0 代是被回收。 从代的角度看,大对象属于 2 代对象,因为只有在 2 代回收时才会处理大对象。当某代垃圾回收执行时,会同时执行更年轻代的垃圾回收。比如:当...
迷你垃圾回收器(Mini GC for C)是一种针对C语言实现的轻量级内存管理工具,主要功能是自动回收不再使用的内存,以防止内存泄漏。在C语言中,程序员需要手动管理内存分配和释放,而垃圾回收器则可以自动化这一过程...
GC总结
垃圾回收(Garbage Collection, GC)是编程语言中用于自动管理内存的重要机制,尤其是在像Java、C#等语言中。这个技术示例旨在深入探讨垃圾回收的原理、工作流程以及在实际开发中的应用。 垃圾回收的基本目标是识别...
### 垃圾回收机制——GC 在 ASP.NET 4.0 中的应用 #### GC介绍 垃圾回收机制(Garbage Collection, 简称GC)是现代编程语言中一种自动化的内存管理方式。它通过自动追踪并回收不再使用的内存,帮助开发者避免了...
Java虚拟机(JVM)的垃圾回收(Garbage Collection, GC)是Java编程语言中一个重要的特性,它自动管理程序的内存,确保无用的对象被有效地释放,避免内存泄漏。理解JVM垃圾回收机制对于优化Java应用性能至关重要。 ...
JVM GC垃圾回收.pdfJVM GC垃圾回收.pdfJVM GC垃圾回收.pdfJVM GC垃圾回收.pdfJVM GC垃圾回收.pdf
其中,垃圾回收机制(Garbage Collection, GC)是Java虚拟机(JVM)的一项重要特性,它能够自动检测并回收不再使用的对象占用的内存空间,从而有效避免了内存泄漏问题。本文将详细介绍Java中的垃圾回收机制及其工作原理...