What do Java objects look like in memory during run-time?(未看)
2014-01-17 23:22
411 查看
We know functions are implemented in memory as a stack of activation records. And we know Java methods are implemented as a stack of frames in JVM Stack and Java objects are allocated in Heap.
How do Java objects look like in heap? Once an object is laid out in memory, it’s just a series of bytes.
Then how do we know where to look to find a particular field? Keep an internal table inside the compiler containing the offsets of each field.
Here is an example of an object layout for class “Base”(B). This class does not have any method, how methods are laid out in memory is in the next section.
If we have another class “Derived”(D) extending this “Base” class. The memory layout would look like the following:
Child object has the same memory layout as parent objects, except that it needs more space to place the newly added fields. The benefit of this layout is that A pointer of type B pointing at a D object still sees the B object at the beginning. Therefore, operations
done on a D object through the B reference guaranteed to be safe, and there is no need to check what B points at dynamically.
Following the same logic, the method can be put at the beginning of the objects.
However, this approach is not efficient. If a class has many methods(e.g. M), then each object must have O(M) pointers set. In addition, each object needs to have space for O(M) pointers. Those make creating objects slower and objects bigger.
The optimization approach is to create a virtual function table (or vtable) which is an array of pointers to the member function implementations for a particular class. Create a single instance of the vtable for each class and make each object store a pointer
to the vtable.
This is the optimized approach.
* The above are my notes for Standford compilers lectures which has very good animation.
How do Java objects look like in heap? Once an object is laid out in memory, it’s just a series of bytes.
Then how do we know where to look to find a particular field? Keep an internal table inside the compiler containing the offsets of each field.
Here is an example of an object layout for class “Base”(B). This class does not have any method, how methods are laid out in memory is in the next section.
If we have another class “Derived”(D) extending this “Base” class. The memory layout would look like the following:
Child object has the same memory layout as parent objects, except that it needs more space to place the newly added fields. The benefit of this layout is that A pointer of type B pointing at a D object still sees the B object at the beginning. Therefore, operations
done on a D object through the B reference guaranteed to be safe, and there is no need to check what B points at dynamically.
Following the same logic, the method can be put at the beginning of the objects.
However, this approach is not efficient. If a class has many methods(e.g. M), then each object must have O(M) pointers set. In addition, each object needs to have space for O(M) pointers. Those make creating objects slower and objects bigger.
The optimization approach is to create a virtual function table (or vtable) which is an array of pointers to the member function implementations for a particular class. Create a single instance of the vtable for each class and make each object store a pointer
to the vtable.
This is the optimized approach.
* The above are my notes for Standford compilers lectures which has very good animation.
相关文章推荐
- What do Java objects look like in memory during run-time?
- What does a Java array look like in memory?(未看)
- How to do run-time (or explicit) linking of C++ plug-in components and objects
- what is time complexity of concatenating two int in java example
- What does Quick Sort look like in Python?
- What means the error-message 'java.lang.OutOfMemoryError: GC overhead limit exceeded' in Java?
- How do I find what queries were executing in a SQL memory dump?-----stack
- What's new in Android M - Run Time Permission
- What does the flowchart look like in top Journals?
- How to run eclipse in clean mode? and what happens if we do so?
- Storing Java objects in MySQL blobs
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- Disaster Recovery: What to do when the SA account password is lost in SQL Server 2005
- A Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run
- how to detect memory in memroy dump objects
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- A java Runtime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Ecl
- HazelCast-Java实现的内存数据网格(In-Memory Data Grid)分析 二
- Memory space manipulating in Java(Section four:Problems Resolving - part two)
- What to Look for in vmstat