[m-dev.] track down a memory leak

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Sep 8 11:46:44 AEDT 2000


On 07-Sep-2000, Peter Ross <peter.ross at miscrit.be> wrote:
> Any advice on how to track down what memory is not being collected?
> The compute example from my thread tutorial doesn't run in constant
> memory and as far as I know it should.  I would like to see what
> memory is not being collected and if possible why?

Compile the garbage collector with -DGC_DEBUG and -DKEEP_BACKPTR.
Run the program under gdb.  Set a breakpoint at some suitable point,
and when the breakpoint is reached, type `call GC_generate_random_backtrace'.
This will pick a random piece of heap and print a backtrace which
explains why that piece of memory is not being collectable by showing
how it is reachable from one of the roots.

Do this a few times, and see which addresses keep cropping up.

See boehm_gc/backtrace.h for more info.

Unfortunately the backtrace is not particularly clear because it just
prints hex addresses, not Mercury terms.  And regrettably that isn't easy
to remedy.  However, if you look around at the memory with gdb's "p"
or "p/a" commands you can often figure out what it is.
If you know what Mercury type it is, you can print it out by calling
ML_io_print_to_current_stream() from the debugger. E.g. if you happen
to know that address 0x12345678 has type `int', you can print it using

	gdb> call ML_io_print_to_current_stream(
		&mercury_data___type_ctor_info_int_0, 0x12345678);

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list