Original | tortured soul: memory allocation process Java objects is how to ensure thread-safe?

JVM memory structure, knowledge is very important, I believe that every meditation ready for the interview programmers can clear the heap, stack, and other methods area described more clearly.

The figure, according to the author is a "Java Virtual Machine Specification (Java SE 8)" JVM runtime memory described structure painted area. Again, these are the JVM specification defines the logical partitions on different versions of the virtual machine to achieve different vendors, or the same virtual machine are not the same.

Many people know Java object is allocated space in the heap memory ( the JIT to optimize the exception), also known memory allocation process is thread-safe, then the virtual machine in the end is how to ensure thread-safe? This article will briefly explain.

Memory allocation Java objects

We know that, Java is an object-oriented language, objects that we use in Java needs is created, in Java, there are several ways to create an object method, such as the use of new, using reflection, use the Clone method, but in any case, the object in the creation process, need to be memory allocation.

Take the most common new keywords, for example, when the code begins to run after we use new to create an object, a virtual machine to execute this new command when the first checks to new object corresponding to the class whether it has been loaded, if not load the first class were loaded.

After checked by loading class, the memory needs to be allocated to the object, the main memory allocated to store the instance variables of an object.

When performing memory allocation, space needs to be allocated is determined according to the information of the object instance variables and other circumstances, and such a divided heap area (assuming no optimization JIT) from Java.

Depending on the type of garbage collector JVM used, because different algorithms recovery, will lead to different heap memory allocation. The tag - clear memory after recovery algorithms have a large number of discrete memory fragmentation, when allocated to a new object, you need the "free list" to determine a vacant area. (This part is not the focus of this article, readers can learn about themselves.)

In either embodiment, it is determined that ultimately require a memory area for new objects to allocate memory. We know that the memory allocation process objects, primarily references to this memory area of ​​an object, then initialize operation.

So the question arises:

In a concurrent scenario, how memory allocation thread safety procedures? If two threads have the object reference point to the same memory area, how to do.

TLAB

Generally there are two solutions:

  • 1, the operation of the memory space allocated to synchronize and process, using CAS mechanisms, with means to ensure failure retry atomic update operation.
  • 2, each thread in the Java heap preassigned a small memory, and then give time allocated memory object allocated directly own a piece of "private" memory, when this part of the region is used up, a new redistribution " private "memory.

Scheme 1 In each allocation control need to be synchronized, which is relatively inefficient.

Scheme 2 HotSpot VM is employed, this solution is called TLAB allocated, i.e. Thread Local Allocation Buffer. This part of the Buffer is carved out from the heap, but is exclusive of local thread.

It is worth noting that when we say TLAB thread exclusive, but only in the "distribution" This action is a thread exclusive, as in reading, garbage collection and other actions are shared by the threads. But also in the use to be no different.

In addition, TLAB act only on the new generation of Eden Space, when the object is first created into this area, but the new generation of large objects can not allocate memory directly into the old era. Therefore, when writing a Java program, usually multiple small object allocation up more efficient than large objects.

Therefore, although the target by the beginning may TLAB allocate memory, stored in the Eden area, but will still be garbage or be moved to Survivor Space, Old Gen and so on.

I do not know if you have not thought about, we used TLAB after, on TLAB thread exclusive to object to allocate memory, and this is not a conflict, however, TLAB carved out their own piece of memory from the heap of the process may there is a memory safety problem ah.

** Therefore, in the process for dispensing TLAB, or the need for synchronization control. ** But this overhead compared to every single object on the time division synchronous control memory to be much lower.

Whether the virtual machine can use TLAB is selected, you can set -XX: +/- UseTLAB arguments.

TLAB

In order to ensure the safety of memory allocation Java objects and increase efficiency, each thread can be assigned a small Java heap memory in advance, this memory is called TLAB (Thread Local Allocation Buffer), this memory is allocated thread exclusive, read, use, recycling is shared by the threads.

By setting the -XX: TLAB to specify whether to open the assignment +/- UseTLAB parameters.

References:

"In-depth understanding of the Java Virtual Machine" www.cnblogs.com/straybirds/... www.zhihu.com/question/56...

Guess you like

Origin juejin.im/post/5d4250def265da03ab422c79