Memory structure of Java objects in the heap __java

Source: Internet
Author: User
Translation staff: Anchor
Translation Date: November 8, 2013
Original link: What do Java objects look like in memory during Run-time?

We know that every time a function is invoked, it has its own active record (activation) in memory, called stack space (stack). The Java method assigns a stack frame (a fragment of the Java stack space) to the JVM stack at the time of invocation, which can be called a method stack. In principle, all objects are allocated in heap space (Heap).

How is the Java object allocated in memory?? Once an object allocates space in the heap, it is essentially a series of bytes. So how do you find a specific property field in an object? The compiler uses an internal table to hold the offset of each domain.

The following figure is an object-memory map of the base class, and the base (base class) does not define any methods, as in the memory distribution of the methods, see what follows.


Figure 1 If there is another derived class "Derived" inherits the base class "base". Then the memory distribution is shown in the following figure:

Figure 2 The Child class object has the same memory distribution as the parent object, and of course, the subclass object needs more space to hold the new property field.
The advantage of this allocation is that the base type pointer, if it points to the object of the subclass derived, still "sees" the base object at the beginning.
Therefore, the operation of the subclass object (Derived) with the parent class reference (Base) is guaranteed to be safe, so there is no need to dynamically check the actual type of the Base reference at run time.
In a kind of way, the method can also be placed at the beginning of the object space, as shown in the following figure.

Figure 3
However, this implementation is inefficient. If a class has many methods (for example, 20), then each object will hold 20 pointers, and each object needs 20 pointers of memory space, which causes the creation object to become slower and occupy more space.
The optimization method is to create a virtual function table (vtable, virtual table), a virtual table is an array of pointers to member functions of a particular class. As shown in the following illustration:

Figure 4
* These are my notes on the Stanford University Compiler lecture, which is very lively and interesting.
Reference Documents:
1. Stanford Compilers Lectures
2. JVM
related articles:

What does a Java array look like in memory? Top 5 Questions about C + + pointers brief Java memory leaks
An example of C + + dot vs. Arrow usage

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.