Before starting the article, I think for the time being that you already understand the process of JVM creation of objects to allocate memory addresses, and also know the JVM memory division. Based on humanitarianism, let me put a picture for you to compare it.
JVM memory structure
Heap memory partition structure
Is there a multi-thread safety problem in the memory allocated in the heap area?
Answer: may exist ;
new Object();
We all know that the above operations will eventually need to open up a memory space in the heap memory, so think about this problem, the heap area is shared by all threads, then when the JVM frequently creates objects, it is to open up space in the heap memory under concurrent circumstances. There is no security issue.
So in order to solve this problem, the first thing we think of is locking, but there is a problem with locking, that is, it affects performance.
TLAB appears (Thread Local Allocation Buffer)
Based on the above questions, was introduced. Forcibly translated, 161010761d611a thread local allocation buffer , first look at the picture
statement : To allocate space in the heap memory, first allocate it in the eden area, not directly in the old generation. After the memory allocation is over, there is no Yong GC. If the object is not recycled, then the number of times its survival is Will +1, if this number of times reaches 15 times, then the object is promoted to the old age.
Then we know that the object allocation is first carried out in the eden area, so it is not difficult to understand the above figure, we divide a region in the eden area, we call it TLAB, each TLAB is ready and private, then it is created concurrently In fact, there is no need to perform operations such as locking when the object is involved, so that the existing security problems are solved.
If the allocated TLAB space is used up or the memory space required by the object is greater than the space provided by the TLAB, then the memory space can only be allocated in the common eden area or the old generation.
Summarize
- 1. JVM prefers TLAB to allocate memory space;
- 2. TLAB occupies 1% of the entire eden area, this value can also be customized by parameters;
Another problem can also be inferred from this question, the heap area is not strictly speaking shared by threads.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。