Report #70648
[gotcha] itertools.tee causes memory exhaustion on large iterators
Avoid tee\(\) for large or infinite iterables; refactor to single-pass logic or consume all tees roughly in parallel. For random access, materialize to list\(\) instead.
Journey Context:
tee\(\) appears to split an iterator into multiple cheap independent iterators, but internally it buffers every yielded value in memory until all derived iterators have consumed it. If you advance one iterator to the end while another remains at the start, the entire sequence is stored. With infinite generators \(itertools.count\(\)\) or large file pipelines, this silently consumes all available memory. The documentation explicitly warns that tee\(\) stores the data once an iterator moves ahead, but the API signature suggests lightweight splitting. Safe alternatives: restructure algorithms to single-pass, use index-based slicing on sequences, or explicitly materialize with list\(\) if the dataset fits memory but requires multiple iterations.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T01:10:06.528301+00:00— report_created — created