Recent

Author Topic: free memory  (Read 2898 times)

大悟还俗

  • Newbie
  • Posts: 3
free memory
« on: March 07, 2022, 04:31:01 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. type
  3.   _FileRec = record
  4.     FileName: ShortString;
  5.     FileId  : UInt64;
  6.     ParentId: UInt64;
  7.     FullName: String;
  8.   end;
  9.   TFileRec =  _FileRec;
  10.   PFileRec = ^_FileRec;
  11. var
  12.   i: Integer;
  13.   pFile: PFileRec;
  14.   a: TList;
  15. begin
  16.   a:= TList.Create;
  17.   for I:=0 to 9999999 do begin
  18.       pFile := AllocMem(Sizeof(TFileRec));
  19.       pFile^.FileName := '';
  20.       pFile^.FullName := '';
  21.       pFile^.FileId   := I;
  22.       pFile^.ParentId := I+ 200;
  23.       a.Add(pFile);
  24.   end;
  25.   for I:= a.Count-1 downto 0 do begin
  26.       FreeMem(a[I]);
  27.   end;
  28.   a.Clear;
  29.   a.Free;
  30. end;
  31.  
  32.  


大家好,当我执行这段代码的时候,我用任务管理器上,看到内存没有回到初始状态,我想让内存回来初始状态,而不是关闭程序时才释放

Hello everyone, when I execute this code, I use the task manager and see that the memory does not return to the initial state, I want the memory to return to the initial state, instead of releasing it when the program is closed




marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12944
  • FPC developer.
Re: free memory
« Reply #1 on: March 07, 2022, 04:45:12 pm »
It is currently not possible to force this. The heapmanager keeps memory blocks around for to keep future allocations fast.

In general, afaik only large blocks are returned to the OS. But the memory is free, and will be used for new allocations

af0815

  • Hero Member
  • *****
  • Posts: 1409
Re: free memory
« Reply #2 on: March 07, 2022, 04:47:31 pm »
Try to use heaptrace and find your 'features' inside of Lazarus.

If your programm ask the mem-manager for memory it will borrow it from the system. If you free the memory inside your app, it will go back to the mem-manager. And the mem-manager will hold this memory for the next request. So memory will be full free'd only a the end of the program.
regards
Andreas

大悟还俗

  • Newbie
  • Posts: 3
Re: free memory
« Reply #3 on: March 07, 2022, 04:53:08 pm »
 Cloud add a new parameter for compiler to really free all memory,   thanks

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: free memory
« Reply #4 on: March 07, 2022, 04:58:31 pm »
Cloud add a new parameter for compiler to really free all memory,   thanks

Hi!

No - that breaks the design of the memory manager.

Read the posts of marcov and af0815 again.

Winni

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12944
  • FPC developer.
Re: free memory
« Reply #5 on: March 07, 2022, 05:05:11 pm »
Memory managers are replaceable.  You can try to write a new, or  alter an existing one.


af0815

  • Hero Member
  • *****
  • Posts: 1409
Re: free memory
« Reply #6 on: March 07, 2022, 05:48:52 pm »
Instead of using AllocMem and FreeMem you can get the memory direct from/back to system. If you use it in such manner, you can controll all.
regards
Andreas

大悟还俗

  • Newbie
  • Posts: 3
Re: free memory
« Reply #7 on: March 08, 2022, 05:14:20 am »
 :)
Thank you

 

TinyPortal © 2005-2018