In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the method of creating Java objects". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The process of creating a Java object begins when the bytecode new instruction is received by the virtual machine.
1 inspection phase
After the virtual opportunity is sent to the new instruction, it will locate the symbol reference of the class in the constant pool according to the parameters of the new instruction, and check whether the class represented by the symbol reference has been loaded, parsed and initialized. If not, you need to perform the corresponding class loading process first.
If the class load check passes, the virtual machine allocates memory for the new object, and how much is allocated can be determined after the class load is complete.
2 memory allocation
The process of allocating memory to an object is equivalent to dividing a fixed-sized piece of memory from the Java heap. There are two main ways to divide it: pointer collisions and free lists. The two ways have different application scenarios.
2.1 pointer collision
Pointer collision is suitable for absolutely regular Java heap memory, that is, Java heap memory, where one side of the used memory and the other side of the idle memory are demarcated by a pointer. The process of allocating memory is a simple and efficient process in which the pointer moves a fixed size to the free side.
2.2 Free list
Memory in the Java heap is probably not absolutely regular, but used memory and free memory are interlaced with each other. In this case, you can use a free list by allocating memory to an object. The virtual machine records which memory in the heap is free in a list, and when allocating memory for objects, it finds a large enough block of memory recorded in the list to allocate to new objects, and updates the records in the list.
2.3 basis for memory allocation
Which method is used to allocate memory for new objects depends on whether the Java heap memory is regular or not, while whether the Java heap memory is regular or not depends on whether the garbage collector used has the ability to compress and clean up space. When using garbage collectors with the ability to compress space (such as Serial,ParNew, etc.), virtual machines use pointer collisions to allocate memory for objects, while when using CMS, a collector based on cleanup algorithms, in theory, memory can only be allocated to objects in the form of free lists.
2.4 problems and Solutions of memory allocation
In Java applications, the creation of objects is very frequent, and the behavior of allocating memory to new objects by the Java virtual machine is also very frequent, but the operation of the virtual machine allocating memory to objects is not thread-safe when concurrently, because allocating and moving pointers, allocating memory and modifying the list of free space are not atomic operations. It is possible that the memory allocation of multiple objects is based on the same location pointed to by the pointer or that multiple objects are allocated to the same free space. There are two ways to solve this problem:
Synchronize the operation of allocating space to objects to ensure that only one object can be allocated memory at a time
Each thread is pre-allocated independent space in the Java heap, that is, the local thread allocates buffers.
In this way, each thread allocates memory to the object in its own thread buffer, and there is no thread safety problem.
However, when the buffer is used up, the operation of reallocating the buffer still requires synchronous locking.
The virtual machine sets whether to use local threads to allocate buffers through-XX:+/-UseTLAB
3 initialize memory to zero
After the memory allocation is completed, the virtual machine must also set all the space outside the allocated object header to zero (if TLAB is used, it can also be set to zero when allocating TLAB), which ensures that the property fields of the object can be used without initial values, and the program accesses these fields will get zero values corresponding to the field type (byte,short,int,long corresponds to 0 float and double corresponds to 0. 0 Boolean corresponds to false. Char corresponds to\ u0000, and reference type also corresponds to null)
4 set object header information
Set the object header information, such as the instance of which class the object is, the hash code of the object, the GC generation age of the object, etc., depending on whether the bias lock is used or not.
This is the end of the content of "how to create Java objects". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.