JetBrains / kotlin-native

Kotlin/Native infrastructure
Apache License 2.0
7.02k stars 566 forks source link

Use NativePlacement with Kotlin classes #1405

Closed barsan-md closed 6 years ago

barsan-md commented 6 years ago

I was looking at https://github.com/JetBrains/kotlin-native/blob/master/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt Though it's purpose is to allow the interoperability with C language it seems like a general interface to use the native memory. Is it possible through the facilities provided by K/N to allocate in the native memory classes defined by Kotlin (with all the semantics of the language like method virtual call, object classes, ...) and avoid garbage collection altogether?

I know this question has been asked before, multiple times and the answer was to not try to do that, because Kotlin is an application language and memory allocation should be at the discretion of the compiler, which are smarter than programmers. Yet given the large amount of work to let Kotlin easily interop with other languages (C, Java), I was wondering if it's technically possible such a use case, or considered, or planned, or at least desired.

olonho commented 6 years ago

As Kotlin is heavily OOP language it has certain features, such as autoboxing, exceptions and generics that requires automated memory management, so it is pretty unlikely we could work without proper memory manager. Having said that, Kotlin's MM is unlike JVM memory manager in sense that it's much closer to C/C++ memory. So in many cases using APIs to completely disable GC, like this one would be enough: https://github.com/JetBrains/kotlin-native/blob/a6fa19a7fd2dc7b783b83702a546aa8b78f246ea/runtime/src/main/kotlin/konan/internal/GC.kt#L49