Java heap memory is shared by the threads! Interviewer: Are you sure?

Author l Hollis

This article is reprinted with permission from Hollis (ID: hollischuang)

Java as an object-oriented, cross-platform language, its objects and memory has been more difficult knowledge, so that even a beginner Java, certainly more or less have some understanding of the JVM. It can be said with regard to knowledge of the JVM, is a basic knowledge of each Java developers must learn, is when the interview and tests of the knowledge points.

In the memory structure of the JVM, the more common of the two regions is the heap memory and stack memory (if not especially, are mentioned in this article refers to the virtual stack machine stack), about the difference between heap and stack, many developers also familiar, there are many books, or online articles about this are described:

1, the heap is the thread shared memory areas, thread stack is exclusive memory area.

2, the main storage heap object instance, the main storage reference stack all the basic data types, object.

However, the authors can responsibly tell you that, more than two conclusions are not entirely correct.

Firstly, take everyone to know why I would say, "is the thread shared memory heap area, stack is a thread exclusive area of ​​memory." This sentence is not entirely correct! ?

Before you start into the topic, allow me to ask a question and seemingly nothing to do with the question: how Java object memory allocation process is to ensure thread-safe?

Memory allocation process Java objects is how to ensure thread-safe?

We know that, Java is an object-oriented language, we use the object in Java needs is created, in Java, there are many ways to create an object, but in any case, the object creation process, we need memory allocation.

Memory allocation process object, the main memory area is a reference point to the object, and then perform an initialization operation.

However, because the heap is globally shared, and therefore at the same time, there may be multiple threads in the application heap space, then in concurrent scenario, if two threads have the object reference point to the same memory area, how to do.

To solve this problem concurrency, memory allocation process objects must be synchronized control. But we all know that no matter what kind of synchronization schemes (in fact, a virtual machine may be using CAS) use will affect the efficiency of allocation of memory.

Java object is assigned a high-frequency operation in Java, all people think of another way to improve efficiency. Here we focus on that one aspect of virtual machine HotSpot:

Each thread a small piece of pre-allocated heap memory in Java, and then give the object to allocate memory when allocated directly in their own piece of "private" memory, when this part of the region is used up, a new redistribution of "private." RAM.

This scheme 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.

What is TLAB

TLAB is a virtual machine heap memory eden carved out a piece of dedicated space, thread-exclusive. In the case of TLAB function of the virtual machine starts, the thread is initialized, the virtual machine assigned to each thread a TLAB space, only to use the current thread, so that each thread has a separate room, if you need to allocate memory in assign your own space, so there is no competition situation, can greatly improve the allocative efficiency.

Noted that the above description "exclusive threads", "only to use the current thread", "each thread has a separate" describe it?

So, because of TLAB technology, heap memory is not completely threads share its eden region or a part of space is allocated to the thread exclusive.

It is worth noting that we say TLAB thread is 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 other words, although each thread at initialization time will go to apply for a heap memory TLAB, this does not mean that other threads TLAB memory area is completely inaccessible, read the other thread is still possible, but not in this area to allocate memory only.

And, after TLAB assignment does not affect the movement and recovery of the object, that is to say, 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 Wait.

Another point to note is that we say TLAB area is allocated in eden, eden because the region itself is not too big, but TLAB memory space is very small, accounting for only 1% of the entire default Eden space of the case. Therefore, there must be some large objects can not be directly allocated TLAB.

TLAB encounter large objects that can not be allocated, or objects may be allocated in eden's old district, or so on, but this assignment on the need for synchronization control, which is why we often say: allocate up small objects than large objects more efficient.

TLAB problems caused by

Although to some extent, TLAB greatly enhance the distribution speed of the object, but not TLAB not have any problems.

We have said before, because TLAB memory area is not very large, so there may not be enough will often appear. In one case in the "real Java virtual machines":

TLAB space such as a thread has 100KB, which has been used 80KB, when needed redistribution of 30KB object can not be directly allocated TLAB, in which case, there are two treatment options:

1, if a subject in need of space larger than the remaining space TLAB, directly in the object memory allocation heap memory.

2, if a subject in need of space larger than the remaining space TLAB, the current waste TLAB, reapply TLAB memory space allocation again.

Both options have their pros and cons above, if Option 1, then there may be an extreme case, TLAB is only 1KB, it will lead to follow-up needs to allocate the most objects need to be directly allocated heap memory.

