Memory allocation in D
Sean Kelly
sean at f4.ca
Thu Nov 29 09:01:17 PST 2007
Marius Muja wrote:
> Hi,
>
> I'm working on a program that is computationally and memory intensive. I
> have noticed that if I allocate the memory using D's new operator I can
> allocate only about half as much memory compared to C's malloc (and also
> the memory allocation is much slower). Does anybody know why D's memory
> allocator uses twice as much memory compared to malloc?
>
> I used the following test program to test this:
>
> version (Tango){
> import tango.core.Memory;
> import tango.stdc.stdlib;
> import tango.io.Stdout;
> }
> else
> {
> import std.stdio;
> import std.c.stdlib;
> }
>
> const int ALLOC_SIZE = 1024*1024;
>
> void test_d_alloc()
> {
> float[] a = new float[ALLOC_SIZE];
> }
With D as it is now, this will call GC.malloc(2049), which will allocate
one page of memory per call. The D runtime adds 1 to every allocation
with "new", according to Walter, this was to prevent certain confusing
errors. It will allocate a page per call because the D GC allocates
from pools devoted to fixed-size blocks which grow in powers of two up
to 4096 bytes (one page on most systems). Allocations beyond 4096 bytes
are "big" allocations, and a multiple of 4096 bytes will be allocated to
fit the requested size. For these allocations, when the memory is
released I believe the pages go back into a free pool. I'm not sure
offhand if empty pages used for smaller blocks do or not.
You might want to try calling GC.malloc() instead and compare results.
Sean
More information about the Digitalmars-d
mailing list