SPARK-10432 introduced cooperative memory management for SQL operators that can spill; however, Spillable s used by the old RDD api still do not cooperate. This can lead to memory starvation, in particular on a shuffle-to-shuffle stage, eventually resulting in errors like:
This can happen anytime the shuffle read side requires more memory than what is available for the task. Since the shuffle-read side doubles its memory request each time, it can easily end up acquiring all of the available memory, even if it does not use it. Eg., say that after the final spill, the shuffle-read side requires 10 MB more memory, and there is 15 MB of memory available. But if it starts at 2 MB, it will double to 4, 8, and then request 16 MB of memory, and in fact get all available 15 MB. Since the 15 MB of memory is sufficient, it will not spill, and will continue holding on to all available memory. But this leaves no memory available for the shuffle-write side. Since the shuffle-write side cannot request the shuffle-read side to free up memory, this leads to an OOM.
The simple solution is to make Spillable implement MemoryConsumer as well, so RDDs can benefit from the cooperative memory management introduced by
Note that an additional improvement would be for the shuffle-read side to simple release unused memory, without spilling, in case that would leave enough memory, and only spill if that was inadequate. However that can come as a later improvement.
Workaround: You can set spark.shuffle.spill.numElementsForceSpillThreshold=N to force spilling to occur every N elements, thus preventing the shuffle-read side from ever grabbing all of the available memory. However, this requires careful tuning of N to specific workloads: too big, and you will still get an OOM; too small, and there will be so much spilling that performance will suffer drastically. Furthermore, this workaround uses an undocumented configuration with no compatibility guarantees for future versions of spark.