On Dec 21, 2011, at 6:52 PM, Fedor Gogolev wrote:

> Hello. I'm trying to get some threads that I can stop and get last
> values that was computed (and that values are IO values, in fact).
> Here is my first approach:
> [...]
> tick :: Int -> IO Int
> tick v = return $ v + 1
> [...]
> The problem is that it looks a little messy and what's worse it leaks
> memory. So I'm wondering if there is a better approach to do so or
> some fix to memory leak.

I don't have any tips for cleaning up the code off the top of my head, but I 
suspect that the memory leak is coming from the fact that the expression (v+1) 
is not being forced, which means that each iteration of the loop is 
constructing a new thunk with a reference to the old thunk resulting in a data 
structure that is growing in memory usage over time.  In this case the fix is 
easy:  just replace "return" in "tick" with "evaluate" from Control.Exception, 
since "evaluate" guarantees in this case that the expression will be evaluated 
before being returned.

(Caveat:  What I said about "evaluate" will be true for numeric expressions, 
but for non-trivial data structures "evaluate" only ensures that the expression 
is evaluated to something called weak-head normal form, which essentially means 
that it will only evaluate enough of the expression to figure out what the 
outermost constructor of the datatype is;  it just so happens that for numeric 
expressions the outermost data constructor is enough to give you the numeric 
value.)

Cheers,
Greg
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to