Last active
September 5, 2019 15:53
-
-
Save fanf/ff39065f7f9738467e40929ad7490da1 to your computer and use it in GitHub Desktop.
Scala keySet keep reference on self, forbig GC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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