In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to ensure thread safety in the memory allocation of Java objects". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how the memory allocation of Java objects ensures thread safety.
JVM memory structure, is a very important knowledge, I believe that every programmer who has prepared for the interview can clearly introduce the stack, stack, method area and so on.
The image above is a picture of the JVM runtime memory region structure described by the author according to the Java Virtual Machine Specification (Java SE 8).
Many people know that Java objects allocate space in heap memory (except for JIT optimization), and that the memory allocation process is thread-safe, so how on earth does the virtual machine ensure thread safety? This article will give a brief introduction.
Memory allocation of 1.Java object
We know that Java is an object-oriented language, and the objects we use in Java need to be created. In Java, there are many ways to create an object, such as using new, using reflection, using Clone methods, etc., but in any case, the object needs to be allocated memory during the creation process.
Take the most common new keyword as an example. When the code starts to run after we use new to create an object, and when the virtual machine executes this new instruction, it will first check whether the class corresponding to the object to be new has been loaded, and if not, load the class first.
After the class load check passes, you need to allocate memory to the object, which is mainly used to store the object's instance variables.
When allocating memory, you need to determine the amount of space that needs to be allocated based on information such as the instance variables in the object, and then divide such an area from the Java heap (assuming there is no JIT optimization).
Depending on the type of garbage collector used by JVM, the memory allocation in the heap is different because of its collection algorithm. Such as marking-it is clear that there will be a large number of discontiguous memory fragments in the memory reclaimed by the algorithm, so when allocating new objects, you need to use the "free list" to determine a free area. This part is not the focus of this article, readers can learn it by themselves.)
Either way, you eventually need to identify a memory area that will be used to allocate memory to the new object. We know that in the process of memory allocation of an object, the main thing is that the reference to the object points to this memory area, and then initializes the operation.
Then the question arises:
In a concurrent scenario, how is the thread safety of the memory allocation process? What if two threads point the object reference to the same area of memory one after another.
2.TLAB
There are generally two solutions:
1. Synchronize the actions of allocating memory space, and adopt CAS mechanism to ensure the thread safety of the update operation by cooperating with failed retry.
2. When each thread allocates a small piece of memory in advance in the Java heap, and then allocates memory to the object, it allocates directly in its own "private" memory. When this part of the area is used up, it allocates new "private" memory.
Scheme 1 needs synchronization control at each allocation, which is relatively inefficient.
Scenario 2 is used in the HotSpot virtual machine, which is called TLAB allocation, or Thread Local Allocation Buffer. This part of the Buffer is split from the heap, but is exclusive to local threads.
It is worth noting that when we say TLAB, the thread is exclusive, but only in the "allocation" action is thread exclusive, as for reading, garbage collection and other actions are shared by the thread. And there is no difference in use.
In addition, TLAB only works on the new generation of Eden Space, and objects are first placed in this area when they are created, but the new generation of large objects that cannot allocate memory will go straight into the old age. Therefore, when writing Java programs, it is usually more efficient to allocate multiple small objects than large objects.
So, although objects may initially be allocated memory through TLAB and stored in the Eden area, they will still be garbage collected or moved to Survivor Space, Old Gen, and so on.
I don't know if you have ever thought that after we use TLAB, it is exclusive to threads when allocating memory to objects on TLAB, so there is no conflict. However, there may also be memory security problems in the process of dividing TLAB memory from the heap.
Therefore, in the allocation process of TLAB, synchronization control is still needed. But this overhead is much lower than the synchronization control each time memory is allocated for a single object.
Whether the virtual machine uses TLAB or not is optional and can be specified by setting the-XX:+/-UseTLAB parameter.
At this point, I believe that everyone on the "Java object memory allocation is how to ensure thread safety" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.