Recent

Author Topic: Easiest Way To Track Down Memory Leaks  (Read 5385 times)

RegNatarajan

  • New Member
  • *
  • Posts: 28
Easiest Way To Track Down Memory Leaks
« on: June 24, 2017, 03:33:55 am »
My Lazarus X-Windows application is leaking memory somewhere.  I let it run for a few hours and it's using a few hundred more megs of memory than it was when I started it.  CPU usage is flat.  Does Lazarus have any tools to assist me in tracking down what is not freeing up memory?

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Easiest Way To Track Down Memory Leaks
« Reply #1 on: June 24, 2017, 04:07:06 am »
go to <project\project options\compiler options\debugging> and check the use heaptrc unit. build and run your application from the ide do what ever need to be done to test for memory leaks and exit. a number of dialogs will pop up to inform of various possible memory leaks.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Easiest Way To Track Down Memory Leaks
« Reply #2 on: June 24, 2017, 04:38:20 am »
 :)
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

RegNatarajan

  • New Member
  • *
  • Posts: 28
Re: Easiest Way To Track Down Memory Leaks
« Reply #3 on: June 24, 2017, 05:51:00 am »
go to <project\project options\compiler options\debugging> and check the use heaptrc unit. build and run your application from the ide do what ever need to be done to test for memory leaks and exit. a number of dialogs will pop up to inform of various possible memory leaks.
Hey, I really appreciate this input but when I checked "use heaptrc" and then ran the app for awhile, no dialogs popped up when I exited.  I did, however, clean up my own code and found a few memory leaks on my own before doing that so does this mean that heaptrc didn't find anything now?   Maybe I actually got them all.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Easiest Way To Track Down Memory Leaks
« Reply #4 on: June 24, 2017, 06:01:29 am »
go to <project\project options\compiler options\debugging> and check the use heaptrc unit. build and run your application from the ide do what ever need to be done to test for memory leaks and exit. a number of dialogs will pop up to inform of various possible memory leaks.
Hey, I really appreciate this input but when I checked "use heaptrc" and then ran the app for awhile, no dialogs popped up when I exited.  I did, however, clean up my own code and found a few memory leaks on my own before doing that so does this mean that heaptrc didn't find anything now?   Maybe I actually got them all.

no it means that you either did not enabled it in the first place or the save to file feature of heaptrc is on. Open the project's source code (double click the lpr file in the project inspector) and look for a line reading
Code: Pascal  [Select][+][-]
  1.   SetHeapTraceOutput('heap.trace');
  2.  
Find attached a no leaks dialog from an empty application.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

RegNatarajan

  • New Member
  • *
  • Posts: 28
Re: Easiest Way To Track Down Memory Leaks
« Reply #5 on: June 24, 2017, 06:23:15 am »
I hate being helpless but I'm not getting it.  I triple checked my project options and made sure that heaptrc was checked.
http://imgur.com/vTbLyWh

Then I edited the project source and found no such line with SetHeapTraceOutput.  I tried compiling/running but again, no dialog.  I then tried manually adding the line SetHeapTraceOutput('heap.trace'); and compiled/ran and still nothing.
http://imgur.com/CmZL883

Sorry for the links, I don't appear to have an option to insert images in this system.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Easiest Way To Track Down Memory Leaks
« Reply #6 on: June 24, 2017, 06:42:07 am »
you do not need the line I posted although if you added it you must be able to find a heap.trace file now. What OS are you using? Lazarus/fpc version? It is odd because I use it constantly in my development from lazarus 1 that I started and never had any problems with the popup report at least on windows that I use.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Easiest Way To Track Down Memory Leaks
« Reply #7 on: June 24, 2017, 07:12:39 am »
Load this project... Do you see any dialog at the end ???
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Easiest Way To Track Down Memory Leaks
« Reply #8 on: June 24, 2017, 07:25:49 am »
Easy example :

This code only creates a TStringlist and you can see HEAPTRC shows line 30 and 31 in uMemLeak.pas...
Code: Pascal  [Select][+][-]
  1. unit uMemLeak;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     procedure FormCreate(Sender: TObject);
  13.   private
  14.     { private declarations }
  15.   public
  16.     { public declarations }
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R *.lfm}
  25.  
  26. procedure TForm1.FormCreate(Sender: TObject);
  27.  var
  28.   sl: TStringlist;
  29. begin
  30.  sl     := TStringlist.Create;
  31.  sl.Text:= 'öalsdkjföalsdkjfölsdkfjösldkfjölsdkfjösldfk'+
  32.            'öalsdkfjöadkfjöasdklfjösldkfjösldkfjösldfkj'+
  33.            'öaldksjfölsdkjfölsdkfjösdfkjösdlkfjöasldfkj';
  34. end;
  35.  
  36. end.              
  37.  
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

RegNatarajan

  • New Member
  • *
  • Posts: 28
Re: Easiest Way To Track Down Memory Leaks
« Reply #9 on: June 24, 2017, 07:37:15 am »
you do not need the line I posted although if you added it you must be able to find a heap.trace file now. What OS are you using? Lazarus/fpc version? It is odd because I use it constantly in my development from lazarus 1 that I started and never had any problems with the popup report at least on windows that I use.
That could be my problem. I'm developing on Ubuntu 16.04 LTS using Gnome and I can't get the dialog to pop up no matter what I try. I found the file though and I'm hopeful that'll help me track down my issues.  Really appreciate everyone's help.  I may trouble you with a couple more noob questions tomorrow after I investigate this file.

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: Easiest Way To Track Down Memory Leaks
« Reply #10 on: June 24, 2017, 07:49:59 am »
and I can't get the dialog to pop up no matter what I try.
Yes, at Linux HepTRC outputs to console. You may watch it by VIEW > DEBUG WINDOWS > TERMINAL OUTPUT or use shortcut ctrl+alt+O.
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Easiest Way To Track Down Memory Leaks
« Reply #11 on: June 24, 2017, 02:22:10 pm »
and I can't get the dialog to pop up no matter what I try.
Yes, at Linux HepTRC outputs to console. You may watch it by VIEW > DEBUG WINDOWS > TERMINAL OUTPUT or use shortcut ctrl+alt+O.
Yeah I'm not very familiar with linux development environment. I do not trust any console output, it goes by faster than I can analyze what I see. Install the leakview package put the line I showed you in your project's source code under an {$IFdef Debug} and go with the view\leaks and traces dialog (installed by the leakview package) to open the leak report inside Lazarus.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018