Recent

Author Topic: heap message  (Read 529 times)

xaver13

  • Jr. Member
  • **
  • Posts: 95
heap message
« on: October 07, 2019, 07:53:35 pm »
Hello, I have application, which I can run on windows under lazarus.
On the end I have message:
Heap dump by heaptrc unit.
..
True heap size 360448
True free heap 360320
Should be 360448
..
When I run the exe the message is the same.
The same application has no problem on Linux.

What should be wrong? Thank you.
« Last Edit: October 07, 2019, 07:59:27 pm by xaver13 »
--
Jiri Cvrk

Thaddy

  • Hero Member
  • *****
  • Posts: 8869
Re: heap message
« Reply #1 on: October 07, 2019, 08:32:06 pm »
Can you please compile with -glh or in Lazarus check line numbers option? Otherwise heaptrc is rather useless....
And the problem is likely the same on all platforms....
Most people that want to use threading should learn to patch their jeans first: use a needle.

xaver13

  • Jr. Member
  • **
  • Posts: 95
Re: heap message
« Reply #2 on: October 07, 2019, 09:31:14 pm »
I uncheck " Heaptrc unit" in other debugging info, for project options and I am happy   ;)

--
Thank you for help, Thaddy.
« Last Edit: October 07, 2019, 09:36:15 pm by xaver13 »
--
Jiri Cvrk

Bart

  • Hero Member
  • *****
  • Posts: 3516
    • Bart en Mariska's Webstek
Re: heap message
« Reply #3 on: October 07, 2019, 09:40:01 pm »
That also works for:
Code: Pascal  [Select]
  1. var
  2.   p: pointer;
  3. begin
  4.   GetMeM(p,1024*1024*1024);
  5. end.

See: no error message at all!
Just ignore you have a 1 GB memory leak  O:-)

Bart

winni

  • Sr. Member
  • ****
  • Posts: 307
Re: heap message
« Reply #4 on: October 07, 2019, 10:41:11 pm »
Bart's answer is good.

But with this you have more fun:

 
Code: Pascal  [Select]
  1.   var
  2.       p: pointer;
  3.       i : integer;
  4.     begin
  5.       for i := 1 to 16 do
  6.       begin
  7.       GetMeM(p,1024*1024*1024*i);
  8.       showMessage ('Still alive?');
  9.       end;
  10.     end.
« Last Edit: October 07, 2019, 10:43:31 pm by winni »

ASerge

  • Hero Member
  • *****
  • Posts: 1405
Re: heap message
« Reply #5 on: October 07, 2019, 10:58:19 pm »
Heap dump by heaptrc unit.
..
True heap size 360448
True free heap 360320
Should be 360448
..
Show the full message, please.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5621
    • wiki
Re: heap message
« Reply #6 on: October 07, 2019, 11:52:08 pm »
He may just refer to the fact that heaptrc *always* prints some statement, even if there is no leak.

On Linux this would not have bothered him much, since it goes to console (or debug output window, which may be closed).

But on Window, for GUI app, it causes a pop up every single time your app finishes.
While that is fine, if you actually check for leaks, if/when you look for other issues that can be distracting.

I recommend in that case to compile with -WC which will give you GUI app an additional console.
It will close before you can get the report, but you may be able to see, if anything was printed (especially if there a lots of leaks).
And using fpdebug (svn trunk) and an IDE build with console too, the leaks can be printed to the IDE's console which will stay open, so they can be copied.

PascalDragon

  • Hero Member
  • *****
  • Posts: 616
  • Compiler Developer
Re: heap message
« Reply #7 on: October 08, 2019, 08:56:12 am »
I uncheck " Heaptrc unit" in other debugging info, for project options and I am happy   ;)
You shouldn't ignore a memory leak. Especially if it is something that leaks regularly your application might require more and more memory if it's running a long time.

xaver13

  • Jr. Member
  • **
  • Posts: 95
Re: heap message
« Reply #8 on: October 08, 2019, 10:50:18 am »
Heap dump by heaptrc unit.
..
True heap size 360448
True free heap 360320
Should be 360448
..
Show the full message, please.

