Sign in Join

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

Mailing lists / lazarus / 2015-10

Finding memory leaks

19 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

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

Aradeonas

Hi,

I have a program that use many units and just now I find out program memory usage will increase slightly after time. I checked all of my objects and I free them,also I used?DumpHeap for finding memory leaks but its a big report and many other simple programs have many report like this and I cant find anything good in it. Is there any way to count objects or whatever so I can find what object will not free?

I cant gett much of help from report becuase its very big (about 100,000 line) and it have many items like this :

Show quoted text 10 lines
Call trace for block $0030EEC8 size 88? $0049664B TFONTHANDLECACHE__ADD, ?line 104 of ./include/font.inc? $00497F37 TFONT__REFERENCENEEDED, ?line 1157 of ./include/font.inc? $00498275 TFONT__GETREFERENCE, ?line 1277 of ./include/font.inc? $0041FE77 SCREENGETSYSTEMFONT, ?line 51 of ./include/screen.inc? $0049A6B1 TCANVAS__TEXTRECT, ?line 1290 of ./include/canvas.inc? $00520133 TCUSTOMPANEL__PAINT, ?line 127 of ./include/custompanel.inc? $004FF413 TCUSTOMCONTROL__PAINTWINDOW, ?line 126 of ./include/customcontrol.inc $BAADF00D

Or it address end of procedures or ... . So how can I find out what is the problem? Regards, Ara

http://www.fastmail.com - Email service worth paying for. Try it for free -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151024/f9592623/attachment-0001.html>

Reply by email

Aradeonas
to Aradeonas

My question stand but I guess problem is in TObjectList but Im not sure so I made a simple demo like this:

Show quoted text 6 lines
procedure TForm1.Button1Click(Sender: TObject); var? i: integer;? b: TButton; begin? list := TObjectList.Create(True);? for i := 0 to 10000 do? begin? ? b := TButton.Create(Self);? ? list.Add(b);? end; end; procedure TForm1.Button2Click(Sender: TObject); begin? list.Clear; list.Free; end;

Form is clean and just have 2 button with their code so I run it and memory usage in my Windows10 device is about 3mb and then hit the button1 and memory goes up to 25mb and when I want to free and hit button2 memory will decrease to 15mb not 3 , just like my main program that I use?TObjectList?and will free its objects with remove function?but it will increase memory usage while running. Did I miss something obvious?

Regards, Ara

http://www.fastmail.com - IMAP accessible web-mail -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151024/452e3043/attachment.html>

Reply by email

Graeme Geldenhuys
to Aradeonas: I have a program that use many units and just now I find out program memory usage will…
Aradeonas wrote
So how can I find out what is the problem?

Hence I recommend to anybody to develop any code with -gh and -gl enabled as standard. It is *much* easier to fix a memory leak the minute you created it - so you know exactly which few lines caused the problem.

Anyway... this is what I do

1. Compile you program with -gh and -gl and -O- compiler parameters.

2. Run as little as possible of the program then quit. Check if leaks are caused.

3. It is easier if you dump the heaptrace output to file so review.

4. Start at the top. The first line is normally the cause of the leak, and the lines below is the backtrace of how you got to that point. This is not exact science - it takes a clear mind and attention to detail to spot the leaks.

5. The heaptrace reports all memory leaks, but concertrate on one at a time. Many times fixing one leak reduces the leak count a lot.

6. Repeat from step 2 until all leaks are solved.

Some leaks are easy to spot and fix, others can take very long to find. There is no "easy fix" for this (existing code). The "easy fix" for newly written code, is to always develop with -gh and -gl enabled.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Martin Frb
to Aradeonas: I have a program that use many units and just now I find out program memory usage will…
Aradeonas wrote 20 lines
Is there any way to count objects or whatever so I can find what object will not free? I cant gett much of help from report becuase its very big (about 100,000 line) and it have many items like this : Call trace for block $0030EEC8 size 88 $0049664B TFONTHANDLECACHE__ADD, line 104 of ./include/font.inc $00497F37 TFONT__REFERENCENEEDED, line 1157 of ./include/font.inc $00498275 TFONT__GETREFERENCE, line 1277 of ./include/font.inc $0041FE77 SCREENGETSYSTEMFONT, line 51 of ./include/screen.inc $0049A6B1 TCANVAS__TEXTRECT, line 1290 of ./include/canvas.inc $00520133 TCUSTOMPANEL__PAINT, line 127 of ./include/custompanel.inc $004FF413 TCUSTOMCONTROL__PAINTWINDOW, line 126 of ./include/customcontrol.inc $BAADF00D Or it address end of procedures or ... . So how can I find out what is the problem?

look at http://wiki.freepascal.org/leakview
If you redirect above to a file ( set environment HEAPTRC to log=file / on windows setting environment requires a newer gdb, see the Lazarus sourceforge site), then you can load them.

Search for those that include your code.
The ide shows how often each leak recurred "(n times)", find the ones with low count

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151025/e9c96cc7/attachment.html>

Reply by email

