[Haskell-cafe] Question about memory usage

Christopher Lane Hinson lane at downstairspeople.org
Sat Aug 14 11:49:13 EDT 2010

On Sat, 14 Aug 2010, Tako Schotanus wrote:

> I was reading this article:
> http://scienceblogs.com/goodmath/2009/11/writing_basic_functions_in_has.php
> And came to the part where it shows:
> > fiblist = 0 : 1 : (zipWith (+) fiblist (tail fiblist))
> But then I read that "Once it's been referenced, then the list up to where you looked is concrete - the
> computations won't be repeated."

It is so implemented.  If you *really* wanted a weak reference that could be
garbage collected and rebuilt (you don't), it could be made to happen.

> and I started wondering how that works.
> Because this seems to mean that functions could have unknown (to the caller) memory requirements.

This is true in any programming language or runtime.  Nothing special
has happened: you could implement the same thing in C/C++/Java/Python,
but it would take 10-100 lines of code.

> How does one, programming in Haskell, keep that in check?
> And when does that memory get reclaimed?

Haskell is garbage collected, as soon as the fiblist is not longer reachable,
and the runtime wants to reclaim the memory, it will.

If fiblist is a top-level declaration it will always be reachable.


More information about the Haskell-Cafe mailing list