[Lazarus] Finding memory leaks

classic Classic list List threaded Threaded
19 messages Options
Reply | Threaded
Open this post in threaded view
|

[Lazarus] Finding memory leaks

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 :
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

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

aradeonas
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?
 
Regards,
Ara
 
 
-- 
http://www.fastmail.com - IMAP accessible web-mail

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

Graeme Geldenhuys-3
In reply to this post by aradeonas
On 2015-10-24 13:51, 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

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

Martin Frb
In reply to this post by aradeonas
On 24/10/2015 13:51, Aradeonas wrote:
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


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

zeljko
In reply to this post by aradeonas


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?

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

zeljko

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

Michael Van Canneyt


On Sun, 25 Oct 2015, Zeljko wrote:

>
>
> 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.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

aradeonas
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 ?

Regards,
Ara


--
http://www.fastmail.com - Same, same, but different...


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

Ondrej Pokorny
On 25.10.2015 14:36, 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

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

aradeonas
Use another memory manager (?)
 
Can you explain more or give an example?
 
Regards,
Ara
 
 
-- 
http://www.fastmail.com - Accessible with your email software
                          or over the web

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

leledumbo
Administrator
In reply to this post by aradeonas
> 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.
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

Ondrej Pokorny
In reply to this post by aradeonas
On 25.10.2015 17:29, 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

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

Michael Van Canneyt
In reply to this post by aradeonas


On Sun, 25 Oct 2015, Aradeonas wrote:

>> Use another memory manager (?)
>
> Can you explain more or give an example?

For example:
Just add cmem as the first unit in the uses clause, and you will be using the C memory manager.
It uses different algorithms.

You can write your own. But that is harder.

Michael.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

aradeonas
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


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

Michael Van Canneyt


On Mon, 26 Oct 2015, 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.

> 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.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

aradeonas
Thanks, so asking out of curiosity if I want really free memory I should
write a custom memory manager?

Ara


--
http://www.fastmail.com - Same, same, but different...


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

Michael Van Canneyt


On Mon, 26 Oct 2015, 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.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

aradeonas
As I said it curiosity.

Regards,
Ara


--
http://www.fastmail.com - The way an email service should be


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

Héctor Fiandor
In reply to this post by Michael Van Canneyt
Dear Michel:

I have used de cmen in the uses section of the .lpr. it is correct?

Thanks in advance

Saludos,
Ing. Héctor Fiandor
[hidden email]


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Finding memory leaks

aradeonas
Yes,the first unit.

Regards,
Ara


--
http://www.fastmail.com - Or how I learned to stop worrying and
                          love email again


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus