Skip to content

Instantly share code, notes, and snippets.

@marty-suzuki
Last active October 17, 2021 04:40
Show Gist options
  • Save marty-suzuki/614c52faa9ea7d04fbc3262a3eb72dc4 to your computer and use it in GitHub Desktop.
Save marty-suzuki/614c52faa9ea7d04fbc3262a3eb72dc4 to your computer and use it in GitHub Desktop.
An Adapter of Kotlin native GarbageCollector for iOS project
import kotlin.native.internal.GC as internalGC
/**
* https://github.com/JetBrains/kotlin/blob/1.5.30/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/GC.kt
*/
object GC {
var threshold: Int
get() = internalGC.threshold
set(value) { internalGC.threshold = value }
var collectCyclesThreshold: Long
get() = internalGC.collectCyclesThreshold
set(value) { internalGC.collectCyclesThreshold = value }
var thresholdAllocations: Long
get() = internalGC.thresholdAllocations
set(value) { internalGC.thresholdAllocations = value }
var autotune: Boolean
get() = internalGC.autotune
set(value) { internalGC.autotune = value }
// Causes Exception because no implementations
// new MM: https://github.com/JetBrains/kotlin/blob/1.5.30/kotlin-native/runtime/src/mm/cpp/Memory.cpp
// legacy MM: https://github.com/JetBrains/kotlin/blob/1.5.30/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp
var cyclicCollectorEnabled: Boolean
get() = internalGC.cyclicCollectorEnabled
set(value) { internalGC.cyclicCollectorEnabled = value }
fun detectCycles(): Array<Any>? = internalGC.detectCycles()
fun collect() = internalGC.collect()
// Causes ThrowIllegalArgumentException
// new MM: https://github.com/JetBrains/kotlin/blob/1.5.30/kotlin-native/runtime/src/mm/cpp/Memory.cpp#L273-L276
// legacy MM: https://github.com/JetBrains/kotlin/blob/1.5.30/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp#L3470-L3477
fun collectCyclic() = internalGC.collectCyclic()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment