[Haskell-cafe] Question about memory usage

Tako Schotanus tako at codejive.org
Sat Aug 14 15:46:35 EDT 2010


First of all, thanks to the people who responded :)

On Sat, Aug 14, 2010 at 17:49, Christopher Lane Hinson <
lane at downstairspeople.org> wrote:

>
> 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.


I understand, if you don't want to keep the memory tied up you either define
the reference locally or you use another algorithm.


>
>
>  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.
>
>
Sure, although the memory use would normally be more obvious and it would
actually be more work to make the result globally permanent.
(the difference between the 10 lines and the 100 lines you refer to
probably)


>
>  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.
>

Ok, makes sense.

Just to make this clear, I'm not complaining nor suggesting there is
anything wrong with the way Haskell does things (minds immeasurably superior
to mine working on this stuff, I'm not going to pretend to know better),
it's just one of those "surprises" for somebody who has no experience yet
with Haskell.

Thanks,
 -Tako
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100814/eeac868a/attachment.html


More information about the Haskell-Cafe mailing list