In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what will happen when Jvm encounters the new keyword". In daily operation, I believe many people have doubts about what will happen when Jvm encounters the new keyword. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt that "what will happen when Jvm encounters the new keyword?" Next, please follow the editor to study!
In the previous article we learned about the memory partition of Jvm, and in this article let's take a closer look at what Jvm does when we create a new object with the new keyword. When the virtual machine executes the new instruction, it is found to be a keyword, so it checks whether the instruction parameter can find the relevant reference in the constant pool, and whether the class represented by the reference has been loaded, parsed, and initialized. If it is not loaded, the class loading process is performed first. When the class is successfully loaded, the virtual machine allocates memory for the newly created object. Allocating memory is nothing more than delimiting an area on the Java heap as storage space for new objects. But in the actual processing is more troublesome. For example, how to find free memory and how to divide fixed-size memory. We know that heap memory in virtual machines, as mentioned in the Java virtual machine specification, can be contiguous or discontiguous. Therefore, when the virtual machine allocates new memory to the object, it should adopt different implementation according to these two different ways. These two methods are called pointer collisions and free lists, respectively. On the front, let's take a look at their specific implementation.
Pointer collisions: continuous memory means that the memory in the Java heap is absolutely regular, with used memory on one side and free memory on the other. There is a pointer in the middle as the demarcation point, and if you want to allocate new memory, just move the pointer to the free memory side. This way of allocating memory is called pointer collisions.
Free list: if the memory in the Java heap is not complete, it is not contiguous. At this time, there are no rules for the use of memory and free memory, and it is impossible to allocate memory by pointer collision. At this time, the virtual machine can only take other ways to identify which memory is used and which memory is free, so the virtual machine has to maintain a list to store which memory is free, and when allocating memory, just divide an area from the list to store the object instance and update the record on the list. This method is called free list.
As the memory allocation methods used by different virtual machines are different, the above mainly illustrates the two ways to allocate memory, which way the virtual machine uses is decided by the heap of different virtual machines.
The above process only allocates the relevant memory space to the object, but the object is still empty, how can the memory data store the information of the object? There are three main areas in which objects are stored in memory in a virtual machine:
Object header: it mainly includes two parts: one is the object's own running data, such as hash code, GC generation age, lock status identification, lock held by thread, biased thread ID and so on. It is officially known as Mark Word. The other part of the object header is the type pointer, the pointer of the object to the class metadata, which is used by the virtual machine to determine which class the object is an instance of. The information of the object header is not fixed, if the object is an array, then there will be information in the object header to record the length of the array.
Instance data: that is, the types and contents of various fields customized in the program, including the data of parent or subclasses, are stored here, which is the real valid information stored by the object.
Align padding: the main thing to do is to use placeholders. Because the virtual machine requires that the starting address of the object must be an integer multiple of 8 bytes, that is, the object size must be an integral multiple of 8, if the instance data of the object is not aligned, it will need to be filled with alignment padding.
The object is now created, but it is stored on the heap. To make it easier for us to manipulate objects in the heap, Java manipulates specific objects on the heap through reference data on the stack. This reference is accessed using a direct pointer. Information about this pointer is stored in the heap, and the reference stores the memory address of the object.
At this point, the study of "what happens when Jvm encounters the new keyword" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.