Java as the OOP language, the abstraction is self-evident. If you need to learn more about the implementation mechanism of the Java language, you have to have a clear understanding of the underlying concepts in the Java language. Today is my first day of blogging on Cnblog and I hope
Today's blog can be my first step to become the future of "Daniel".
Object-oriented language, the concept of objects is actually very abstract, for beginners and even the development experience of the comrades are not easy to understand. Recently saw this cow people write articles, feel pretty beneficial, and everyone share it. Translation has
Some poor, "Daniel" Please ignore me directly to read the original, Hee ~.
SOURCE Link: http://www.programcreek.com/2011/11/what-do-java-objects-look-like-in-memory/
We know functions is implemented in memory as a stack of activation recordS. And we know Java methods is implemented as a stack of frames INJVM stack and Java objects is allocated in Heap.
The function is implemented in memory through a pile of "activity records" (activation record, also known as a stack frame). We also know that the Java method is implemented in the virtual machine stack through a stack frame, and the Java object is in the heap
Allocated memory space (heap).
COMMENT1:
1) Funtion and procedure in Java are collectively referred to as method. Generally speaking, function and procedure are different (of course, many of the programming books are mixed, I also have no language ==!)
Funtion-method that has no return value (void or constructor returns a value type that is not written); Procedure--Method with return value
2) The stack frame, also known as the activity record (activation record). method is pressed into the stack frame when called (called), the stack frame is saved with local variables, return value types and other information. The specific content of this article does not delve into.
How does Java objects look like in heap? Once An object was laid out in memory, it's just a series of bytes.
What does the Java object look like in the heap? Once an object is put into memory, it is simply a series of bytes.
Then how does we know where to look to find a particular field? Keep An internal table inside the compiler containing the offsetsof each field.
So how do we know where to look and look for such a special field? A table with each field offset is saved inside the compiler.
COMMENT2:
1) field some translated "domain", "field", in fact, is a meaning. All refer to the member variables of the class (including static member variables, except that static is stored in the method area, belongs to the class, all class objects are shared)
2) object member variable auto-addressing: a virtual table (Vsan or called Vtable) is stored inside the compiler, including the address offset of each object member variable relative to the first address of the object space (the first member variable), which
sample is can be to easily to access the member variables of the object.
Here's an example of a object layout for class "Base" (B). This class does not has any method, how methods was laid out in memory was in the nextsection.
Give an example of a base class instance object. There is no method for this class, and how its methods are arranged in memory is explained in the next section.
If we have the another class "Derived" (D) extending this "Base" class. The memory layout would look like the following:
If we have another derived class that inherits from the base class. The layout of the memory is as follows:
Child object have the same memory layout as parent objects, except that it needs more space to place the newly added fi ELDs. The benefit of this layout is, a pointer of type B pointing at a D object still sees the B object at the Beginning. Therefore, operations done in a D object through the B reference guaranteedto be safe, and there are no need to C Heck what B points at dynamically.
The subclass object has the same type of memory layout as the parent object except that more space is needed to place the new field. The advantage of this layout is that when there is a base class reference to the derived class object, the reference can still access the base class object where the memory area begins. Therefore, all operations on the derived class object through the base class reference are secure, and there is no need to dynamically check what the base class points to.
COMMENT3:
1) pointer and reference
In fact, sometimes I look at the Java English Literature when it is also silly to tell the end of that. If it is C + + that pointer and reference (the reference and the pointer surface are not the same, the reference is implemented by pointers in the bottom), obviously
is a two concepts. In the Java Reference and C + + reference surface is fundamentally different concept, Java reference more like C + + pointer variable, but is castrated version of C + + pointer variable (Java reference and C + + type * Const PTR, etc.
The value of PTR cannot be changed, but the contents of PTR point can be changed).
2) The grammatical phenomenon of upward modelling (also called "Upstream styling")
The benefit of this layout is, a pointer of type B pointing at a D object still sees the B object at the beginning.
A reference to a class B (parent Class) can still access a B object after it points to a Class D object. Still sees the B object at the beginning shows that the memory is the same for the beginning of object B and Object D, and Object D is
the memory of the head can be recognized to be is Object B.
Through the above analysis, we look at the following written question:
1 Import Staticjava.lang.System.out;2 3 classSupclass {4 intx = 10;5 voidShow () {6System.out.println ("Supclass"); 7 } 8 }9 Ten classSubclassextendssupclass{ One intx = 20; A voidShow () { -System.out.println ("Subclass"); - } the } - - Public classTest { - Public Static voidMain (string[] args) { +Supclass sup =Newsubclass (); - out.println (sup.x); + } A}
supclass sup = new subclass (); The parent class (Supclass) refers to the child class (subclass) object, because the parent class reference can only access the components inherited from the parent class in the subclass, so x = 20 is not accessible by the SUP.
The answer is 10, not 20.
3) Why does a parent reference can point to a subclass object and the compiler does not make an error?
Therefore, operations done in a D object through the B reference guaranteed to be safe, and there yes no need to check what B points at dynamically.
Therefore, all operations that reference the heap D object through the base class are secure and do not need to dynamically check what the base class is referencing.
This sentence can really explain why this operation compiler does not error. In fact, this is the case, because the subclass inherits from the parent class the same part of the structure as the parent class (through the Super keyword, when the subclass object is more
The contents of the parent class). The parent class reference can access the part of the subclass that inherits from the parent class through the object of the subclass, so the compiler does not error. And this form of access is safe, neither changing the contents of the parent class nor visiting
Ask the sub-class to come out of that part.
4) Deep heap memory
What is heap memory? Heap memory is a virtual memory on a physical machine. What do you call virtual memory? Virtual memory is actually a hard disk, just a portion of the storage capacity of the hard disk as the memory of the JVM, obviously
This memory is fake, and the memory of the physical machine is not the same. of course, since the JVM (Java Virtual machine) is a virtual computer, it is not just a heap, but all the memory of the JVM (including the Java stack, etc.) is virtual in the physical machine.
Memory, which is the storage space in the actual hard disk.
JVM Heap Memory actually is the linked list (Linked list) This data structure is maintained so that contiguous memory in heap memory is often referred to as logical continuity, and the physical heap is not contiguous in memory.
Following the same logic, the method can be put on the beginning of the objects.
Depending on the same logic, the method can be placed at the beginning of the object.
However, Thisapproach is not efficient. If A class has many methods (e.g. m) and then Eachobject must having O (m) pointers set. In addition, each object needs to Havespace for O (M) pointers. Those make creating objects slower and objects bigger.
However, this method is not efficient. If a class has many methods, then each object must have a pointer set (that is, a lot of pointer variables). In addition, each object needs to have space to store these pointer variables. These can cause the creation of objects to become slower while object space becomes larger.
COMMENT4:
1) Theoretically we write the method at the beginning of the object memory when we create the class object, but there may be many methods for the object compared to a finite object member variable, and if you create an object every time you need a lot of pointers to the method, that
There is no doubt that the heap memory will become very large, and it will become very slow when creating objects (object creation is created from the beginning of the first address).
2) The object does not include a method, but instead includes a pointer variable that points to the method. method is stored in the method area. In this model (the JVM does not use this model), when an object calls a non-static method, the pointer variable in the heap is
The pointer variable in the heap accesses the corresponding method in the method area and puts it in the Java stack, corresponding to a stack frame.
The Optimizationapproach is to create a virtual function table (or vtable) which is an array ofpointers to the member function implementations for a particular class. Createa Single instance of the vtable for each class and make each object store apointer to the vtable.
The optimization method is to create a virtual function table of a particular class, which is an array of pointers to the implementation of the member function. Create a single instance of the virtual table for each class, and have each object store a pointer to the virtual function table.
COMMENT5:
1) The actual use of the JVM is the second model above (when the method area is stored in the same way, it also holds a list of virtual function tables, virtual function table placement method code).
Code for Base.sayhi refers to the method code information in the method area; Sayhi refers to the method pointer that is created in the heap that points to the method-area method, and vtable* refers to a collection of method pointer arrays that create a dynamic array of pointers, which
The dynamic pointer points to a specific stack frame in the stack and is shared by all class objects (stored in the method area).
2) The object can access the contents of the constant pool in the heap, what does it mean? Does it indicate that a pointer to a constant pool exists in the heap. Also, accessing the content of a constant pool is limited to the contents of a class. So it can be seen that the objects in the heap actually exist
there is a. Class Handle pointer. Of course, this is my guess, there is no basis.
3) I think, seeing this you and I know what is the real object of the meaning. Object =.class information handle pointer (can point to the method area constant pool) + virtual function table pointer variable (method that points to the method area) + Object member variable (data)
4) The methods of the class are stored in the method area and are shared by the objects of all classes. When a method pointer within a virtual function table of a specific object is created, the method in the method area is placed into the Java stack, which is bound to the stack frame. This allows each class to
like the problem of having to invoke many methods through the virtual function table , the object is created faster than the first model, and the object requires less heap memory space than the first type.
Through the above discussion, the following interview questions can be resolved:
1 Public classTestClass {2 Public Static voidMain (string[] args) {3Supclass sup =Newsubclass ();4 Sup.show1 ();5 Sup.show2 ();6 }7 }8 9 classSupclass {Ten intx = 10; One Public voidShow1 () { ASystem.out.println ("Supclass"); - } - Public voidShow2 () { the System.out.println (x); - } - } - + classSubclassextendssupclass{ - intx = 20; + Public voidShow1 () { ASystem.out.println ("Subclass"); at } - Public voidShow2 () { - System.out.println (x); - } -}
The answer to the question is:
Subclass
20
Analysis: The subject of the test is completely the method of overwrite (override override) knowledge points.
1) in the same class, if the method signature of two methods is the same, the compiler will make an error. From the computer thinking, machine processing must be clear things, can not be indeterminate quantity, if there is uncertainty, the machine will error, because
For you to make it dizzy, it does not know to choose that.
2) in the parent-child class relationship two classes, the subclass method signature is the same as the parent class method signature, which belongs to the method override. So why does the parent class reference access subclasses override the parent class's methods when the child class overrides the version and cannot be the parent class version
this , and there is no problem? In fact, in the method area, the sub-class does not contain the methods of the parent class (including the constructor method), but rather has the methods written in the respective code. When you use a parent class reference to point to a child class object, if the child class overrides the party of the parent class
method, then it will find this method in the subclass method area, locate it and put it on the stack, if not found, because of the extends keyword, the super pointer to the parent class method area, in the parent class method area to find the appropriate method.
3) What is a parent class that cannot inherit from a quilt class?
In the Java inheritance mechanism, the methods of the superclass (including the constructor method) cannot be inherited by the derived class, but are implemented by the invocation mechanism. The specific content will be discussed in a subsequent article.
1 Public classTestClass {2 Public Static voidMain (string[] args) {3Supclass sup =Newsubclass ();4 Sup.show1 ();5 Sup.show2 ();6 }7 }8 9 classSupclass {Ten intx = 10; One Public voidShow1 () { ASystem.out.println ("Supclass"); - } - Public voidShow2 () { the System.out.println (x); - } - } - + classSubclassextendssupclass{ - intx = 20; + Public voidShow1 () { ASystem.out.println ("Subclass"); at } - Public voidShow2 (inta) { - System.out.println (x); - } -}
The answer to the question is:
Subclass
10
Analysis: The topic is the variable, Supclass and subclass have 2 methods, but in the subclass of the parent class Show1 () is overwritten, and the parent class Show2 () method is not overwritten. In the method area, the parent class method is the parent class method, and the child class
method is a subclass method, which is the code that is actually written in the method, and there is no method of the parent class in the subclass method area space . However, when a concrete object is created, the virtual function table of the subclass has a pointer to the parent class method. When a subclass overrides a parent class
method, the table is actually updated, which means that the method pointer of the parent class in the table overrides the method pointer of the quilt class.
This is the optimized approach.
The above is the optimized method.
References:
1. Stanford Compilers Lectures
2. JVM
Learning sentiment:
1) If the domestic blog does not have the answer you need, or is not satisfied, it is best to go to the community abroad to take a look, of course, Java official documentation is the foundation of everything.
2) Some professional nouns do need to accumulate, such as activation record I did not translate accurately at first, then through the browser query to translate accurately.
3) Don't be afraid to say wrong, like maybe I have a lot of mistakes in this article. Communicate with others and promote yourself in communication.
4) Some concepts may not be expressed clearly and need to further strengthen their language expression ability. After all, anything, you understand and let others sensible two different things, both abilities.
Java Object Model (i)