HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

Request an account if you don't have one.

Memory leak

Categories: Glossary

A memory leak means that a program allocates more memory than necessary for its execution. Although Haskell implementations use garbage collectors, programmers must still keep memory management in mind. A garbage collector can reliably prevent dangling pointers, but it is easily possible to produce memory leaks, especially in connection with lazy evaluation.

Consider for example:

let xs = [1..1000000::Integer]
in  sum xs * product xs

Since most Haskell compilers expect, that the programmer used let in order to share xs between the call of sum and the call of product, the list xs is completely materialized and hold in memory. However, the list xs is very cheap to compute, and thus it would reduce memory usage considerably, if xs is recomputed for both calls. Since we want to avoid code duplication, we like to achieve this by turning the list definition into a function with a dummy argument.

let makeXs _ = [1..1000000::Integer]
in  sum (makeXs False) * product (makeXs True)

Retrieved from "http://haskell.cs.yale.edu/haskellwiki/Memory_leak"

This page has been accessed 704 times. This page was last modified 17:26, 28 December 2008. Recent content is available under a simple permissive license.