JVM Notes - 1

来源:互联网 发布:jquery 渲染div数据 编辑:程序博客网 时间:2024/05/02 21:34

Being the Java programmer for a long time, I feel that it is more and more important
to know what is running under the java. That is why I start to read the JVM which is
the tough part of Java. I wish after the learning I can benifit on:
1. Know how do java do the method invocation.
2. Know how do java do the overload, override.
3. Know how do java throw the exception.
4. Know how do java know the scope of the variable.
5. Know how do java handle the String as the constant.

I will record everything I got from reading the book. Inside the Java virtual machine.

JVM Note 1.


The life time of JVM :
The life of a JVM depends on the application runs inside it. When the application completes the JVM will die.

Data type :
All the primitive type of java programming(byte, short, int, long, float, double, boolean), except the boolean
are all the primitive type of the JVM. For the boolean type, it will be translated into the int or byte in
the JVM represnetation.

There is a data type that only worked inside JVM but java programming, the returnValue type which indicates the
finally clauses of the java program.

The reference type, the value of which comes in three flavor: the class type, the interface type, the array type.

Word size :
The JVM run time data area are desgined base on the word which is 32 bit long.(as int) and when the java program
runs the JVM will measure the necessary area base on the word.

Class loader :
JVM contains two kinds of the class loader: primodial class loader and the class loader object. the first one is
part of the JVM, another one is the java object which is part of the running application. The implementation
of JVM must be able to recognize class file. An implementation is free to recognize other binary format. Eg, the
Microsoft VM can load the CAB file as well. The class loader maintains its own name space populated by the types it
has loaded, a single Java application can load multiple types with the same fully qualified name.(?????Why,)

The method area :
Inside the JVM, information about loaded types is stored in a logical area of memory called the method area. memory
for class variable declared in the class is also taken from the method area. All the thread share the same method
area which implieds that the static variable must be designed as thread safe. Also the size of the method area is
not fixed.

there is a lot of infomation included in the method area like: The fully qualified name of the type, the superclass,
whether it is a class or interface, type modifier, the method table and so forth. the variable which modified by final
is not only included in the class that defined them, the class which refer to them will get a local copy.

A JVM can alway determine the amount of memory requried to represent an object by looking into the class data stored
in the method area
[Question]
But how. it is easy to under stand that a class only contains some variable as primitive type, but how about the
String, it is unlimited. how java know how many memory should be allocated to the string object.?
[/Question]

[Answer]
It is easy for JVM to know how many memory should be allocated to the string object, since it is controlled by a Array which contains a collection of the characters, then read the length of the array is ok.
[/Answer]

The heap:
There is only one heap inside one JVM, all the object is in it. Two different can not trample each other's heap data
but the thread could. This is why the programmer should be care about the multi-thread accessing on the same object.
Usually, a Java Virtual Machine implementaion uses a garbage collector to manage the heap. Note, the garbage collector
is not a required part of the JVM. The object usually inculdes some kind of pointer into the method area. because:
1. It may something perform the cast type
2. It may incude the instanceof checking
3. It may invoke on some class variale.


The program counter:

It hold by the thread, measures in word size and contains the current instruction being executed.

The stack :
It lives in the heap. in my understanding, there is no data survives in the heap but stack. The stack is created when
thread starts. The Java stack stores a thread's state in its frame. As thread is alreay in some method. then all the
data include in the stack is private to the current thread (It is the local variable which defined in the method).

The stack frame :
In my understanding, It takes charge of the temporary data storage, manipulate the data, execute the instruction. In
other words, It is concrete execution part of the application.