Skip to the page
Tecyfy

PySpark interview question · hard

What causes memory issues in PySpark transformations, and how can they be mitigated?

The short answerMemory problems in PySpark usually come from one of four places: pulling too much data to the driver, partitions too large for an executor's memory (often because of skew), Python processes using memory outside the JVM that the container doesn't allow for, or caching more than fits.

The full answer

Memory problems in PySpark usually come from one of four places: pulling too much data to the driver, partitions too large for an executor's memory (often because of skew), Python processes using memory outside the JVM that the container doesn't allow for, or caching more than fits. The fix depends on which one it is, so read the error first.

Recognising the failure

SymptomLikely cause
Driver OutOfMemoryError, or the notebook kernel dies after collect() or toPandas()Too much data brought to the driver, or a large broadcast
Executor OutOfMemoryError: Java heap spaceLarge partitions, skew, a huge row (big arrays or strings)
Container killed for exceeding memory limits (YARN or Kubernetes)Off-heap use: Python workers, Arrow buffers, native libraries; overhead too small
Heavy spill to disk, long garbage collectionExecutor memory too small for the partitions

Causes and fixes

  1. Data to the driver Avoid collect() and toPandas() on large data. Use show(), take(), aggregate first, or write results to storage. Raise spark.driver.maxResultSize only if the result really is meant to be large.

  2. Partitions too large Each task holds its partition's working data. Increase the number of shuffle partitions so each is about 100–200 MB, and let adaptive query execution coalesce small ones. For file reads, spark.sql.files.maxPartitionBytes (128 MB by default) controls split size.

  3. Skew One huge partition runs out of memory while the others are fine. AQE skew join handling, broadcast joins and salting spread the data.

  4. Python memory Python UDFs and pandas UDFs run in Python worker processes outside the JVM heap. Increase spark.executor.memoryOverhead (by default 10% of executor memory, at least 384 MB) or set spark.executor.pyspark.memory, and reduce Arrow batch sizes. Better: replace Python UDFs with built-in functions.

  5. Aggregations that build big collections collect_list, collect_set and RDD groupByKey gather all values of a key into memory. Use aggregations that combine as they go (sum, count, approx_count_distinct), or reduceByKey instead of groupByKey on RDDs.

  6. Too much caching Cache only what is reused, and unpersist() afterwards. A DataFrame's cache() uses MEMORY_AND_DISK, so partitions that don't fit go to disk rather than failing, but cached data still competes with execution memory.

  7. Large broadcasts Broadcasting a table that is too large can exhaust driver and executor memory; check sizes before forcing broadcast().

  8. Executor sizing Raise spark.executor.memory, or keep memory the same and lower spark.executor.cores, so each task gets more memory. Very large heaps (well over 64 GB) suffer long garbage collection pauses; more, medium-sized executors are usually better.

spark.memory.fraction (0.6 by default) rarely needs changing; partition sizing and avoiding driver collection solve most cases.

Last updated 16 October 2026

Sources

Where each fact comes from

  1. 1Tuning Spark
  2. 2Spark configuration
  3. 3Performance tuning