[fpc-devel] Making use of mremap

Florian Klämpfl florian at freepascal.org
Fri Aug 16 22:55:10 CEST 2019


Recently, there was some discussion on problems with memory 
re-allocations. So in r42713 I implemented the use of the mremap syscall 
on linux. It basically means that a memory block is re-allocated with 
the help of the OS if possible. The OS can do this much better as it can 
move memory by just remapping the physical pages at other locations in 
the virtual memory. So even huge memory blocks can be re-allocated 
quickly under certain conditions. A small test program shows the result 
(x86_64-linux, i386 and arm tested as well):

var
   a : array[0..3] of pointer;
   i,j : longint;
   HeapStatus : THeapStatus;
begin
   randomize;
{$if not(defined(CPU8)) and not(defined(CPU16))}
   for i:=1 to 200 do
     begin
       j:=random(length(a));
       if not(assigned(a[j])) then
         getmem(a[j],1024*1024)
       else
         reallocmem(a[j],MemSize(a[j])*11 div 10);
     end;
   for i:=0 to high(a) do
     freemem(a[i]);
   HeapStatus:=GetHeapStatus;
   with HeapStatus do
     begin
       writeln('TotalAllocated: ',TotalAllocated);
       writeln('TotalFree: ',TotalFree);
     end;
{$endif not(defined(CPU8)) and not(defined(CPU16))}
end.

with 3.0.4:
time ./theap2
TotalAllocated: 0
TotalFree: 0

real	0m3,899s
user	0m1,768s
sys	0m2,131s

and with 3.3.1:
$ time ./theap2
TotalAllocated: 0
TotalFree: 0

real	0m0,004s
user	0m0,004s
sys	0m0,000s

So this might reduce problems with re-allocations under certain 
circumstances.


More information about the fpc-devel mailing list