[Haskell-cafe] Finding Memory Leaks

Matthew Bromberg mattcbro at earthlink.net
Thu Nov 2 15:02:13 EST 2006

I have a rather complex Haskell program that performs an engineering 
simulation using a lot of large complex valued
matrices.  The imperative components of the simulation are written in C 
and interfaced into Haskell which handles
the higher level logic.  This has served me fairly well, and I've been 
pleased with both the development style and
the performance of my code.  However I've run into a fairly major issue 
with an unresolvable memory leak in the Haskell code.
The memory leak is apparent since both private allocated bytes and page 
file bytes associated with the process increase linearly over time while 
the process is running.
Eventually it causes the process to crash.

I believe it's in the Haskell code because I have replaced malloc, free, 
realloc etc. in the C code with a memory leak detector and the C code
appears to free all allocated resources.  I also know that the memory 
leak is due to unmatched free's from a call to malloc from one of the 
windows XP system libraries. Unfortunately GHC's heap profiler does not 
reveal the memory leak.  All my code is compiled using the -prof -auto 
flag and then run using +RTS -hc -RTS.  The resulting plots do not show 
a linear increase in heap usage, although the Windows XP operating 
system does report such an increase.

This leaves me in a difficult position in terms of finding the offending 
code.  I am currently resorting to commenting out code line by line, but 
it's very tedious since I have to preserve the state functionality of 
the code to get it to run.  Is there a way to compile Haskell into C 
using GHC and then forcing the C code to hook up to the memory leak 
detecting versions of malloc?  I know that GHC can produce intermediary 
C code, but I have no idea how to access it and how to setup all the run 
time libraries etc, so that I can compile the C code separately.  Any 
advice for me in this situation would be appreciated.

More information about the Haskell-Cafe mailing list