自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

還不收藏?Spark動態(tài)內(nèi)存管理源碼解析!

存儲 存儲軟件 Spark
Spark有兩種內(nèi)存管理模式,靜態(tài)內(nèi)存管理(Static MemoryManager)和動態(tài)(統(tǒng)一)內(nèi)存管理(Unified MemoryManager)。動態(tài)內(nèi)存管理從Spark1.6開始引入,在SparkEnv.scala中的源碼可以看到,Spark目前默認(rèn)采用動態(tài)內(nèi)存管理模式,若將spark.memory.useLegacyMode設(shè)置為true,則會改為采用靜態(tài)內(nèi)存管理。

一、Spark內(nèi)存管理模式

Spark有兩種內(nèi)存管理模式,靜態(tài)內(nèi)存管理(Static MemoryManager)和動態(tài)(統(tǒng)一)內(nèi)存管理(Unified MemoryManager)。動態(tài)內(nèi)存管理從Spark1.6開始引入,在SparkEnv.scala中的源碼可以看到,Spark目前默認(rèn)采用動態(tài)內(nèi)存管理模式,若將spark.memory.useLegacyMode設(shè)置為true,則會改為采用靜態(tài)內(nèi)存管理。

  1. // SparkEnv.scala 
  2.     val useLegacyMemoryManager = conf.getBoolean("spark.memory.useLegacyMode"false
  3.     val memoryManager: MemoryManager = 
  4.       if (useLegacyMemoryManager) { 
  5.         new StaticMemoryManager(conf, numUsableCores) 
  6.       } else { 
  7.         UnifiedMemoryManager(conf, numUsableCores) 
  8.       } 

[[231842]]

二、Spark動態(tài)內(nèi)存管理空間分配

相比于Static MemoryManager模式,Unified MemoryManager模型打破了存儲內(nèi)存和運行內(nèi)存的界限,使每一個內(nèi)存區(qū)能夠動態(tài)伸縮,降低OOM的概率。由上圖可知,executor JVM內(nèi)存主要由以下幾個區(qū)域組成:

(1)Reserved Memory(預(yù)留內(nèi)存):這部分內(nèi)存預(yù)留給系統(tǒng)使用,默認(rèn)為300MB,可通過spark.testing.reservedMemory進(jìn)行設(shè)置。

  1. // UnifiedMemoryManager.scala 
  2. private val RESERVED_SYSTEM_MEMORY_BYTES = 300 * 1024 * 1024 

另外,JVM內(nèi)存的最小值也與reserved Memory有關(guān),即minSystemMemory = reserved Memory*1.5,即默認(rèn)情況下JVM內(nèi)存最小值為300MB*1.5=450MB。

  1. // UnifiedMemoryManager.scala 
  2.     val minSystemMemory = (reservedMemory * 1.5).ceil.toLong 

(2)Spark Memeoy:分為execution Memory和storage Memory。去除掉reserved Memory,剩下usableMemory的一部分用于execution和storage這兩類堆內(nèi)存,默認(rèn)是0.6,可通過spark.memory.fraction進(jìn)行設(shè)置。例如:JVM內(nèi)存是1G,那么用于execution和storage的默認(rèn)內(nèi)存為(1024-300)*0.6=434MB。

  1. // UnifiedMemoryManager.scala 
  2.     val usableMemory = systemMemory - reservedMemory 
  3.     val memoryFraction = conf.getDouble("spark.memory.fraction", 0.6) 
  4.     (usableMemory * memoryFraction).toLong 

他們的邊界由spark.memory.storageFraction設(shè)定,默認(rèn)為0.5。即默認(rèn)狀態(tài)下storage Memory和execution Memory為1:1.

  1. // UnifiedMemoryManager.scala 
  2.      onHeapStorageRegionSize = 
  3.         (maxMemory * conf.getDouble("spark.memory.storageFraction", 0.5)).toLong, 
  4.       numCores = numCores) 

(3)user Memory:剩余內(nèi)存,用戶根據(jù)需要使用,默認(rèn)占usableMemory的(1-0.6)=0.4.

三、內(nèi)存控制詳解

首先我們先來了解一下Spark內(nèi)存管理實現(xiàn)類之前的關(guān)系。