Zeljko
to Aradeonas: My question stand but I guess problem is in TObjectList but Im not sure so I made a…
Aradeonas wrote 28 lines
My question stand but I guess problem is in TObjectList but Im not sure so I made a simple demo like this: procedure TForm1.Button1Click(Sender: TObject); var i: integer; b: TButton; begin list := TObjectList.Create(True); for i := 0 to 10000 do begin b := TButton.Create(Self); list.Add(b); end; end; procedure TForm1.Button2Click(Sender: TObject); begin list.Clear; list.Free; end; Form is clean and just have 2 button with their code so I run it and memory usage in my Windows10 device is about 3mb and then hit the button1 and memory goes up to 25mb and when I want to free and hit button2 memory will decrease to 15mb not 3 , just like my main program that I use TObjectList and will free its objects with remove function but it will increase memory usage while running. Did I miss something obvious?

Maybe TObjectlist.Clear does not free objects, in that case you have to iterate through TObjectList and use Remove() which will free objects ?

zeljko

Reply by email

Michael Van Canneyt
to Zeljko
Zeljko wrote 31 lines
On 10/24/2015 03:10 PM, Aradeonas wrote: > My question stand but I guess problem is in TObjectList but Im not sure > so I made a simple demo like this: > > procedure TForm1.Button1Click(Sender: TObject); > var > i: integer; > b: TButton; > begin > list := TObjectList.Create(True); > for i := 0 to 10000 do > begin > b := TButton.Create(Self); > list.Add(b); > end; > end; > procedure TForm1.Button2Click(Sender: TObject); > begin > list.Clear; > list.Free; > end; > > Form is clean and just have 2 button with their code so I run it and > memory usage in my Windows10 device is about 3mb and then hit the > button1 and memory goes up to 25mb and when I want to free and hit > button2 memory will decrease to 15mb not 3 , just like my main program > that I use TObjectList and will free its objects with remove > function but it will increase memory usage while running. > Did I miss something obvious?

There is a difference between releasing an object in heap memory, and returning that memory to the OS: It's not because you've released the memories from the various objects in the list, that the memory used by these objects is returned to the OS.

So what you observe is not necessarily an indication of a memory leak.

TObjectList does remove the objects from memory in a Clear. If that would not work properly, we'd have heard a long time ago :)

Michael.

Reply by email

Ondrej Pokorny
to Aradeonas
Aradeonas wrote
Yea it remove and even I remove all of the list items it get the same result. So Michael, what should I do about this? How can I really free it ?

Use another memory manager (?)

Ondrej

Reply by email

leledumbo
to Aradeonas: I have a program that use many units and just now I find out program memory usage will…
I checked all of my objects and I free them,also I used DumpHeap for

finding memory leaks but its a big report and many other simple programs have many report like this and I cant find anything good in it.

The leak reports lines from both your app and the underlying library you use, i.e. LCL. Find the one from your app (./include/customcontrol.inc, ./include/whatever.inc certainly don't belong to your app), that's where you should start tracing. It's sometimes obvious, sometimes not, no generalization possible.

View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-Finding-memory-leaks-tp4044940p4044955.html Sent from the Free Pascal - Lazarus mailing list .

Reply by email

Ondrej Pokorny
to Aradeonas: Can you explain more or give an example? Regards, Ara http://www.fastmail.com -…
Aradeonas wrote
Use another memory manager (?) Can you explain more or give an example?

The memory manager decides about how to allocate and release memory from/back to system.

https://www.google.at/search?q=fpc+memory+manager
http://www.freepascal.org/docs-html/3.0.0/prog/progsu176.html

But I am not an expert in this topic. I was always happy with the default FPC memory manager and FastMM in Delphi.

Ondrej
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151025/885c0b52/attachment.html>

Reply by email

Aradeonas
to Michael Van Canneyt

Thanks.
cmem will clean memory much better.
App start with 2mb that is less than 3mb without cmem and goes up to 18mb that again is less than 25mb in previous example and after destroying all the items it will decrease to 3mb that is much better result although there is 1mb extra memory usage
Michael as it seems you write it can I ask why this is not the default memory manager?

Regards,
Ara

http://www.fastmail.com - Send your email first class

Reply by email

Michael Van Canneyt
to Aradeonas
Aradeonas wrote
Thanks. cmem will clean memory much better.

It uses a different strategy. It is not necessarily the better. It depends what you care about.

Show quoted text 6 lines
App start with 2mb that is less than 3mb without cmem and goes up to 18mb that again is less than 25mb in previous example and after destroying all the items it will decrease to 3mb that is much better result although there is 1mb extra memory usage Michael as it seems you write it can I ask why this is not the default memory manager?

Because it depends on the C runtime library.

Also, it depends on what you need.
In some cases the FPC memory manager performs better.

Michael.

Reply by email

Michael Van Canneyt
to Aradeonas
Aradeonas wrote
Thanks, so asking out of curiosity if I want really free memory I should write a custom memory manager?

You can. But why would you, if cmem suites your needs ?

Michael.

Reply by email