- Mastering High Performance with Kotlin
- Igor Kucherenko
- 348字
- 2021-06-25 20:55:23
Java Memory Model (JMM)
The JMM pides the memory space between thread stacks and the heap. Each application has at least one thread, which is referred to as the main thread. When a new thread starts, a new stack is created. If an application has several threads, the simplified memory model may look like this:

The thread stack is a stack of blocks. Whenever a thread calls a method, a new block is created and added to the stack. This is also referred to as the call stack. This block contains all the local variables that were created in its scope. The local variables cannot be shared between threads, even if threads are executing the same method. A block fully stores all local variables of primitive types and references to objects. One thread can only pass copies of local primitive variables or references to another thread:

Kotlin doesn't have primitive types, in contrast to Java, but Kotlin does compile into the same bytecode as Java. And if you don't manipulate a variable in the same way as an object, then the generated bytecode will contain the variable of a primitive type:
fun main(vars: Array<String>) {
val localVariable = 0
}
The simplified generated bytecode will look like this:
public final static main([Ljava/lang/String;)V
LOCALVARIABLE localVariable I L2 L3 1
But if you specify the type of the localVariable as Nullable, as follows:
val localVariable: Int? = null
Then this variable will be represented as an object in the bytecode:
LOCALVARIABLE localVariable Ljava/lang/Integer; L2 L3 1
All objects are contained in the heap even if they're local variables. In the case of local primitive variables, they'll be destroyed automatically when the execution point of a program leaves the scope of the method. The object can be destroyed only with the GC. So the use of local primitive variables is preferable. Since the Kotlin compiler applies optimizations to variables that can be primitive, in most cases the bytecode will contain variables of primitive types.
This diagram illustrates how two threads can share the same object:

- Getting Started with Gulp(Second Edition)
- ServiceNow Application Development
- 嵌入式軟件系統測試:基于形式化方法的自動化測試解決方案
- JavaScript全程指南
- Visual FoxPro程序設計教程
- C語言程序設計實訓教程
- Learning Linux Binary Analysis
- Visual Basic程序設計習題解答與上機指導
- C語言程序設計學習指導與習題解答
- Flutter跨平臺開發入門與實戰
- Node.js全程實例
- C#應用程序設計教程
- 案例式C語言程序設計實驗指導
- 零基礎學C語言(升級版)
- 玩轉.NET Micro Framework移植:基于STM32F10x處理器