|
"Space leaks" are not "memory leaks". A memory leak means a program will never free some region of memory; e.g. if it's pointer has been discarded without calling 'free'. That is certainly a matter of correctness. That is certainly a problem for finite-memory machines. In constrast, a "space leak" is just a suboptimal usage of memory. As a classic example, we want the sum of a list of integers to fully evaluate the running total at each step, like this: sum(0, [1,2,3])
sum(0+1, [2, 3])
sum(1, [2, 3])
sum(1+2, [3])
sum(3, [3])
sum(3+3, [])
sum(6, [])
6
However, lazy evaluation may avoid performing the additions right away; instead building up unevaluated 'thunks' (nullary functions), which only get evaluated at the end, like this: sum(0, [1,2,3])
sum(0+1, [2, 3])
sum((0+1)+2, [3])
sum(((0+1)+2)+3, [])
((0+1)+2)+3
(1+2)+3
3+3
6
This is a perfectly correct calculation; and everything has been 'cleaned up' at the end (no worries about 'infinite tapes', etc.). However, if we're trying to e.g. process a massive data stream from disk, these unevaluated thunks may quickly exhaust our available memory. |
The memory gets freed when the program terminates.