Recent

Author Topic: Size of memory  (Read 5325 times)

Okoba

  • Hero Member
  • *****
  • Posts: 555
Size of memory
« on: October 21, 2024, 03:19:55 pm »
Hi,

Can I ask why result of MemSize is different of what I asked from GetMem?
And is the memory of that size is safe to use?
Code: Pascal  [Select][+][-]
  1. WriteLn(MemSize(GetMem(1024)));
This code returns 1032 on Windows instead of 1024. Why? And can I use those 1032 bytes safely?

Also, what is the difference between MemSize and SysMemSize? I can not find anything about that in the documentation?
I can not find anything about SysFreememSize too, what it does?

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Size of memory
« Reply #1 on: October 21, 2024, 03:58:34 pm »
Its a managed memory block. The size you are getting is including the pointer size type that denotes the amount of memory that is allocated. IN the case of 64 bits, that would be an 8 byte pointer, so add that to the 1024+8 = 1032
and on 32 bit systems, it results in 1028.

Not sure if that is supposed to report that way or not, but it allows for FreeMem to free the block without knowing the size.

The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Size of memory
« Reply #2 on: October 21, 2024, 04:02:12 pm »
That of course would cause some issues, SysMemsize reports the same, so maybe we have a bug here?
The only true wisdom is knowing you know nothing

Okoba

  • Hero Member
  • *****
  • Posts: 555
Re: Size of memory
« Reply #3 on: October 21, 2024, 04:06:36 pm »
I do not know about the underlying of the default memory manager and the functions I mentioned are barley documented, so maybe we should wait for someone with more official knowledge.

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Size of memory
« Reply #4 on: October 21, 2024, 04:33:47 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Caption := (MemSize(Getmem(1024))-SizeOf(Pointer)).Tostring;
  4. end;          
  5.  

That would be the corrected value in either platform.

but still, this isn't Documented as such.
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 16177
  • Censorship about opinions does not belong here.
Re: Size of memory
« Reply #5 on: October 21, 2024, 04:52:16 pm »
Getmem() and Allocmem() are not allocating managed memory at all, Jamie: it is strictly unmanaged heap memory, so it is strange. Here it says as on the tin: 1024.
Code: Pascal  [Select][+][-]
  1. var
  2. a,b:pointer;
  3. begin
  4.   a:=getmem(1024);
  5.   writeln(memsize(a));//1024
  6.   freemem(a);
  7.   b:=allocmem(1024);
  8.   writeln(memsize(b));//1024
  9.   freemem(b);
  10. end.
« Last Edit: October 21, 2024, 04:57:37 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Okoba

  • Hero Member
  • *****
  • Posts: 555
Re: Size of memory
« Reply #6 on: October 21, 2024, 04:55:55 pm »
Here on Windows 10 64bit and FPC 3.3.1 from 2 months ago shows 1032.
What's your OS?

Thaddy

  • Hero Member
  • *****
  • Posts: 16177
  • Censorship about opinions does not belong here.
Re: Size of memory
« Reply #7 on: October 21, 2024, 05:02:17 pm »
Tested 3.3.1 64 bit Windows and Linux from today and 3.2.2 32 bit Windows.
memsize may be affected by a memorymanager, but will always return at least the size of the requested memory,
Running with heaptrc? or cmem?
The actual size is stored at a negative offset from the memory pointer returned, so technically speaking the allocation is bigger by at least the size of a pointer.
« Last Edit: October 21, 2024, 05:10:32 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Okoba

  • Hero Member
  • *****
  • Posts: 555
Re: Size of memory
« Reply #8 on: October 21, 2024, 05:10:17 pm »
No it is a clean simple project, on Release mode.

Thaddy

  • Hero Member
  • *****
  • Posts: 16177
  • Censorship about opinions does not belong here.
Re: Size of memory
« Reply #9 on: October 21, 2024, 05:11:20 pm »
Can not reproduce. 1024 on all platforms. Both with heaptrc and the default memorymanager.

One remark: both of you lost your memory: impossible to free it.
« Last Edit: October 21, 2024, 05:15:10 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8029
Re: Size of memory
« Reply #10 on: October 21, 2024, 05:21:01 pm »
Getmem() and Allocmem() are not allocating managed memory at all, Jamie: it is strictly unmanaged heap memory, so it is strange. Here it says as on the tin: 1024.

I think you two are sparring over terminology: Jamie means that it includes the management block associated with the allocation.

Here it consistently reports 1032.

And filling in the important info: FPC 3.2.2 on x86_64 Debian 12.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Size of memory
« Reply #11 on: October 21, 2024, 05:25:25 pm »
I did the test with 3.2.2-32 bit windows
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Size of memory
« Reply #12 on: October 21, 2024, 05:45:27 pm »
I just fired up an OLD copy of laz on my Win2000PC, Version 2.0.0.4, I think that was 2.7.1 or something FPC>

Anyways, that also reports the same, it includes the place holder for the size of memory block.

Then on that PC I have OLD Delphi, that does not even have that function.

But it does have the SysmemSize funciton

This must be an added function of Free Pascal?
« Last Edit: October 21, 2024, 05:46:59 pm by jamie »
The only true wisdom is knowing you know nothing

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: Size of memory
« Reply #13 on: October 21, 2024, 05:48:59 pm »
Can not reproduce. 1024 on all platforms. Both with heaptrc and the default memorymanager.

With fpc 3.2.2 win32 for i386 I get 1028 bytes on Windows 10 Enterprise:

Code: [Select]
C:\OpenSourceProjects\tests>copy con mem.pp
program mem;
var a: pointer;
begin
  a := GetMem(1024);
  WriteLn(MemSize(a));
  FreeMem(a);
end.
^Z
        1 file(s) copied.

C:\OpenSourceProjects\tests>c:\OpenSourceProjects\lazarus-3.2\fpc\3.2.2\bin\i386-win32\fpc.exe -vhiwn -l mem.pp
Hint: Start of reading config file c:\OpenSourceProjects\lazarus-3.2\fpc\3.2.2\bin\i386-win32\fpc.cfg
Hint: End of reading config file c:\OpenSourceProjects\lazarus-3.2\fpc\3.2.2\bin\i386-win32\fpc.cfg
Free Pascal Compiler version 3.2.2 [2024/02/26] for i386
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling mem.pp
Linking mem.exe
7 lines compiled, 0.0 sec, 28896 bytes code, 1316 bytes data
2 hint(s) issued

C:\OpenSourceProjects\tests>mem.exe
1028

C:\OpenSourceProjects\tests>systeminfo

Host Name:                 xxxxx
OS Name:                   Microsoft Windows 10 Enterprise

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Size of memory
« Reply #14 on: October 21, 2024, 05:54:31 pm »
sorry, I was mistaken, Delphi does not have the SysMemSize either
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018