[fpc-pascal] Is such memory statistics possible?

Ludo Brands ludo.brands at free.fr
Mon Aug 15 11:17:53 CEST 2011


> In our previous episode, Max Vlasov said:
> > about collecting some statistics.
> > 
> > The idea is similar to some disk utilities that collects 
> and sort the 
> > sizes of directories. You know when every folder on the computer is 
> > scanned and all the resulting paths are sorted by the summed size. 
> > Such utilities usually help to find space on the hard drive to free.
> > 
> > With memory for every memory allocation we can call the path the 
> > addresses of procedures in stack. So when a function 
> funci() is called 
> > from the function parentfunc() and getmem is called inside 
> func() then 
> > we should add this size to the corresponding entries both 
> for func() 
> > and parentfunc(). Sure for every freemem we should decrease 
> from every 
> > entry related to the previous getmem (that might be a 
> challenge since 
> > freemem not necessary follows the same calling pattern). Anyway, if 
> > this system is implemented, in any moment during the 
> program run, the 
> > developer can query N most memory hungry procedures to the 
> moment. And 
> > this will include non only the procedures that call 
> getmem,new  etc, 
> > but also higher level ones giving better hints for further 
> > optimization.
> > 
> > Do tools(units) like this already exist? If not, is developing such 
> > unit technically possible/hard with the currently available debug 
> > information?
> 
> This is basically what valgrind (or fulldebugmode of fastmm) 
> does. But they do this by parsing the stack on each call to 
> the memory manager, and then keep track of it.
> 
> Note that all these techniques can be very, very slowing. 
> E.g. I tried to debug the CHM support with valgrind, and I 
> terminated the valgrind process after 5 hours because it was 
> not even half way to where the bug was.
> 
> Without valgrind the program reached the point in 1-2 minutes.....
> 

The valgrind massif tool (valgrind --tool=massif) does the same but reports
on a sampling basis. It'll look at the heap used and report who allocated
what on a cumulative basis. It won't trace every call nor report leaks but
it allows you to track the big users quite well. I'm currently tracking the
memory "eaters" in lazarus with it. It slows down the program but with a
factor 5 to 10 which makes it more practical than valgrid itself.

Ludo




More information about the fpc-pascal mailing list