If Option 2, there may exist frequently abandoned TLAB, the frequent application TLAB situation, and we know that while allocating memory on TLAB thread is exclusive, but they had really TLAB memory conflict may exist from the process carved out of the heap Therefore, TLAB allocation process actually requires concurrency control. And frequent TLAB allocated lost sense of TLAB.

In order to solve the problems of these two programs, the virtual machine defines a refill_waste value, this value can be translated as "the biggest waste of space."

When the memory is greater than refill_waste allocation request, selects allocated heap memory. If the value is less than refill_waste will discard the current TLAB, recreate the object-TLAB memory allocation.

In the previous example, the total space Tlab 100KB, using 80KB, 20KB surplus, if the set value refill_waste 25KB, then if a new object is greater than 25KB memory, the heap memory allocation directly before is less than 25KB, will be discarded that TLAB, a TLAB reallocate space to allocate memory for the new object.

Parameters used TLAB

TLAB selected function is turned on or off, by setting -XX: +/- UseTLAB parameter to specify whether TLAB dispensing opening.

TLAB default is 1% eden region, can the option -XX: TLABWasteTargetPercent set Eden space TLAB space occupied by the percentage size.

By default, TLAB space will continue to adjust at run time, allowing the system to achieve the best operating condition. To disable automatic adjustment TLAB size, may be used -XX: -ResizeTLAB to disable, using -XX: TLABSize TLAB to manually specify the size.

The refill_waste TLAB also be adjusted, the default value is 64, i.e., approximately 1/64 indication as refill_waste space, using the parameters: -XX: TLABRefillWasteFraction adjusted.

If you want to observe the use of TLAB, you can use the parameters -XX + PringTLAB track.

to sum up

In order to ensure the safety of the thread memory allocation process objects in, HotSpot virtual machine provides a technique known TLAB (Thread Local Allocation Buffer) in.

When the thread is initialized, the virtual machine assigned to each thread a TLAB space, only to use the current thread, when the need to allocate memory allocated in their own space, so there is no competition situation, can greatly improve the allocative efficiency.

Therefore, "the heap is the thread shared memory area" This sentence is not entirely correct, because TLAB is part of heap memory, he really is a thread to share in the reading, but in the memory allocation, thread-exclusive.

TLAB space is not large, so large objects or may need to be directly allocated heap memory. So, memory allocation step object is to first try to allocate TLAB, after the lack of space, and then determine whether it should go directly to the old era, and then determine whether to re-allocate or assign eden in the old era.

Say a few words

After reading this article I believe part of, one might think of a little too "quibble", "carping" was. Some quick temper may no shortage of people turn to look directly at the beginning of the end of the text were ready to hate.

Whether you think do not agree with the authors said: "The heap is the thread shared memory area of ​​this statement is not entirely correct." In fact, this is not important, it is important when it comes to heap memory, threads share mentioned, referring to the object memory allocation, you may think there is a TLAB is special on it.

Sometimes, the most frightening is not that they do not know, but I do not know they do not know.

There is, TLAB just an optimization program HotSpot virtual machine, Java Virtual Machine Specification nor any provisions on the TLAB. So, do not represent all of the virtual machine has this feature.

This article is based on an overview of the HotSpot virtual machine, the author is not intentional, "sweeping," but because HotSpot virtual machine is the most popular virtual machine, and most of default, we are also discussed based on the HotSpot.

Author: Hollis, has a unique quest for Coding people, the current Alibaba technical experts, personal technology blogger, technical articles, the amount of reading the whole network of tens of millions, "three classes programmer" joint author.

Recommended Reading 

do not have a hair out! With Flutter + Dart quickly build a beautiful mobile App

Look what I found a good thing? Java Optional, definitely worth learning | Force program

Tencent combined ACNet mention fine-grained classification, the effect is up to the latest SOTA | CVPR 2020

My favorite cloud IDE recommendation!

advanced features Solidity written contract of intelligence

return E staff to return to work readme: back to work, Wuhan, Hefei fly first, then go back and pick chartered by the company

You look at every point, I seriously as a favorite

发布了1830 篇原创文章 · 获赞 4万+ · 访问量 1654万+

Guess you like

Origin blog.csdn.net/csdnnews/article/details/104890434