我們知道,spark執行的時候,可以通過 --executor-memory
來設置executor執行時所需的memory。但如果設置的過大,程序是會報錯的,如下
那麼這個值最大能設置多少呢?本文來分析一下。
文中安裝的是Spark1.6.1,安裝在hadoop2.7上。
1、相關的2個參數
1.1 yarn.scheduler.maximum-allocation-mb
這個參數表示每個container能夠申請到的最大內存,一般是集羣統一配置。Spark中的executor進程是跑在container中,所以container的最大內存會直接影響到executor的最大可用內存。當你設置一個比較大的內存時,日誌中會報錯,同時會打印這個參數的值。如下圖 ,6144MB,即6G。
1.2 spark.yarn.executor.memoryOverhead
executor執行的時候,用的內存可能會超過executor-memoy,所以會爲executor額外預留一部分內存。spark.yarn.executor.memoryOverhead代表了這部分內存。這個參數如果沒有設置,會有一個自動計算公式(位於ClientArguments.scala中),代碼如下:
其中,MEMORY_OVERHEAD_FACTOR默認爲0.1,executorMemory爲設置的executor-memory, MEMORY_OVERHEAD_MIN默認爲384m。參數MEMORY_OVERHEAD_FACTOR和MEMORY_OVERHEAD_MIN一般不能直接修改,是Spark代碼中直接寫死的。
2、executor-memory計算
計算公式:
val executorMem = args.executorMemory + executorMemoryOverhead
假設executor-爲X(整數,單位爲M),即
1) 如果沒有設置spark.yarn.executor.memoryOverhead,
executorMem= X+max(X*0.1,384)
2)如果設置了spark.yarn.executor.memoryOverhead(整數,單位是M)
executorMem=X +spark.yarn.executor.memoryOverhead
需要滿足的條件:
executorMem< yarn.scheduler.maximum-allocation-mb
注意:以上代碼位於Client.scala中。
本例中 :
6144=X+max(X*0.1,384)
X=5585.45
向上取整爲5586M,即最大能設置5586M內存。