Haskell-beginners problem with memory consuption

Ketil Malde ketil+haskell at ii.uib.no
Thu Oct 2 10:01:32 EDT 2003

Petter Egesund <petter.egesund at kunnskapsforlaget.no> writes:

> I load my file in one chunk, and does a lot of substitutes on the string -
> this is quick eating all my memory and the computers start to get really
> slow.

Keep in mind that Strings are lists of characters.  I think (somebody
correct me if I'm wrong) GHC will store a character inside a cons
cell, but that still leaves 8 bytes per character.  Worst case it will
store the 8-byte cons cell pointing to a 32-bit char value, 12 bytes
per character. (Strings as lists-of-char is very useful, but not
terribly efficient). 

Using hGetArray to read into a UArray of Word8, or something like
that, will probably be a lot faster and save a lot of space.

> The problem is of course that the string is copied each time I do a
> substitute

As W.J. says, you should make sure that you don't keep more references
to the original string than you need to, so that old stuff can be
garbage collected.

If you post (pieces of) the code, people may be able to point out
improvements more easily.

If I haven't seen further, it is by standing in the footprints of giants

More information about the Haskell-Cafe mailing list