[fpc-pascal] Effective memory allocation
ne at erfurth.eu
Mon Nov 3 08:12:35 CET 2014
On 02.11.14 15:05, Xiangrong Fang wrote:
> Sorry, the results in previous mail was mis-labeled.
> The result is:
> Using SetLength:
> Alloc: 9.4078169999999997E-0001
> Clear: 2.1342020000000002E-0001
> Using GetMemory:
> Alloc: 2.8100000000000000E-0005
> Clear: 7.7497550000000004E-0001
SetLength will already clear the array before returning it, so the time
for SetLength is approximate time(GetMem) + time(Clear).
As your test allocates a gigabyte of memory the OS will not immediatly
assign physical memory to your process. The pagetable after a the
allocation will contain entries which create faults when accessing the
virtual addresses and then the OS will fill those pages in. So on first
access a lot of time will be spend inside the OSes memory subsystem.
For Linux the newly allocated memory will most likely point to a read
only shared physical page which only contains zeros. On the first write
to one of those virtual addresses the real page allocation will trigger
and Linux assigns a new empty physical page to the virtual memory area.
On Linux you can avoid this by passing MAP_POPULATE to mmap when
allocating the memory.
For SetLength this first access is already done when clearing the memory
inside SetLength. For GetMemory it is done when your explicitly access
the memory by calling FillQWord later.
More information about the fpc-pascal