JVM principle | what is TLAB

TLAB (Thread Local Allocation Buffer)

1 Overview

TLAB (Thread local Allocation Buffer) local thread allocation buffer, which is a thread-specific memory allocation area, which can solve the problem of memory allocation conflicts;

虚拟机参数 -XX:UseTLAB

When the thread is initialized, it will also apply for a piece of memory of a specified size, which is only used by the current thread , so that each thread has a separate space. If you need to allocate memory, allocate it in its own space, so that there is no competition Situation, can greatly improve the distribution efficiency

The memory of TLAB space is very small. By default, it only -XX:TLABWasteTargetPercentoccupies 1% of the entire Eden space. You can also set the percentage of Eden space occupied by TLAB space through options.

2. The essence of TLAB

In fact, the area is three pointer management: start, topand endeach thread is assigned a space from Eden, for example, says 100KB, as their TLAB, which start and end with a placeholder, identified eden be managed in this TLAB The area is stuck in a piece of space in eden to prevent other threads from allocating here.

Insert picture description here
TLAB just allows each thread to have a private allocation pointer, but the memory space of the underlying object is still accessible to all threads, but other threads cannot allocate in this area. From this point of view, it has been translated into 线程私有分配区more reasonable point
when TLAB with a full (allocation pointer hit the top end of the distribution limit), to apply for a new TLAB, and objects in the old TLAB remain where they do not have anything Pipe-they can't perceive whether they have been allocated from TLAB, but only care that they are allocated in eden.但是如果分配一次以后内存还是不够的话, 则直接移入Eden区

3. Disadvantages of TLAB:

  1. TLABs are usually small, so they can't fit large objects.

    1. The size of the TLAB space is fixed, but at this time a large object, the remaining space of my TLAB can no longer accommodate it. (For example, a 100kb TLAB, a 110KB object comes)
    2. There is a little unused TLAB space left, a bit reluctant. (For example, a 100kb TLAB was installed with 80KB, and another 30KB object came.)
      So JVM developers did the following processing and set the maximum wasted space.
      • When the remaining space is less than the maximum wasted space, the thread to which the TLAB belongs is re-applying for a TLAB space in the Eden area. There is still not enough space for object creation, then your object is too big, go to the Eden area to create it directly!
      • When the remaining space is greater than the maximum wasted space, please go directly to the Eden area to create this large object. My TLAB can't put down the unused space.
  2. When the Eden space is enough, it is okay to apply for TLAB again; but if it is not enough, GC will start in the Eden area of ​​Heap

  3. TLAB allows wasted space , resulting in discontinuous space in the Eden area, and the accumulation of less will add up. I need someone to help take care of it in the future

4. Expansion

4.1 Memory allocation for creating objects

img

  1. The compiler judges whether the object is allocated on the stack or on the heap through escape analysis, and if it is allocated on the heap, proceed to the next step. (Enable escape analysis needs to set jvm parameters)
  2. If tlab can put down the object, allocate it on tlab, otherwise go to the next step.
  3. Reapply for a tlab and try to store the object again. If you can’t fit it, go to the next step.
  4. Add a lock in the eden area and try to store it in the eden area. If it cannot be stored, go to the next step.
  5. Perform Young GC once.
  6. After Young GC, if the eden area still cannot fit the object, it will be allocated directly in the old generation.

Guess you like

Origin blog.csdn.net/weixin_40597409/article/details/115260241