Recent

Author Topic: mem size  (Read 761 times)

Paolo

  • Hero Member
  • *****
  • Posts: 581
mem size
« on: March 08, 2025, 01:31:44 pm »
Hello

with this code (win-64, FPC 3.2.2)

Code: Pascal  [Select][+][-]
  1. type
  2.   PntDouble = ^double;
  3.  
  4. procedure TForm1.Button3Click(Sender: TObject);
  5. var
  6.   P : PntDouble;
  7.   V : QWord;
  8.   S, i : integer;
  9. begin
  10.   S:=SizeOf(Double);
  11.   for i:=1 to 16 do begin
  12.     GetMem(P, i*S);
  13.     V:=MemSize(P);
  14.     Memo1.Lines.Add(V.ToString);
  15.   end;
  16. end;
  17.  

I have this as output.
24
24
24
56
56
56
56
88
88
88
88
120
120
120
120
152

tha was for me unexpected result.

what I missed ?

I was expecting 8 byte more after any iteration....
8
16
32
etc...

LV

  • Sr. Member
  • ****
  • Posts: 250
Re: mem size
« Reply #1 on: March 08, 2025, 01:46:42 pm »

Paolo

  • Hero Member
  • *****
  • Posts: 581
Re: mem size
« Reply #2 on: March 08, 2025, 02:01:34 pm »
@LV
I have just updated the multiplication matrix routines with dynamically allocate memory, everything seems working fine, but once I checked with memSize I was confused.
I'll look at the link you provided.

LV

  • Sr. Member
  • ****
  • Posts: 250
Re: mem size
« Reply #3 on: March 08, 2025, 02:07:47 pm »
I would like to add that the result depends on the selected memory manager, as you are currently using the default memory manager. Try it

uses
 cmem;

Code: Text  [Select][+][-]
  1. 8
  2. 16
  3. 24
  4. 32
  5. 40
  6. 48
  7. 56
  8. 64
  9. 72
  10. 80
  11. 88
  12. 96
  13. 104
  14. 112
  15. 120
  16. 128
  17.  

uses
 heaptrc;

Code: Text  [Select][+][-]
  1. 8
  2. 16
  3. 24
  4. 32
  5. 40
  6. 48
  7. 56
  8. 64
  9. 72
  10. 80
  11. 88
  12. 96
  13. 104
  14. 112
  15. 120
  16. 128
  17.  


jamie

  • Hero Member
  • *****
  • Posts: 6880
Re: mem size
« Reply #4 on: March 08, 2025, 04:04:36 pm »
Old problem. Don't use memsize, it's not exactly what u allocated at all times.
Its more for internal use.
The only true wisdom is knowing you know nothing

Paolo

  • Hero Member
  • *****
  • Posts: 581
Re: mem size
« Reply #5 on: March 08, 2025, 04:25:27 pm »
I saw...

thanks to all.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5940
  • Compiler Developer
Re: mem size
« Reply #6 on: March 08, 2025, 05:23:34 pm »
Code: Pascal  [Select][+][-]
  1. type
  2.   PntDouble = ^double;
  3.  
  4. procedure TForm1.Button3Click(Sender: TObject);
  5. var
  6.   P : PntDouble;
  7.   V : QWord;
  8.   S, i : integer;
  9. begin
  10.   S:=SizeOf(Double);
  11.   for i:=1 to 16 do begin
  12.     GetMem(P, i*S);
  13.     V:=MemSize(P);
  14.     Memo1.Lines.Add(V.ToString);
  15.   end;
  16. end;
  17.  

Please note that aside from the notes regarding MemSize this code will leak memory. You should better use ReallocMem as that will properly either grow the allocated memory if possible or safely move it to a new, larger location. And don't forget a FreeMem at the end. ;)

Paolo

  • Hero Member
  • *****
  • Posts: 581
Re: mem size
« Reply #7 on: March 08, 2025, 05:32:24 pm »
Sure Freemem is there in the real code,

thanks

LV

  • Sr. Member
  • ****
  • Posts: 250
