[Haskell-beginners] Space leak debugging

Philip Scott haskell-beginners at foo.me.uk
Thu Jun 3 16:18:12 EDT 2010


Hello again, it's me, your favourite haskell beginner :)

This one really has me puzzled. I've got a little example here - the 
program itself is a bit silly but it illustrates my problem nicely.

He's a little program:

import qualified Data.ByteString as B

chunkFiller i bs = do
     if (B.null bs ) then return 1
                    else chunkFiller (i+1) bs

main = do
     let bs = (B.singleton 10)
     j <- chunkFiller 0 bs
     return 1

It runs forever, which is fine, but it doesn't run in constant space. In 
fact, it gobbles up all the memory until it can't get any more and dies. 
I can't for the life of me work out what is going on, surely there is 
some enormous thunk being built up somewhere, but for all the 
Debug.Trace(), $!, seq's, and profilign outputs in the world I can't 
figure out where. What is *really* interesting is that if you replace 
the 'if' line with

     if (B.null bs || i== -1) then return 1

(i never equals -1) then it runs in constant space! That just boggles my 
mind.

As usual, any thoughts greatly appreciated.

All the best,

Philip


More information about the Beginners mailing list