Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2019-08

Making use of mremap

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Florian Klämpfl

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.

Reply by email

Jonas Maebe
to Florian Klämpfl
Florian Klämpfl wrote 6 lines
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.

I've also added support for Darwin.

Jonas

Reply by email