如何确保未使用的蓄电池的垃圾收集?

时间:2015-01-21 11:58:45

标签: garbage-collection apache-spark accumulator

我遇到了Spark上的累加器不能成为GC的问题。

def newIteration (lastParams: Accumulable[Params, (Int, Int, Int)], lastChosens: RDD[Document], i: Int): Params = {
    if (i == maxIteration)
        return lastParams.value

    val size1: Int = 100
    val size2: Int = 1000

    // each iteration generates a new accumulator
    val params = sc.accumulable(Params(size1, size2))

    // there is map operation here
    // if i only use lastParams, the result in not updated
    // but params can solve this problem 
    val chosen = data.map {
        case(Document(docID, content)) => {
            lastParams += (docID, content, -1)
            val newContent = lastParams.localValue.update(docID, content)
            lastParams += (docID, newContent, 1)
            params += (docID, newContent, 1)
            Document(docID, newContent)
        }
    }.cache()
    chosen.count()
    lastChosens.unpersist()
    return newIteration(params, chosen, i + 1)
}

问题是它分配的内存总是在增长,直到内存限制。似乎lastParms不是GC。类RDDBroadcast有一个方法unpersist(),但我在文档中找不到这样的方法。

为什么Accumulable不能自动生成GC,或者是否有更好的解决方案?

1 个答案:

答案 0 :(得分:3)

更新(2016年4月22日):SPARK-3885 Provide mechanism to remove accumulators once they are no longer used现已解决。

正在进行的工作是在不再引用蓄电池时添加对自动垃圾收集蓄电池的支持。有关此功能的跟踪进度,请参阅SPARK-3885。目前正在审核中的Spark PR #4021是此功能的补丁。我希望这可以包含在Spark 1.3.0中。