What is a TLAB (Thread Local Allocation Buffer)?

11,482

The idea of a TLAB is to reduce the need of synchronization between threads. Using TLABs, this need is reduced as any thread has an area it can use and expect that it is the only thread using this area. Assuming that a TLAB can hold 100 objects, a thread would only need to aquire a lock for claiming more memory when allocating the 101st object. Without TLABs, this would be required for every object. The downside is of course that you potentially waste space.

Large objects are typically allocated outside of a TLAB as they void the advantage of reducing the frequency of synchronized memory allocation. Some objects might not even fit inside of a TLAB.

You can set the size of a TLAB using the -XX:TLABSize flag but generally I do not recommend you to mess with these settings unless you really discovered a problem that you can solve by that.

Share:
11,482

Related videos on Youtube

user1745356
Author by

user1745356

Updated on June 06, 2022

Comments

  • user1745356
    user1745356 almost 2 years

    I couldn't find a comprehensive source that would explain the concept in a clear manner. My understanding is that a thread is given some chunk of memory in the eden where it allocates new objects. A competing thread will end up having a somewhat consecutive chunk of eden. What happens if the first thread runs out of free area in its TLAB? Would it request a new chunk of eden?

    • maaartinus
      maaartinus almost 7 years
      Concerning your last question, I guess that a new TLAB gets allocated for the thread. Think of allocating TLABs instead of small memory pieces like a simple optimization making frequent work thread local instead of requiring locks.
  • user1745356
    user1745356 almost 7 years
    Thanks. 1) When a thread allocates a TLAB it still has to acquire a lock so that a competing thread would not allocate a TLAB at the same area that the first thread did. Is that assumption correct? 2) Is allocating an object outside of TLAB is always expensive since a lock must be acquired for allocating each new object that does not fit in a TLAB?
  • Rafael Winterhalter
    Rafael Winterhalter almost 7 years
    Think of thread 1 claiming a TLABfrom offset 0 to 99 and thread 2 claiming one from offsets 100 to 199. The contract says that once claimed, thread 2 cannot allocate in 0-99 and thread 1 cannot allocate in 100-199. No thread can claim this space as its future allocation buffer either. This way, each thread can allocate 100 objects without synchronizing if each object takes one of the hypothetical slots. Allocating outside a TLAB is not very expensive but it is obviously more expensive than within a TLAB as it requires communication with other threads which might run concurrenctly.
  • user1745356
    user1745356 almost 7 years
    Sorry, if my question is not clear, I'll try to put it another way - if two competing threads want to allocate memory for the next 100 objects and the next available memory address is starting at 100 then the first one who acquires a lock will get addresses from 100 to 199 and the second thread will get 200 - 299. Is this correct?
  • Rafael Winterhalter
    Rafael Winterhalter almost 7 years
    Yes (instead of a lock, there will rather be some CAS routine). Without the TLAB, a thread would need to "double-check" for every allocation. With the TLAB, it can allocate up to 100 instance without any additional checks.
  • user1745356
    user1745356 almost 7 years
    I get the picture now. Thanks a lot. I guess I need to read up on CAS routine then.
  • KeyC0de
    KeyC0de over 4 years
    So basically TLAB is a thread_local arena or a thread_local object pool