Re: mem size
« Reply #8 on: March 08, 2025, 08:16:17 pm »
Please note that aside from the notes regarding MemSize this code will leak memory. You should better use ReallocMem as that will properly either grow the allocated memory if possible or safely move it to a new, larger location. And don't forget a FreeMem at the end. ;)

Let's try using the default memory manager first.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. //uses
  4.   //cmem;
  5.   //heaptrc;
  6.  
  7. var
  8.   P: Pointer;
  9.   V, S, i: PtrUInt;
  10.  
  11. begin
  12.  
  13.   S := 8;
  14.   GetMem(P, S);
  15.   for i := 1 to 16 do
  16.   begin
  17.     ReallocMem(P, i * S);
  18.     V := MemSize(P);
  19.     writeln('i * S = ', i * S, '   V = ', V);
  20.   end;
  21.   Freemem(P, V);
  22.  
  23.   Readln;
  24.  
  25. end.  
  26.  

The default memory manager:
Code: Text  [Select][+][-]
  1. i * S = 8   V = 24
  2. i * S = 16   V = 24
  3. i * S = 24   V = 24
  4. i * S = 32   V = 88
  5. i * S = 40   V = 88
  6. i * S = 48   V = 88
  7. i * S = 56   V = 88
  8. i * S = 64   V = 88
  9. i * S = 72   V = 88
  10. i * S = 80   V = 88
  11. i * S = 88   V = 88
  12. i * S = 96   V = 216
  13. i * S = 104   V = 216
  14. i * S = 112   V = 216
  15. i * S = 120   V = 216
  16. i * S = 128   V = 216
  17.  
:(

Then, we will use the cmem memory manager, noting that heaptrc is the same:
Code: Text  [Select][+][-]
  1. i * S = 8   V = 8
  2. i * S = 16   V = 16
  3. i * S = 24   V = 24
  4. i * S = 32   V = 32
  5. i * S = 40   V = 40
  6. i * S = 48   V = 48
  7. i * S = 56   V = 56
  8. i * S = 64   V = 64
  9. i * S = 72   V = 72
  10. i * S = 80   V = 80
  11. i * S = 88   V = 88
  12. i * S = 96   V = 96
  13. i * S = 104   V = 104
  14. i * S = 112   V = 112
  15. i * S = 120   V = 120
  16. i * S = 128   V = 128
  17.  
:)

P.S. Memsize is a documented function  https://www.freepascal.org/docs-html/rtl/system/memsize.html
« Last Edit: March 08, 2025, 08:21:08 pm by LV »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5940
  • Compiler Developer
Re: mem size
« Reply #9 on: March 11, 2025, 09:28:47 pm »
P.S. Memsize is a documented function  https://www.freepascal.org/docs-html/rtl/system/memsize.html

And did you read the documentation?

Quote
MemSize returns the size of a memory block on the heap.

It does not say that it will be the same size that you passed to the GetMem or ReallocMem function. If MemSize is larger than the allocated size then the heap manager will very likely use the remaining space for growing it when ReallocMem is used.

Though all of this highly depends on the used heap manager and thus should be considered an implementation detail of the heap manager.

LV

  • Sr. Member
  • ****
  • Posts: 250
Re: mem size
« Reply #10 on: March 12, 2025, 01:01:54 am »
@PascalDragon, thanks for the clarification. I have read the documentation and the issue mentioned at https://gitlab.com/freepascal.org/fpc/documentation/-/issues/39403.
Latest reply:
Quote
"Michael Van Canneyt @mvancanneyt · 4 months ago Owner
I tested on linux. The numbers are indeed different, which is of course a bug...
Please file a bug report in the FPC source repository, so the behavior can be corrected.
Once corrected, I will adapt the documentation accordingly."

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12161
  • FPC developer.
Re: mem size
« Reply #11 on: March 12, 2025, 09:54:24 am »
Pascaldragon's remarks is also what I remember. This is used to decide if a memblock can be enlarged without reallocating (e.g. lengthening a string).

The numbers are memory manager dependent.

 

TinyPortal © 2005-2018