Migrate to the new memory manager

Edit pageLast modified: 09 April 2025

This guide compares the new Kotlin/Native memory manager with the legacy one and describes how to migrate your projects.

The most noticeable change in the new memory manager is lifting restrictions on object sharing. You don't need to freeze objects to share them between threads, specifically:

  • Top-level properties can be accessed and modified by any thread without using @SharedImmutable.

  • Objects passing through interop can be accessed and modified by any thread without freezing them.

  • Worker.executeAfter no longer requires operations to be frozen.

  • Worker.execute no longer requires producers to return an isolated object subgraph.

  • Reference cycles containing AtomicReference and FreezableAtomicReference do not cause memory leaks.

Apart from easy object sharing, the new memory manager also brings other major changes:

  • Global properties are initialized lazily when the file they are defined in is accessed first. Previously global properties were initialized at the program startup. As a workaround, you can mark properties that must be initialized at the program start with the @EagerInitialization annotation. Before using, check its documentation.

  • by lazy {} properties support thread-safety modes and do not handle unbounded recursion.

  • Exceptions that escape operation in Worker.executeAfter are processed like in other runtime parts, by trying to execute a user-defined unhandled exception hook or terminating the program if the hook was not found or failed with an exception itself.

  • Freezing is deprecated and always disabled.

Follow these guidelines to migrate your projects from the legacy memory manager: