Garbage collection as a dual of laziness?

It occurs to me that garbage collection can be seen as some kind of dual of laziness. Laziness means deferring the creation of values until later in the future (or even never). Garbage collection means eagerly destroying data created in the past, and reclaiming the memory used by it, before some other event which would do so (in most garbage-collected languages, I think process destruction is the only other thing that frees memory, leaving aside foreign functions). If you don't have enough laziness (e.g. because of strict pattern matching on tuples) your program might do unnecesssary work (time wastage); if you don't have enough garbage collection (e.g. because a value will never be accessed again but is still referred to from something live), your program might leak memory (space wastage). Of course, space wastage can lead to time wastage, and vice-versa. Can this intuition be made any more formal? Is it merely of pedagogical use, or does anything interesting follow from it? -- Robin

"Robin" == Robin Green
writes:
Robin> It occurs to me that garbage collection can be seen as some Robin> kind of dual of laziness. Laziness means deferring the Robin> creation of values until later in the future (or even Robin> never). Garbage collection means eagerly destroying data Robin> created in the past, and reclaiming the memory used by it, Robin> before some other event which would do so I think of garbage collection in the opposite way - that the programmer doesn't free the memory as soon it is known to be safe to do so, but leaves the job to a garbage collector, which may not get round to it for quite some time (perhaps not until program termination, even). -- Colin Adams Preston Lancashire

On 23/11/2008, at 9:18 PM, Robin Green wrote:
It occurs to me that garbage collection can be seen as some kind of dual of laziness. Laziness means deferring the creation of values until later in the future (or even never).
A program optimisation might also have the same effect (of avoiding a computation/work). Also note: if you want to take an extreme position, then most (all?) programming languages can be seen to be somewhat "lazy", since computations are goal directed, and therefore functions (or procedures) are only evaluated on points in their domain which are "needed" by the rest of the computation (consider a function defined on an infinite domain). However, that is not the traditional definition of lazy.
Garbage collection means eagerly destroying data created in the past, and reclaiming the memory used by it, before some other event which would do so (in most garbage-collected languages, I think process destruction is the only other thing that frees memory, leaving aside foreign functions).
Don't forget the stack. Besides, I'm not sure how "eager" most GCs are.
If you don't have enough laziness (e.g. because of strict pattern matching on tuples) your program might do unnecesssary work (time wastage); if you don't have enough garbage collection (e.g. because a value will never be accessed again but is still referred to from something live), your program might leak memory (space wastage).
Of course, space wastage can lead to time wastage, and vice-versa.
Can this intuition be made any more formal? Is it merely of pedagogical use, or does anything interesting follow from it?
If you are looking for interesting (and perhaps unconventional) musings on GC, then you might enjoy this paper: "Thermodynamics and Garbage Collection", Henry G Baker. http://home.pipeline.com/~hbaker1/ThermoGC.html Maybe that will spark some new ideas. Cheers, Bernie.

A way this analogy breaks down is that lazyness evaluates precisely what is needed, and no more. The set of values evaluated by lazyness is exactly equivalent to the set of values needed. Garbage collectors are conservative by nature, the values collected by the garbage collector are some subset of the values that will not be needed in the future, which particular subset that is depends on details of the garbage collector and the optimizer. Also, there is no real direct way to "observe" _when_ values have been garbage collected (without resorting to weak pointers or implementation details) so it is questionable what a correspondence will give you practically in terms of reasoning ability. Whether the garbage was 'collected' right away or when needed, as long as you don't run out of memory the program is oblivious to it. John -- John Meacham - ⑆repetae.net⑆john⑈
participants (4)
-
Bernie Pope
-
Colin Paul Adams
-
John Meacham
-
Robin Green