Java Object creation Process

Source: Internet
Author: User

Object creation

At the language level, creating an object is usually solved with just one new keyword, but in a virtual machine, the creation of an object takes a complex process.

Constant pool in the method area

When the virtual opportunity goes to a new keyword, first go to the constant pool in the method area to find the symbol reference for this class, and check whether the class represented by this symbol has been loaded, parsed and initialized, if the class loading process is not performed first.

Heap Memory

After the class is loaded, a chunk of memory is then partitioned on the heap in the virtual machine, storing the object of the class (the size, after the class is loaded, determines the amount of space the class needs based on its internal variable type and reference). Because of the different mechanisms of GC, there are two possible mechanisms for allocation.

Idle List method

If the garbage collection mechanism is the 标记清除法 virtual machine must maintain a list of which memory is available, and then allocate a large enough space to partition the class object instance.

Pointer Collision method

If the garbage collection mechanism is 复制算法 or 标记整理法 , then when allocating memory, move the pointer directly to the idle area for a distance equal to the size of the object.

In memory allocation, to solve the problem of multi-threaded synchronization, there are two scenarios: one is the allocation of space from the action of synchronous processing; the other is that the memory allocation is divided by thread, that is, each thread allocates a small chunk of memory in the Java heap and becomes the local thread allocation buffer.

When the memory allocation is complete, the virtual machine allocates the memory space to zero, which guarantees the initialization of the member objects in the object.

The storage structure of objects in memory

The storage structure of objects in memory can be divided into object headers, instance data, and aligned fills.

Object Header

The object header stores two pieces of information, some of which store the object's own runtime data, such as HASHCODE,GC generational age, lock status flag, and so on. The other part of the object header stores the type pointer, which points to the object class metadata, which determines which class is an instance of the object.

If the object is 数组 , the length of the array should also be recorded in the object header. Because different objects can determine the size of an object through the metadata of the class, the array's length is not known through the array's metadata.

Instance data

The instance data section stores valid information about an object, mainly member variables. Whether it is inherited from a parent class or defined in a subclass, it is recorded. The order of storage is primarily affected by the virtual machine allocation policy.

The distribution policy in hotspot is: Long/double,int,short/char,byte/boolean,opps (ordinary Object pointers). Fields of the same width are always put together, and when this condition is met, the variables defined in the parent class are always placed before the subclasses.

Snap To fill

Primarily because the Hotspot automatic memory management system requires that the object start address must be an integer multiple of 8 bytes.

Java Object creation Process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.