How Kotlin/Native garbage collector works in C?
Asked Answered
T

1

14

I'm found some explanation of Kotlin/Native memory management model in JetBrains FAQ.

A: Kotlin/Native provides an automated memory management scheme, similar to what Java or Swift provides. The current implementation includes an automated reference counter with a cycle collector to collect cyclical garbage.

I understand more or less how it works in Java or Kotlin (JVM). Can any describe detailed how memory is managed in Kotlin/Native in projects with C?

Also, if there is the garbage collector, why do we need a Kotlin/Native function memScoped { }?

Also, I found here :

Kotlin/Native is a technology for compiling Kotlin to native binaries that run without any VM. Broadly speaking, Native code is any code whose memory is not managed by the underlying framework but has to be managed by the programmer themselves. i.e. there is no Garbage collection. e.g. C++’ delete and C’s free

which in my opinion contradicts what is written in JetBrains FAQ

Toadstool answered 10/4, 2019 at 10:38 Comment(0)
R
16

Memory Management in K/N is provided by the runtime. It consists of two main parts: automatic reference counting and cycle collector. This provides one with availability to write the code just as in the Kotlin/JVM. Some details on this topic can be found digging inside this file but all you need to know is that it is automatic by default.


About MemScoped etc. When you use interoperability with C, you have to deal with managing such a resource as native memory. Native memory is the memory provided to the application process by the operating system. As it has nothing to do with the Kotlin code, this resource can't be managed by K/N runtime. But all C struct s and variables you are going to use must be allocated there. You can do it straight by calling nativeHeap.alloc() function. When the need of this memory is gone, it can be freed by nativeHeap.free(). But to make your experience more comfortable, K/N also has Arena class, implementing region-based memory management. It simplifies memory management to just a series of alloc() wherever you need, and one deallocation by .clear() for all the region. Also, there is a MemScoped {} blocks, that covers Arena from you, and lets to forget about even the freeing native memory. So in your code that includes some elements from C, you can just write MemScoped { ... }, and then put operations into it. You can see a lot of examples of this approach in the samples from the K/N repository

Reims answered 30/5, 2019 at 14:48 Comment(3)
When is garbage collection triggered in K/N? Does the compiler insert calls to garbageCollect when you try to allocate memory or when a stack frame is popped or something like this? Or is a thread created that manages garbage collection?Hyperbolic
Hi @KerrickStaley! As one can see in the code above, garbageCollect() is being called by the runtime in some operations(usually, with conditions check). Also, maybe this video would be interesting fur you.Reims
Thanks! The video, along with a closer read of the Memory.cpp file, was helpful :)Hyperbolic

© 2022 - 2024 — McMap. All rights reserved.