Tech challenge · PySpark

The driver crashes at the end of the job

EasyPySpark

A notebook runs heavy transformations on 800 GB without trouble, then crashes with a driver out-of-memory error on the last line: pdf = result.toPandas(). Explain the failure and what to do instead.

Think it through or sketch an answer first, then open the hint or the solution.

Show a hint

Where does the data end up after toPandas() or collect()?

Show the solution

toPandas() and collect() bring every row to the driver, a single machine, so a large result does not fit. Keep large results distributed: write them to a table or files, aggregate them down first, or take a sample or limit() for inspection.

Example (Python)

# keep it distributed
result.write.mode('overwrite').saveAsTable('analytics.daily_result')

# or bring back only what fits
summary = result.groupBy('region').count().toPandas()
preview = result.limit(1000).toPandas()

Common mistakes

  • Giving the driver more memory: it moves the limit rather than removing it.
  • Calling count() and display() on the same large DataFrame repeatedly without caching: each action recomputes everything.

Learn more

More PySpark challenges

Today's challenge and the full archive