Code: Pascal  [Select]
  1. jiricvrk@jc-TP25:~/Pascal/Sportka$ ./sportka
  2. Gtk-Message: 10:46:51.680: Failed to load module "canberra-gtk-module"
  3. Heap dump by heaptrc unit
  4. 23134 memory blocks allocated : 2092458/2151208
  5. 23134 memory blocks freed     : 2092458/2151208
  6. 0 unfreed memory blocks : 0
  7. True heap size : 819200
  8. True free heap : 819200
  9. jiricvrk@jc-TP25:~/Pascal/Sportka$
  10.  

this is full message from ubuntu, thank for reminding to look at the console.


and in windows:

Code: Pascal  [Select]
  1. Error
  2. Heap dump by heaptrc unit
  3. 27752 memory blocks allocated:2471849/2554856
  4. 27752 memory block feed:2471849/2554856
  5. O unfreed memory blocks: 0
  6. True heap size: 425984
  7. True free heap:425856
  8. Should be: 425984
  9.  
« Last Edit: October 08, 2019, 12:29:18 pm by xaver13 »
--
Jiri Cvrk

Thaddy

  • Hero Member
  • *****
  • Posts: 8869
Re: heap message
« Reply #9 on: October 08, 2019, 12:55:16 pm »
Which is correct... it tells you there are no leaks.
Most people that want to use threading should learn to patch their jeans first: use a needle.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5621
    • wiki
Re: heap message
« Reply #10 on: October 08, 2019, 03:39:56 pm »
As far as I know:

Heaptrc is initialized as early as possible. But there may be other stuff happening just before this (or maybe/guessing the OS adds some offset that is not reported correctly). Then whatever happens before can not be correctly reported.

Memory allocation is separated into 2 parts:
A- get huge chunks from the OS (getting small chunks can be very expensive or even impossible)
B- when your code allocs mem (strings, classes, getmem(), ...) then small parts of the big chunk are returned

If  in "B" your app code (or the RTL) gets mem, and does not return this (return to the big chunk), then that is reported as leak.

My guess is that the "Should be" stuff is somehow related to the "big chunk" allocation. (and can not be influenced in your code)


Thaddy

  • Hero Member
  • *****
  • Posts: 8869
Re: heap message
« Reply #11 on: October 08, 2019, 04:24:42 pm »
heaptrc is initialized at compiler level, not unit level. It should be always first therefore. This is documented.
Most people that want to use threading should learn to patch their jeans first: use a needle.

PascalDragon

  • Hero Member
  • *****
  • Posts: 616
  • Compiler Developer
Re: heap message
« Reply #12 on: October 09, 2019, 09:15:35 am »
heaptrc is initialized at compiler level, not unit level. It should be always first therefore. This is documented.
The compiler injects heaptrc as early as possible in the unit initialization table, but the System unit is always first. Thus any initialization the System unit does is done on the non-heaptrc heap.

Thaddy

  • Hero Member
  • *****
  • Posts: 8869
Re: heap message
« Reply #13 on: October 09, 2019, 09:28:39 am »
heaptrc is initialized at compiler level, not unit level. It should be always first therefore. This is documented.
The compiler injects heaptrc as early as possible in the unit initialization table, but the System unit is always first. Thus any initialization the System unit does is done on the non-heaptrc heap.
AFAIK Florian explained that heaptrc is included at the point that the compiler needs to allocate dynamic heap memory. This documented  behavior differs from lower than 3.0.2 (which was the same but not documented), I believe.
If you are correct I will have to adapt the wiki.
« Last Edit: October 09, 2019, 09:49:34 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

PascalDragon

  • Hero Member
  • *****
  • Posts: 616
  • Compiler Developer
Re: heap message
« Reply #14 on: October 10, 2019, 09:15:48 am »
Just look at the INITFINAL table that the compiler generates for the final executable (use -al and search in the resulting .s file of the main program). You'll see this:

Code: ASM  [Select]
  1. .section .data.n_INITFINAL,"d"
  2.         .balign 4
  3. .globl  INITFINAL
  4. INITFINAL:
  5.         .long   3,0
  6.         .long   INIT$_$SYSTEM
  7.         .long   0
  8.         .long   INIT$_$HEAPTRC
  9.         .long   FINALIZE$_$HEAPTRC
  10.         .long   INIT$_$FPINTRES
  11.         .long   0

The RTL walks this table from top to bottom.

Additionally if the System unit would be initialized after the heaptrc unit then the System unit would overwrite the memory manager of the heaptrc unit.