| ID | 157e5b14-c0d4-4c7e-9eb0-6b6e63d4e603 |
|---|---|
| DEERTOPIAVISIBILITY | public |
Retaining the head
In Clojure, "retaining the head" refers to an issue of lazy seqs. Consider the following head-retaining snippet:
(def a (range))
(def b (count a))
Evaluating (count a) attempts to exhaust an infinite lazy sequence while retaining a reference (a) to the head of the sequence, preventing the garbage collector from reclaiming items once consumed. Thus, the runtime will eventually run out of memory and crash. Alternatively, consider the non–head-retaining alternative:
(def b (count (range)))
This snippet will never terminate, but it won't cause the runtime to exhaust its memory.
References
[cite:@terrel2015lazy]