Head retention is the unintended preservation of references to the beginning of a lazy sequence. As elements are realized, retaining that head can keep earlier elements in memory and prevent garbage collection, undermining lazy processing of large inputs. section 3.4.5
Realizing versus releasing a sequence
doall realizes a lazy sequence and returns the realized collection, so all elements remain retained. For side-effecting traversal, dorun also realizes items but returns nil without holding the sequence head. doseq and dotimes likewise return nil by design. section 3.4.5 section 3.4.5
Avoid head retention when processing potentially large lazy sequences:
- Prefer dorun for realization whose purpose is executing side effects. section 3.4.5
- Use doall deliberately when retaining the realized collection is required. section 3.4.5
