I have the code below that first read a file and then put these information in a HashMap
(indexCategoryVectors
). The HashMap
contains a String
(key) and a Long
(value). The code uses the Long
value to access a specific position of another file with RandomAccessFile
.
By the information read in this last file and some manipulations the code write new information in another file (filename4
). The only variable that accumulates information is the buffer (var buffer = new ArrayBuffer[Map[Int, Double]]()
) but after each interaction the buffer is cleaned (buffer.clear
).
The foreach
command should run more than 4 million times, and what I'm realizing there is an accumulation in memory. I tested the code with a million times interaction and the code used more than 32GB of memory. I don't know the reason for that, maybe it's about Garbage Collection or anything else in JVM. Does anybody knows what can I do to prevent this memory leak?
def main(args: Array[String]): Unit = {
val indexCategoryVectors = getIndexCategoryVectors("filename1")
val uriCategories = getMappingURICategories("filename2")
val raf = new RandomAccessFile("filename3", "r")
var buffer = new ArrayBuffer[Map[Int, Double]]()
// Through each hashmap key.
uriCategories.foreach(uri => {
var emptyInterpretation = true
uri._2.foreach(categoria => {
val position = indexCategoryVectors.get(categoria)
// go to position
raf.seek(position.get)
var vectorSpace = parserVector(raf.readLine)
buffer += vectorSpace
//write the information of buffer in file
writeInformation("filename4")
buffer.clear
}
})
})
println("Success!")
}