官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 阿拉尔市| 海安县| 南充市| 庆元县| 正镶白旗| 通州市| 志丹县| 宁化县| 夏河县| 孝昌县| 古交市| 福泉市| 建湖县| 尼玛县| 焦作市| 久治县| 平潭县| 通山县| 德格县| 玉屏| 霞浦县| 象州县| 微博| 威宁| 察雅县| 巴中市| 水城县| 益阳市| 纳雍县| 天全县| 沅江市| 日照市| 苍南县| 孝义市| 当雄县| 马尔康县| 尚志市| 和田县| 德庆县| 金湖县| 民乐县|