space leak

space leak

A data structure which grows bigger, or lives longer, thanmight be expected. Such unexpected memory use can cause aprogram to require more garbage collections or to run out ofheap. Space leaks in functional programs usually resultfrom excessive laziness. For example, the Haskell function

sum [] = 0sum (x:xs) = x + sum xs

when applied to a list will build a chain of closures for theadditions and only when it reaches the end of the list will itperform the additions and free the storage. Another exampleis the function

mean l = sum l / length l

The sum function forces the entire list l to be evaluated andbuilt in the heap. None of it can be garbage collected untilthe length function has consumed it.