Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods for creating Java objects?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about the creation of Java objects about what methods, the editor thinks it is very practical, so share with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.

When the virtual machine encounters a new instruction, it first checks whether the parameters of the instruction can locate a symbolic reference to a class in the method area constant pool, and checks whether the symbolic reference represents a class that has been loaded, parsed, and initialized. If not, you must first perform the corresponding class loading process.

Object creation of Java

After the class load check, the virtual machine allocates memory for the new object. The amount of memory required for an object is fully determined after the class is loaded, and allocating space for an object is equivalent to dividing a fixed-sized piece of memory from the Java heap.

There are two situations at this time:

Regular memory in Java heap

If the memory in the Java heap is regular, the virtual machine will allocate memory using pointer collisions (BumpthePointer). That is, the used memory is stored on one side, and the idle one on the other, with a pointer in the middle as a demarcation point indicator, so to allocate memory is to move the pointer to the idle side by the same distance as the object.

Irregular memory in Java heap

If the memory in the Java heap is irregular and the used and unused memory is interlaced, the virtual machine will allocate memory using a free list (FreeList). That is, the virtual machine maintains a free list, records which blocks of memory are available, finds a large enough space from the free list to allocate to the object instance, and updates the list when allocating memory.

The choice of allocation method is determined by whether the Java heap is neat or not, and whether the Java heap is neat or not is determined by whether the garbage collector has a compression function. For example, collectors such as Serial and ParNew, which are based on replication algorithm and mark-collation algorithm, use pointer collision to allocate memory. CMS, a collector based on the mark-and-clear algorithm, uses a free list.

About garbage collection algorithms and garbage collectors.

Another problem is to ensure thread safety when allocating memory, even if a simple pointer collision changes the position of the pointer, it is not thread safe in the case of concurrency. There are two ways to solve this problem, one is to synchronize the action of allocating memory to ensure the atomicity of the operation, and the other is the local thread allocation buffer TLAB mentioned earlier.

After the memory allocation in the Java heap is complete, the virtual machine initializes the allocated memory space to zero (excluding object headers). This ensures that the instance fields of the object can be used without assigning initial values in the Java code, and the program can access the zero values corresponding to the data types of these fields. If TLAB is adopted, this step can also be advanced to the time of TLAB allocation.

Then set the necessary information on the object, such as which class the object is, how to find the metadata information of the class, the hash code of the object, the GC generation age of the object, and so on, all of which are stored in the object header (ObjectHeader).

At this point, a new object has been generated from a virtual machine point of view, but from a Java program point of view, the object creation has just begun, the init method has not yet been executed, and all fields are still zero. So the init method is executed to initialize the object according to the programmer's wishes.

The above is about the methods of creating Java objects, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report