我编写了以下spark作业来比较重复段落的大量文本文件。我是新手,并且想知道我是否有内存泄漏,因为我有时会遇到内存不足的GC异常。
val conf = ConfigFactory.load()
val partitionsCount = conf.getInt("simtext.partitions")
val minMatchLength = conf.getInt("simtext.minmatchlength")
val hdfsDir1 = conf.getString("simtext.hdfs.dir1")
val hdfsDir2 = conf.getString("simtext.hdfs.dir2")
val outputDir = conf.getString("simtext.hdfs.output")
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("simtext4s with Spark")
val sc = new SparkContext(conf)
val tokenizer = new Tokenizer()
val tokenizedFiles1 = getTokenizedFilesAsRdd(sc, tokenizer, hdfsDir1)
val tokenizedFiles2 = getTokenizedFilesAsRdd(sc, tokenizer, hdfsDir2)
val comparetuples: RDD[CompareTuple] = tokenizedFiles1.cartesian(tokenizedFiles2).map {
case ((name1, tokens1), (name2, tokens2)) =>
CompareTuple(TokenizedText(name1, tokens1), TokenizedText(name2, tokens2))
}
val results: RDD[CompareResult] = comparetuples.map { compareTuple =>
val forwardRefTable: SortedMap[Int, Int] = buildForwardReferenceTable(
compareTuple.combinedTokens,
minMatchLength
)
val similarity: Double = compareTokenLists(
compareTuple.splitIndex,
forwardRefTable,
minMatchLength
)
CompareResult(compareTuple.tokenizedText1.name, compareTuple.tokenizedText2.name, similarity)
}
results.saveAsTextFile(outputDir)
sc.stop()
}
buildForwardRefTable是同一个对象中的方法,如main方法:
def buildForwardReferenceTable(tokens: List[String], minMatchLength: Int): SortedMap[Int, Int] = {
val (_, forwardRefTable) = tokens
.sliding(minMatchLength)
.zipWithIndex
.foldLeft(HashMap.empty[List[String], Int], SortedMap.empty[Int, Int]) {
case ((lastIndexAcc, forwardRefTableAcc), (slice, index: Int)) =>
if (lastIndexAcc.contains(slice)) {
val updatedForwardRefTable = forwardRefTableAcc + (lastIndexAcc.get(slice).get -> index)
val updatedLastIndexAcc = lastIndexAcc + (slice -> index)
(updatedLastIndexAcc, updatedForwardRefTable)
} else {
val updatedLastIndexAcc = lastIndexAcc + (slice -> index)
(updatedLastIndexAcc, forwardRefTableAcc)
}
}
forwardRefTable
}
我的问题是关于创建结果值的main方法中的map函数。 它为每个比较元组生成一个前向引用表来比较文本。是否存储这些前向引用表,直到所有比较元素上的整个映射函数完成为止?因为我想要的行为是它生成前向引用表并在计算相似性后将其从内存中删除。
是否还有其他可能导致内存泄漏的重大错误?这是我的第一个火花工作。