Kotlin
A concise multiplatform language developed by JetBrains
Try the New Kotlin/Native Memory Manager Development Preview
Today we are taking a huge step towards making the process of sharing code between mobile platforms with Kotlin Multiplatform Mobile more enjoyable. The development preview of the new memory management approach for Kotlin/Native is now ready for you to try out in your project! It moves us closer to eliminating the differences between the JVM and Native platforms, allowing you to focus on the business logic of your app when writing common concurrent Kotlin code.
Read on for details:
- Why do we need a new memory manager?
- Benefits of the new approach
- How to try the development preview
- Next steps
Why do we need a new memory manager?
Kotlin Multiplatform Mobile (KMM) allows you to share code between mobile platforms. Usually, you only have to think about platform specifics when you work with platform-specific APIs. However, as Kotlin Multiplatform uses different compiler backends to seamlessly integrate with the iOS and Android ecosystems, it sometimes leads to differences in how you should write shared code targeting different platforms.
Working with concurrent code in KMM projects was such a case. The original Kotlin/Native memory management approach had limitations when it came to concurrency, which created a bunch of problems and led to a steep learning curve for developers trying to share their Kotlin code between iOS and Android.
Three months ago we published an update on our progress with reworking the original approach and some details about memory management design. Today we release the first development preview of the new Kotlin/Native memory manager, which will allow you to make your Android applications work on iOS and create new cross-platform mobile applications much more easily!
Benefits of the new approach
The new Kotlin/Native automatic memory manager lifts the existing restrictions on object sharing between threads and provides fully leak-free concurrent programming primitives that are safe and don’t require any special management or annotations from the developers.
New versions of kotlinx.coroutines and Ktor libraries are already benefiting from the new approach, so you can freely launch coroutines using a multithreaded background dispatcher without having to freeze objects that work in background threads.
With the new approach, it will be much easier to start using KMM in your projects to make your existing Android application work on iOS and to create new features for both platforms simultaneously. Write common code in your favorite language and focus on the business logic of your mobile app – Kotlin will take care of making this code work efficiently on both platforms!
How to try the development preview
If you already have a KMM project – here are instructions on how to turn on the development preview of the new memory manager and migrate your project. With the new approach, you can remove all of the freeze()
calls from your code and everything will work as usual! The only exception is when your projects or dependencies are using the AtomicReference
class or explicitly require the object to be frozen.
If you haven’t had the chance to create your own KMM project, but are interested in cross-platform mobile development with Kotlin – you can play with our production sample, which uses new versions of kotlinx.coroutines and Ktor libraries to load XML and parse it in the background.
You also see how the new approach simplifies development by checking out the updated Kotlin/Native concurrency hands-on. The freeze()
call, which was needed to perform background jobs, has been removed. Now the samples that previously failed, which demonstrate working in the background, don’t produce any compile or runtime errors!
Next steps
The development preview uses a classic stop-the-world mark-sweep garbage collector. It is simple and not optimal, but it did allow us to get the first correct implementation and prepare the infrastructure for future garbage collector evolution. Your feedback is vital to reach the next step of this challenging project! If you run into any problems while migrating your projects, report issues to our issue tracker, YouTrack.
You will be able to use the new memory manager development preview in your project in Kotlin 1.6. We continue to support the original Kotlin/Native memory management scheme to simplify the migration of existing Kotlin/Native applications. You’ll be able to choose your memory manager implementation when building your Kotlin/Native application via compiler flags or Gradle project properties.
Now we are working on the performant production-ready approach and are looking at more sophisticated and efficient garbage collector algorithms, like concurrent mark-sweep. We would appreciate it if you paid special attention to the possible performance issues of the development preview. It showed 5 times regression on some of our examples. If you observe anything more significant in your applications, please report it to a dedicated meta-issue.
Read more
- Kotlin/Native memory management roadmap blog post
- Kotlin/Native Memory Management update blog post
- Prototype of a new garbage collector in the roadmap