Upgrade to Pro — share decks privately, control downloads, hide ads and more …

KMM new memory management

KMM new memory management

Anna Zharkova

January 25, 2022
Tweet

More Decks by Anna Zharkova

Other Decks in Programming

Transcript

  1. Introduction • Mobile developer since 2013 • Lead Mobile developer

    in «Usetech software» . • Native mobile development, iOS and Android (Swift/Objective-C, Kotlin/Java) cross-platform mobile development (Xamarin, Kotlin multiplatform) • Leading mobile team. Mentorship • Public speaking (Mobius Moscow 2020, AppsLive 2020) • Tutor in Otus (iOS advanced) • Writing articles about mobile development (especially iOS and KMM ) • Women Techmaker Ambassador since 2021 2
  2. Goals. Today discuss: • Kotlin Multiplatform and common concurrency •

    Coroutines, solving problems in Kotlin/Native • Background Workers • Advanced Sharing • Flows & Kotlin/Native, iOS side 3
  3. Kotlin Multiplatform 5 • SDK, not framework • JVM for

    Android and LLVM for iOS • Total sharing (almost)
  4. Concurrency in Kotlin/Native 7 expect Dispatchers iOS: actual Dispatchers Android:

    actual Dispatchers Kotlin Native Kotlin JVM Kotlin JVM concurrency != Kotlin Native concurrency
  5. Networking as a case Common implementation (e.g. use Ktor) ->

    Coroutines Separated native -> Coroutines, native concurrency 8 Depends on app architecture
  6. Sample. Networking with Ktor and Coroutines 9 Common concurrency with

    Coroutines Ktor implements async work under its hood
  7. Coroutines vs GCD. Suspended 10 Suspended not blocking in iOS

    Suspended Kotlin easy transforms into Swift (since Kotlin Native 1.4)
  8. Problem of common architecture 11 Network service API service Common

    architecture iOS View Android View Common Work with coroutines Common architecture = common scope
  9. Setting up thread and contexts 12 Android/ iOS : Dispatchers.Default

    (Android) Dispatchers.Main – unique for platform
  10. CoroutineDispatcher for iOS 19 Not available to create working dispatcher

    bound to DispatchersQueue background For Ktor we can use MainDispatcher
  11. How to work with threads in Kotlin/Native 21 • Mutable

    == for one thread • Immutable == multiple thread In Kotlin JVM everything is already done
  12. How to work with threads in Kotlin/Native 22 Object should

    be freezed to become immutable https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concur rent/freeze.html
  13. Why not to use GlobalScope 24 Default background work? Not

    recommended Potential leaks and losing of callbacks ^ l Fix
  14. Sharing data and variables 37 Everything to share should be

    freezed InvalidMutabilityException: mutation attempt of frozen
  15. What’s new 58 Support safe sharing and chage of thread

    contexts: 1. Multithreading without freeze() 2. AtomicReferences/ FreezableAtomicReference without leaks 3. No need of ShareImmutable when use globals 4. Worker.execute doesn’t return isolated graph of dependencies
  16. What’s new 59 1. Atomic references need freeze(). Use FreezableAtomicReference

    instead 2. Lazy globals. @'EagerInitialization for immediate initialization 3. No guarantee that suspend returns to main 4. Deinit could be called in other thread
  17. Use logic with main thread 67 It also could be

    useful to add autoreleasepool in order to prevent memory leaks!
  18. Summary. 70 • Concurrency in Kotlin Native could be really

    hard • But there is full toolkit to provide this • New memory management model is going to solve all our pains • There will be improvements with leaks and garbage collection
  19. Sources 71 • https://betterprogramming.pub/using-kotlin-flow-in-swift-3e7b53f559b6 • https://github.com/JetBrains/kotlin-native • https://github.com/JetBrains/kotlin-native/blob/master/IMMUTABILITY.md • https://github.com/JetBrains/kotlin/blob/master/kotlin-native/NEW_MM.md

    • https://github.com/anioutkazharkova/kmm-concurrency-helper • https://kmpdocs.suparnatural.com/concurrency/#worker • https://elizarov.medium.com/the-reason-to-avoid-globalscope-835337445abc