Skip to content

Instantly share code, notes, and snippets.

@fanf
Last active September 5, 2019 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanf/ff39065f7f9738467e40929ad7490da1 to your computer and use it in GitHub Desktop.
Save fanf/ff39065f7f9738467e40929ad7490da1 to your computer and use it in GitHub Desktop.
Scala keySet keep reference on self, forbig GC
package test
import net.sourceforge.sizeof.SizeOf
object CountSetMemory {
def bigArray = new Array(1024*1024)
def main(args: Array[String]): Unit = {
val map = Map(1 -> bigArray, 2 -> bigArray, 3 -> bigArray)
val keys1 = map.keySet
val keys2 = map.keySet.toSeq.toSet
val keys3 = map.keysIterator.toSet
// keys1: 12.003791809082031 Mb
println("keys1: " ++ SizeOf.humanReadable(SizeOf.deepSizeOf(keys1)))
//keys2: 3.8046875 Kb
println("keys2: " ++ SizeOf.humanReadable(SizeOf.deepSizeOf(keys2)))
//keys3: 3.8046875Kb
println("keys3: " ++ SizeOf.humanReadable(SizeOf.deepSizeOf(keys3)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment