In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to decrypt Java objects, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Objects are created all the time when the Java program is running. There are many ways to create objects. The most common one is new, followed by clone and deserialization. Let's decrypt the creation of an object, the memory layout, and how to locate an object.
Object creation
When we create an object, we first check whether the class that created the object can locate the symbol reference in the constant pool, and check whether the class represented by the symbol reference has been loaded, parsed, and initialized. If not, you must perform the corresponding class loading process (this will be explained in a separate article later).
The process of creating an object consists of the following steps:
The virtual machine allocates memory to the object. The amount of memory required by the object can be fully determined after the class is loaded. Set the necessary information for the object. For example, when the third step is executed, such as the instance of which class the object is, the hash code of the object, and the GC generation age of the object, the virtual machine actually thinks that the object has been created successfully, but it is not completed from the point of view of the Java program. The following will execute the method to initialize the object and allocate the object memory according to the written code.
Allocating memory to an object is essentially allocating a fixed size of memory from the Java heap to the Java object. There are two main types of object memory allocation:
Pointer collision free list
Which method is used depends on whether the heap memory is neat or not, and whether the heap memory is neat in nature depends on whether the garbage collector has the function of compression and collation.
The pointer collision allocation method is used to allocate objects in a neat memory heap, with all the used ones stored on one side and the unused ones on the other side, with a pointer in the middle as an indicator of the demarcation point. When allocating memory for an object, you only need to move the pointer to the unused side at a distance equal to that of the object.
The free list is suitable for allocating memory to objects in irregular memory. In order to know which memory areas are available, the virtual machine must maintain a list. When memory allocation is made, select a large enough space in the list to allocate to the object.
The allocation of objects in virtual machines is not thread-safe. To solve this problem, there are two main solutions:
CAS + failed retry TLAB: each thread pre-allocates a small piece of memory in the Java heap, called TLAB (Local Thread allocation buffer), which thread needs to allocate memory on the TLAB of the thread. Synchronous locking is required only when the TLAB is used up and a new TLAB is allocated. Object memory layout
There are three main layouts of objects in memory:
Object header instance data alignment padding object header
Object headers are mainly used to store two pieces of information:
Pointer to the data type running by the storage object itself
The run-time data of the object itself mainly includes: hash code, GC generation age, lock status flag, lock held by thread, bias thread ID, bias timestamp and so on. The length of this part of the data is 32-bit and 64-bit in 32-bit and 64-bit virtual machines (without compression pointer on), respectively.
Stored content flag bit state object hash code, object generation age 01 unlocked pointer to lock record lightweight lock pointer to heavyweight lock 10 inflated (heavyweight lock) null, no recording information 11GC flag biased towards thread ID, biased timestamp, object generational age 01 can be biased
The type pointer can be used to determine which class the object is an instance of, but the implementation of the virtual machine does not have to keep the type pointer on the object.
Instance data
Instance data is the real valid information stored by the object, that is, the content of various types of fields in the code, whether defined from the parent class or subclass, the order in which the content is stored is affected by the virtual machine allocation policy parameters and the order in which the fields are defined in the Java source code. But fields of the same width are assigned together, and under this premise, the narrower variables of the subclass are inserted into the gaps of the parent variable.
Align fill
Alignment padding is not inevitable, because the memory management of the virtual machine requires that the address of the object must be an integral multiple of 8 bytes, that is, the object size must be an integral multiple of 8 bytes. Therefore, when the object instance is not an integer multiple of 8 bytes, it needs to be filled by alignment.
Object access
After the object is created, we need to use it. We can manipulate specific objects on the heap through reference on the Java stack, but we need to solve how to find specific objects through reference. At present, there are two main ways:
Handle direct pointer
The following figure shows the use of handles to access objects
The following figure shows the use of direct pointers to access objects
Through the comparison of the above two kinds of graphs, we can see that the advantage of the handle is that the content stored in the reference in the stack is a stable handle address, which will not change due to the movement of the object, but the access will be inferior to the direct pointer because of the extra time cost of pointer positioning.
The biggest advantage of direct pointer access is that it is fast and saves the time cost of pointer positioning.
On how to decrypt the Java object to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.