Out of memory Excepetions in Spark
- Out of Memory Exceptions
- Driver Memory Exceptions
- Exception due to Spark driver running out of memory
- Job failure because the Application Master that launches the driver exceeds memory limits
- Executor Memory Exceptions
- Exception because executor runs out of memory
- FetchFailedException due to executor running out of memory
- Executor container killed by YARN for exceeding memory limits
- Driver Memory Exceptions
Out of Memory Exceptions:
Spark jobs might fail due to out of memory exceptions at the driver or executor end. When troubleshooting the out of memory exceptions, you should understand how much memory and cores the application requires, and these are the essential parameters for optimizing the Spark application. Based on the resource requirements, you can modify the Spark application parameters to resolve the out-of-memory exceptions.
Driver Memory Exceptions:
Exception due to Spark driver running out of memory:
Description: When the Spark driver runs out of memory, exceptions similar to the following exception occur.
Exception in thread "broadcast-exchange-0" java.lang.OutOfMemoryError: Not enough memory to build and broadcast the tableto all worker nodes. As a workaround, you can either disable broadcast by setting spark.sql.autoBroadcastJoinThreshold to -1or increase the spark driver memory by setting spark.driver.memory to a higher value
Resolution: Set a higher value for the driver memory, using one of the following commands in Spark Submit Command Line Options on the Analyze page:
--conf spark.driver.memory= <XX>gOR
--driver-memory <XX>G
Job failure because the Application Master that launches the driver exceeds memory limits:
Description: A Spark job may fail when the Application Master (AM) that launches the driver exceeds the memory limit and is eventually terminated by YARN. The following error occurs:
Resolution: Set a higher value for the driver memory, using one of the following commands in Spark Submit Command Line Options on the Analyze page:
--conf spark.driver.memory= <XX>gOR
--driver-memory <XX>G
As a result, a higher value is set for the AM memory limit.
Executor Memory Exceptions:
Exception because executor runs out of memory
Description: When the executor runs out of memory, the following exception might occur.
Resolution: Set a higher value for the executor memory, using one of the following commands in Spark Submit Command Line Options on the Analyze page:
--conf spark.executor.memory= <XX>gOR
--executor-memory <XX>G
FetchFailedException due to executor running out of memory
Description: When the executor runs out of memory, the following exception may occur.
Resolution: From the Analyze page, perform the following steps in Spark Submit Command Line Options:
Set a higher value for the executor memory, using one of the following commands:
--conf spark.executor.memory= <XX>gOR
--executor-memory <XX>G
Increase the number of shuffle partitions, using the following command:
--spark.sql.shuffle.partitions
Executor container killed by YARN for exceeding memory limits
Description: When the container hosting the executor needs more memory for overhead tasks or executor tasks, the following error occurs.
org.apache.spark.SparkException: Job aborted due to stage failure: Task X in stage X.X failed X times,most recent failure: Lost task X.X in stage X.X (TID XX, XX.XX.X.XXX, executor X): ExecutorLostFailure(executor X exited caused by one of the running tasks) Reason: Container killed by YARN for exceeding memory limits. XX.X GBof XX.X GB physical memory used. Consider boosting spark.yarn.executor.memoryOverhead
Resolution: Set a higher value for
spark.yarn.executor.memoryOverheadbased on the requirements of the job. The executor memory overhead value increases with the executor size (approximately by 6-10%). As a best practice, modify the executor memory value accordingly.To set a higher value for executor memory overhead, enter the following command in Spark Submit Command Line Options on the Analyze page:
--conf spark.yarn.executor.memoryOverhead=XXXXNote
For Spark 2.3 and later versions, use the new parameter
spark.executor.memoryOverheadinstead ofspark.yarn.executor.memoryOverhead.If increasing the executor memory overhead value or executor memory value does not resolve the issue, you can either use a larger instance, or reduce the number of cores.
To reduce the njmber of cores, enter the following in the Spark Submit Command Line Options on the Analyze page:
--executor-cores=XX. Reducing the number of cores can waste memory, but the job will run.