Forum > General

[SOLVED] Understanding memory usage

(1/2) > >>

Wesbat:
Hello.

I noticed that when I create multiple instances of a form, the memory usage climbs slightly each time. Also showing an already created form instance also increases memory slightly. And when these forms are closed with CloseAction := caFree it is not reclaimed back.

I noticed this only because my new hobby project requires opening multiple instances of a form.

I feel like my assumptions are wrong, or the metrics I'm seeing in Process explorer (also, Windows task manager) are misleading me.

I created a test project to demonstrate this. I enabled heaptrc and it reports no unfreed allocs.

As a learning experience I'd like to understand what is going on here, or how I should adjust my expectations  :)


Leledumbo:
As far as I understood the documentation, your assumptions are correct. The memory manager does not immediately return deallocated memory to the OS upon call to Dispose/FreeMem, but instead put them into the freelist cache. The idea is that next allocation should search this cache first to avoid round trip to the OS.

Handoko:
I downloaded the code, modified and tested on Ubuntu Mate 23.10. This is the test result:

- Starting the code the memory usage was 1.3 GB
- After clicking the button, the memory usage was 1.4 GB
- After I manually closing the forms, the memory usage was 1.3 GB

If your computer's memory usage didn't return to its previous size after closing the forms, probably because:
- OS related behavior
- You use too few samples

This is the code I added:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  NewForm: TForm2;  i: Integer;begin  for i := 0 to 99 do  begin    NewForm := TForm2.Create(Self);    NewForm.Color := Random($FFFFFF);    NewForm.Show;  end;end;


------
Oops, I'm too late. Leledumbo already provided the answer.

Wesbat:

--- Quote from: Leledumbo on November 18, 2024, 08:11:24 am ---As far as I understood the documentation, your assumptions are correct. The memory manager does not immediately return deallocated memory to the OS upon call to Dispose/FreeMem, but instead put them into the freelist cache. The idea is that next allocation should search this cache first to avoid round trip to the OS.

--- End quote ---

Thanks for the documentation link, that is good info to know Leledumbo.


--- Quote from: Handoko on November 18, 2024, 08:15:14 am ---If your computer's memory usage didn't return to its previous size after closing the forms, probably because:
- OS related behavior
- You use too few samples

--- End quote ---

Indeed, sample size is something I completely ignored - thanks for the good insight Handoko.

marcov:

--- Code: ---[quote author=Handoko link=topic=69364.msg538493#msg538493 date=1731914114]
    NewForm := TForm2.Create(Self);

--- End code ---

Creating forms this way will assign "self" ((T)form1 instance)  as the owner of the newly created form2 instance. These will only be automatically freed when form1 is destroyed.

This is why at the end all memory is released.  For popups forms, assign NIL, and manually free them (isn't there a free on close property?), or remove them from the form1 list and free them.

Navigation

[0] Message Index

[#] Next page

Go to full version