From d1892a35baa74fea99899a2ba27a0f2b0acc6242 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Fri, 30 Dec 2016 13:51:52 +0700 Subject: [PATCH] stdlib: disable HashMap views caching because it leads to memory leaks due to reference cycles --- .../main/kotlin/kotlin/collections/HashMap.kt | 57 ++++++++++++------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/runtime/src/main/kotlin/kotlin/collections/HashMap.kt b/runtime/src/main/kotlin/kotlin/collections/HashMap.kt index f968c6e901f..46d155a9752 100644 --- a/runtime/src/main/kotlin/kotlin/collections/HashMap.kt +++ b/runtime/src/main/kotlin/kotlin/collections/HashMap.kt @@ -13,9 +13,9 @@ class HashMap private constructor( override var size: Int = 0 private set - private var keysView: HashSet? = null - private var valuesView: HashMapValues? = null - private var entriesView: HashMapEntrySet? = null + // private var keysView: HashSet? = null + // private var valuesView: HashMapValues? = null + // private var entriesView: HashMapEntrySet? = null // ---------------------------- functions ---------------------------- @@ -85,30 +85,45 @@ class HashMap private constructor( } override val keys: MutableSet get() { - val cur = keysView - return if (cur == null) { - val new = HashSet(this) - keysView = new - new - } else cur + return HashSet(this) + + // Caching creates a reference cycle and thus leads to memory leaks; + // TODO: fix and enable. + + // val cur = keysView + // return if (cur == null) { + // val new = HashSet(this) + // keysView = new + // new + // } else cur } override val values: MutableCollection get() { - val cur = valuesView - return if (cur == null) { - val new = HashMapValues(this) - valuesView = new - new - } else cur + return HashMapValues(this) + + // Caching creates a reference cycle and thus leads to memory leaks; + // TODO: fix and enable. + + // val cur = valuesView + // return if (cur == null) { + // val new = HashMapValues(this) + // valuesView = new + // new + // } else cur } override val entries: MutableSet> get() { - val cur = entriesView - return if (cur == null) { - val new = HashMapEntrySet(this) - entriesView = new - return new - } else cur + return HashMapEntrySet(this) + + // Caching creates a reference cycle and thus leads to memory leaks; + // TODO: fix and enable. + + // val cur = entriesView + // return if (cur == null) { + // val new = HashMapEntrySet(this) + // entriesView = new + // return new + // } else cur } override fun equals(other: Any?): Boolean {