GC and slices

Lionello Lunesu lio at lunesu.remove.com
Thu Sep 21 23:59:30 PDT 2006


> When you try to append to a slice a reallocation occurs--growing an 
> array in place is only possible if your array reference refers to the 
> head of the memory block.  Also, a rellocation will occur on an append 
> periodically as the as the block grows.  Up to 4096 bytes (the size of a 
> memory page) reallocations will occur when the array passes a power of 
> two boundary--16, 32, 64, etc--beyond 4096 bytes a reallocation will 
> occur when the array passes a page boundary.  This has to do with how 
> the GC manages memory, and it is a pretty typical allocator design.

Hi Sean,

Thanks for the explanation.. I was thinking about it some more and 
indeed forgot to test the pointer value after the append :S Pretty 
stupid of me.

But a thought occured. Reallocation often involves a copy, but why 
should that copy be necessary? I mean, the CPU (286+ anyway) has this 
mapping table from physical to virtual memory. Wouldn't it be possible 
to allocate a piece of virtual memory, but to let the 'old' pages refer 
to the old physical memory so no copy is needed? Is this possible in 
Windows/Linux?

(In windows there's VirtualAlloc, which can be used to grow an existing 
virtual memory block but I pretty sure that there's a memory copy if 
that block can not be grown when a new block of virtual address space is 
reserved.)

L.



More information about the Digitalmars-d mailing list