# Trying out the experimental new Kotlin/Native memory model

DevFeed: [Trying out the experimental new Kotlin/Native memory model](<https://devfeed.tech/articles/trying-out-the-experimental-new-kotlin-native-memory-model-25004.md>)

Original publisher: [Read original article](<https://dev.to/touchlab/trying-out-the-experimental-new-kotlin-native-memory-model-235k>)

Author: Russell Wolf

Published: 2021-08-07T15:54:36Z

Content type: tutorial

Language: en

Sources: [DEV Community 👩💻👨💻: Russell Wolf](<https://devfeed.tech/sources/dev-community-russell-wolf.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [Gradle](<https://devfeed.tech/topics/gradle.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>)

Tags: [coding](<https://devfeed.tech/tags/coding.md>), [community](<https://devfeed.tech/tags/community.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [development](<https://devfeed.tech/tags/development.md>), [engineering](<https://devfeed.tech/tags/engineering.md>), [experimental](<https://devfeed.tech/tags/experimental.md>), [gradle](<https://devfeed.tech/tags/gradle.md>), [inclusive](<https://devfeed.tech/tags/inclusive.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-native](<https://devfeed.tech/tags/kotlin-native.md>), [kotlinmultiplatform](<https://devfeed.tech/tags/kotlinmultiplatform.md>), [native](<https://devfeed.tech/tags/native.md>), [software](<https://devfeed.tech/tags/software.md>)

## AI overview

This article explores the experimental new Kotlin/Native memory model available in the Kotlin 1.5.30 early-access release. It explains how to enable the model, disable compiler caching, use coroutines with it, and mutate unfrozen state across threads while noting that freeze semantics still apply in some cases.

## Source excerpt

Photo by Victor Serban on Unsplash Update Aug 12, 2021: This article has been updated with a new section about using the new memory model with kotlinx-coroutines. If you've been following Kotlin/Native at all over the last couple of years, you'll know that it's memory model has been controversial. Last year, the Kotlin team committed to redesigning it, and this year they promised a preview by the end of the summer. Well, there hasn't been an official announcement yet, but that preview is present in the Kotlin 1.5.30 early-access release. TL,DR Update your Kotlin version to 1.5.30-RC Add kotlinOptions.freeCompilerArgs += listOf("-memory-model", "experimental") to your Kotlin/Native compilations Add kotlin.native.cacheKind=none to gradle.properties To use coroutines with the new model, add https://maven.pkg.jetbrains.space/public/p/kotlinx-coroutines/maven to repositories and use version 1.5.1-new-mm-dev1. Mutate unfrozen objects from different threads Wait, what?! Yup! The memory model is controlled with the -memory-model command-line flag. Pass experimental for the new model or strict for the existing one*. Due to current limitations, you also need to disable compiler caching with the kotlin.native.cacheKind=none gradle property. *You can also pass relaxed, but it's probably not a good idea. You can pass the flag to all your Kotlin/Native targets by doing something like this from Gradle: kotlin { targets.withType<KotlinNativeTarget>().all { compilations.all { kotlinOptions.freeCompilerArgs += listOf("-memory-model", "experimental") } } } Now you can freely mutate unfrozen state across threads. Let's see what that looks like. Testing the two models In the current strict memory model, if you wanted to write a function to run code in a background thread, it might look something like this: fun <T> doInBackground(action: () -> T): T { val worker = Worker.start() val future = worker.execute( TransferMode.SAFE, { action.freeze() }, { it() } ) return future.result } The fun