1.MemoryManager主要功能是:(1)記錄用了多少StorageMemory和ExecutionMemory;(2)申請Storage、Execution和Unroll Memory;(3)釋放Stroage和Execution Memory。

Execution內(nèi)存用來執(zhí)行shuffle、joins、sorts和aggegations操作,Storage內(nèi)存用于緩存和廣播數(shù)據(jù),每一個JVM中都存在著一個MemoryManager。構(gòu)造MemoryManager需要指定onHeapStorageMemory和onHeapExecutionMemory參數(shù)。

  1. // MemoryManager.scala 
  2. private[spark] abstract class MemoryManager( 
  3.     conf: SparkConf, 
  4.     numCores: Int
  5.     onHeapStorageMemory: Long, 
  6.     onHeapExecutionMemory: Long) extends Logging { 

創(chuàng)建StorageMemoryPool和ExecutionMemoryPool對象,用來創(chuàng)建堆內(nèi)或堆外的Storage和Execution內(nèi)存池,管理Storage和Execution的內(nèi)存分配。

  1. // MemoryManager.scala 
  2.   @GuardedBy("this"
  3.   protected val onHeapStorageMemoryPool = new StorageMemoryPool(this, MemoryMode.ON_HEAP) 
  4.   @GuardedBy("this"
  5.   protected val offHeapStorageMemoryPool = new StorageMemoryPool(this, MemoryMode.OFF_HEAP) 
  6.   @GuardedBy("this"
  7.   protected val onHeapExecutionMemoryPool = new ExecutionMemoryPool(this, MemoryMode.ON_HEAP) 
  8.   @GuardedBy("this"
  9.   protected val offHeapExecutionMemoryPool = new ExecutionMemoryPool(this, MemoryMode.OFF_HEAP) 

默認(rèn)情況下,不使用堆外內(nèi)存,可通過saprk.memory.offHeap.enabled設(shè)置,默認(rèn)堆外內(nèi)存為0,可使用spark.memory.offHeap.size參數(shù)設(shè)置。

  1. // All the code you will ever need 
  2.  final val tungstenMemoryMode: MemoryMode = { 
  3.     if (conf.getBoolean("spark.memory.offHeap.enabled"false)) { 
  4.       require(conf.getSizeAsBytes("spark.memory.offHeap.size", 0) > 0, 
  5.         "spark.memory.offHeap.size must be > 0 when spark.memory.offHeap.enabled == true"
  6.       require(Platform.unaligned(), 
  7.         "No support for unaligned Unsafe. Set spark.memory.offHeap.enabled to false."
  8.       MemoryMode.OFF_HEAP 
  9.     } else { 
  10.       MemoryMode.ON_HEAP 
  11.     } 
  12.   } 
  1. // MemoryManager.scala  
  2.  protected[this] val maxOffHeapMemory = conf.getSizeAsBytes("spark.memory.offHeap.size", 0) 

釋放numBytes字節(jié)的Execution內(nèi)存方法

  1. // MemoryManager.scala 
  2. def releaseExecutionMemory( 
  3.       numBytes: Long, 
  4.       taskAttemptId: Long, 
  5.       memoryMode: MemoryMode): Unit = synchronized { 
  6.     memoryMode match { 
  7.       case MemoryMode.ON_HEAP => onHeapExecutionMemoryPool.releaseMemory(numBytes, taskAttemptId) 
  8.       case MemoryMode.OFF_HEAP => offHeapExecutionMemoryPool.releaseMemory(numBytes, taskAttemptId) 
  9.     } 
  10.   } 

釋放指定task的所有Execution內(nèi)存并將該task標(biāo)記為inactive。

  1. // MemoryManager.scala 
  2.  private[memory] def releaseAllExecutionMemoryForTask(taskAttemptId: Long): Long = synchronized { 
  3.     onHeapExecutionMemoryPool.releaseAllMemoryForTask(taskAttemptId) + 
  4.       offHeapExecutionMemoryPool.releaseAllMemoryForTask(taskAttemptId) 
  5.   } 

釋放numBytes字節(jié)的Stoarge內(nèi)存方法

  1. // MemoryManager.scala 
  2. def releaseStorageMemory(numBytes: Long, memoryMode: MemoryMode): Unit = synchronized { 
  3.     memoryMode match { 
  4.       case MemoryMode.ON_HEAP => onHeapStorageMemoryPool.releaseMemory(numBytes) 
  5.       case MemoryMode.OFF_HEAP => offHeapStorageMemoryPool.releaseMemory(numBytes) 
  6.     } 
  7.   } 

釋放所有Storage內(nèi)存方法

  1. // MemoryManager.scala 
  2. final def releaseAllStorageMemory(): Unit = synchronized { 
  3.     onHeapStorageMemoryPool.releaseAllMemory() 
  4.     offHeapStorageMemoryPool.releaseAllMemory() 
  5.   } 

2.接下來我們了解一下,UnifiedMemoryManager是如何對內(nèi)存進(jìn)行控制的?動態(tài)內(nèi)存是如何實現(xiàn)的呢?

UnifiedMemoryManage繼承了MemoryManager

  1. // UnifiedMemoryManage.scala 
  2. private[spark] class UnifiedMemoryManager private[memory] ( 
  3.     conf: SparkConf, 
  4.     val maxHeapMemory: Long, 
  5.     onHeapStorageRegionSize: Long, 
  6.     numCores: Int
  7.   extends MemoryManager( 
  8.     conf, 
  9.     numCores, 
  10.     onHeapStorageRegionSize, 
  11.     maxHeapMemory - onHeapStorageRegionSize) { 

重寫了maxOnHeapStorageMemory方法,***Storage內(nèi)存=***內(nèi)存-***Execution內(nèi)存。

  1. // UnifiedMemoryManage.scala 
  2.  override def maxOnHeapStorageMemory: Long = synchronized { 
  3.     maxHeapMemory - onHeapExecutionMemoryPool.memoryUsed 
  4.   } 

核心方法acquireStorageMemory:申請Storage內(nèi)存。

  1. // UnifiedMemoryManage.scala 
  2. override def acquireStorageMemory( 
  3.       blockId: BlockId, 
  4.       numBytes: Long, 
  5.       memoryMode: MemoryMode): Boolean = synchronized { 
  6.     assertInvariants() 
  7.     assert(numBytes >= 0) 
  8.     val (executionPool, storagePool, maxMemory) = memoryMode match { 
  9.       //根據(jù)不同的內(nèi)存模式去創(chuàng)建StorageMemoryPool和ExecutionMemoryPool 
  10.       case MemoryMode.ON_HEAP => ( 
  11.         onHeapExecutionMemoryPool, 
  12.         onHeapStorageMemoryPool, 
  13.         maxOnHeapStorageMemory) 
  14.       case MemoryMode.OFF_HEAP => ( 
  15.         offHeapExecutionMemoryPool, 
  16.         offHeapStorageMemoryPool, 
  17.         maxOffHeapMemory) 
  18.     } 
  19.     if (numBytes > maxMemory) { 
  20.       // 若申請內(nèi)存大于***內(nèi)存,則申請失敗 
  21.       logInfo(s"Will not store $blockId as the required space ($numBytes bytes) exceeds our " + 
  22.         s"memory limit ($maxMemory bytes)"
  23.       return false 
  24.     } 
  25.     if (numBytes > storagePool.memoryFree) { 
  26.       // 如果Storage內(nèi)存池沒有足夠的內(nèi)存,則向Execution內(nèi)存池借用 
  27.       val memoryBorrowedFromExecution = Math.min(executionPool.memoryFree, numBytes)//當(dāng)Execution內(nèi)存有空閑時,Storage才能借到內(nèi)存 
  28.       executionPool.decrementPoolSize(memoryBorrowedFromExecution)//縮小Execution內(nèi)存 
  29.       storagePool.incrementPoolSize(memoryBorrowedFromExecution)//增加Storage內(nèi)存 
  30.     } 
  31.     storagePool.acquireMemory(blockId, numBytes) 
  32.   } 

核心方法acquireExecutionMemory:申請Execution內(nèi)存。

  1. // UnifiedMemoryManage.scala 
  2. override private[memory] def acquireExecutionMemory( 
  3.       numBytes: Long, 
  4.       taskAttemptId: Long, 
  5.       memoryMode: MemoryMode): Long = synchronized {//使用了synchronized關(guān)鍵字,調(diào)用acquireExecutionMemory方法可能會阻塞,直到Execution內(nèi)存池有足夠的內(nèi)存。 
  6.    ... 
  7.     executionPool.acquireMemory( 
  8.       numBytes, taskAttemptId, maybeGrowExecutionPool, computeMaxExecutionPoolSize) 
  9.   } 

方法***調(diào)用了ExecutionMemoryPool的acquireMemory方法,該方法的參數(shù)需要兩個函數(shù):maybeGrowExecutionPool()和computeMaxExecutionPoolSize()。

每個Task能夠使用的內(nèi)存被限制在pooSize / (2 * numActiveTask) ~ maxPoolSize / numActiveTasks。其中maxPoolSize代表了execution pool的***內(nèi)存,poolSize表示當(dāng)前這個pool的大小。

  1. // ExecutionMemoryPool.scala 
  2.       val maxPoolSize = computeMaxPoolSize() 
  3.       val maxMemoryPerTask = maxPoolSize / numActiveTasks 
  4.       val minMemoryPerTask = poolSize / (2 * numActiveTasks) 

maybeGrowExecutionPool()方法實現(xiàn)了如何動態(tài)增加Execution內(nèi)存區(qū)的大小。在每次申請execution內(nèi)存的同時,execution內(nèi)存池會進(jìn)行多次嘗試,每次嘗試都可能會回收一些存儲內(nèi)存。

 

  1. // UnifiedMemoryManage.scala  
  2.      def maybeGrowExecutionPool(extraMemoryNeeded: Long): Unit = {  
  3.       if (extraMemoryNeeded > 0) {//如果申請的內(nèi)存大于0  
  4.         //計算execution可借到的storage內(nèi)存,是storage剩余內(nèi)存和可借出內(nèi)存的***值  
  5.         val memoryReclaimableFromStorage = math.max(  
  6.           storagePool.memoryFree,  
  7.           storagePool.poolSize - storageRegionSize)  
  8.         if (memoryReclaimableFromStorage > 0) {//如果可以申請到內(nèi)存  
  9.           val spaceToReclaim = storagePool.freeSpaceToShrinkPool(  
  10.             math.min(extraMemoryNeeded, memoryReclaimableFromStorage))//實際需要的內(nèi)存,取實際需要的內(nèi)存和storage內(nèi)存區(qū)域全部可用內(nèi)存大小的最小值  
  11.           storagePool.decrementPoolSize(spaceToReclaim)//storage內(nèi)存區(qū)域減少  
  12.           executionPool.incrementPoolSize(spaceToReclaim)//execution內(nèi)存區(qū)域增加  
  13.         }  
  14.       }  
  15.     }  
責(zé)任編輯:武曉燕 來源: 若澤大數(shù)據(jù)
相關(guān)推薦

2024-01-26 16:28:28

C++動態(tài)內(nèi)存開發(fā)

2022-04-26 06:21:59

編程動態(tài)內(nèi)存

2022-01-13 10:30:21

C語言內(nèi)存動態(tài)

2012-04-01 14:38:06

Windows Ser虛擬化

2025-03-26 00:00:05

2021-02-08 23:51:31

開源工具代碼

2019-04-17 14:44:42

Spark內(nèi)存源碼

2019-10-10 16:20:23

spark內(nèi)存管理

2018-12-18 14:37:26

Spark內(nèi)存管理

2010-08-18 10:05:28

Hyper-V動態(tài)內(nèi)存

2022-01-07 15:10:53

C++動態(tài)內(nèi)存

2019-05-30 11:04:52

內(nèi)存Spark管理

2021-05-21 09:25:11

鴻蒙HarmonyOS應(yīng)用

2010-03-02 08:53:59

Windows 8動態(tài)內(nèi)存

2010-03-01 09:09:21

Windows 8動態(tài)內(nèi)存

2022-03-18 22:39:57

動態(tài)內(nèi)存malloc

2011-07-28 10:03:53

Hyper-V動態(tài)內(nèi)存

2011-07-20 13:47:14

CC++

2015-11-26 11:02:37

微軟LinuxHyper-V

2017-04-01 14:01:50

Apache Spar內(nèi)存管理
點贊
收藏

51CTO技術(shù)棧公眾號