Currently in Spark we entirely unroll a partition and then check whether it will cause us to exceed the storage limit. This has an obvious problem - if the partition itself is enough to push us over the storage limit (and eventually over the JVM heap), it will cause an OOM.
This can happen in cases where a single partition is very large or when someone is running examples locally with a small heap.
We should think a bit about the most elegant way to fix this - it shares some similarities with the external aggregation code.
A simple idea is to periodically check the size of the buffer as we are unrolling and see if we are over the memory limit. If we are we could prepend the existing buffer to the iterator and write that entire thing out to disk.