Java - JVM: 读书笔记 Chapter 05 The Java Virtual Machine ( Part I Basic )
Tutorial Materials: Inside Java Machine
Chapter 5 - The Java Virtual Machine
5.1 The Lifetime of a Java Virtual Machine
A runtime instance of the Java Virtual Machine has a clear mission in life: to run one Java application, When a Java application starts, a runtime instance is born. When the application completes, the instance dies. on the same computer, using the same concrete implementation, you'll get three Java Virtual Machine instances. Each Java application runs inside its own Java Virtual Machine.
5.2 The Architecture of the Java Virtual Machine
a. Internal architecture
b. Runtime data area
Each instance of the Java Virtual Machine has one method area and one heap. These areas are shared by all threads running inside the virtual machine. When the virtual machine loads a class file, it parses information about a type from the binary data contained in the class file. It places this type information into the method area. As the program runs, the virtual machine places all objects the program instantiates onto the heap.
c. Runtime data areas exclusive to each Thread
1) When each thread initialized, it gets its own pc register(program counter) and Java Stack
2) If the thread is executing a Java method (not a native method), the value of the pc register indicates the next instruction to execute.
3) A threadís Java stack stores the state of Java (not native) method invocations for the thread. The state of a Java method invocation includes its local variables, the parameters with which it was invoked, its return value (if any), and intermediate calculations. The state of native method invocations is stored in an implementation-dependent way in native method stacks, as well as possibly in registers or other implementation-dependent memory areas.
4) The Java stack is composed of stack frames (or frames). A stack frame contains the state of one Java method invocation. When a thread invokes a method, the Java Virtual Machine pushes a new frame onto that thread's Java stack. When the method completes, the virtual machine pops and discards the frame for that method.
5) The Java Virtual Machine is not designed to use the registers to hold the intermediate data values but to use the Java Stack instead. - The goal is to eliminate the few or irregular general purpose registers of different PC Platform.
6) No thread can access the pc register or Java stack of another thread.
5.3 Data Types
Data Type of the JVM composes by Primitive Type and Reference Types.
Reference values refer to objects, but are not objects themselves. ( consider it as the value of the address of the Object instance )
returnValue type. This primitive type is used to implement finally clauses of Java programs.
5.4 Word Size
The basic unit of size for data values in the Java Virtual Machine is the word--a fixed size chosen by the designer of each Java Virtual Machine implementation
1. The word size must be large enough to hold a value of type byte, short, int, char, float, returnValue, or reference. Two words must be large enough to hold a value of type long or double
2. An implementation designer must therefore choose a word size that is at least 32 bits, but otherwise can pick whatever word size will yield the most efficient implementation. The word size is often chosen to be the size of a native pointer on the host platform.
5.5 The Class Loader Subsystem
5.5.1 Loading, Linking and Initialization is the responsiblities of the Class Loader Subsystem
1. Loading: finding and importing the binary data for a type
2. Linking: performing verification, preparation, and (optionally) resolution
a. Verification: ensuring the correctness of the imported type
b. Preparation: allocating memory for class variables and initializing the memory to default values
c. Resolution: transforming symbolic references from the type into direct references.
3. Initialization: invoking Java code that initializes class variables to their proper starting values.
5.5.2 The Primordial Class Loader
Primordial Class Loader - Java Virtual Machine implementations must be able to recognize and load classes and interfaces stored in binary files that conform to the Java class file format.
Every Java Virtual Machine implementation has a primordial class loader, which knows how to load trusted classes, including the classes of the Java API. The Java Virtual Machine specification doesnít define how the primordial loader should locate classes. That is another decision the specification leaves to implementation designers.
5.5.3 The Class Loader Objects
Class Loader Objects themselves are part of the Java Application, three methods defined in class ClassLoader are getways into Java Virtual Machine.
protected final Class defineClass(byte data[], int offset, int length);
protected final Class findSystemClass(String name);
protected final void resolveClass(Class c);
defineClass ( ) method accepts byte arrays, class ClassLoader expects binary data conforming to the Java class file format--binary data that represents a new type for the running application. Every Java Virtual Machine implementation must make sure the defineClass() method of class ClassLoader can cause a new type to be imported into the method area. ( the new Type should be the binary data of the Class data, it stores in the Method area )
findSystemClass ( ) : When a class loader object invokes this method, it is requesting that the virtual machine attempt to load the named type via its primordial class loader. If the primordial class loader has already loaded or successfully loads the type, it returns a reference to the Class object representing the type( The reference to the Class data stores in the Mehtod area). If it can't locate the binary data for the type, it throws ClassNotFoundException. Every Java Virtual Machine implementation must make sure the findSystemClass() method can invoke the primordial class loader in this way.
resolveClass ( ) method accpets a reference to a Class instance ( The class instance is the class data stored in the Method area ). This method causes the type represented by the Class instance to be linked and initialized (if it hasnít already been linked and initialized). ( The defineClass() method, described above, only takes care of loading. (See the above section, "Loading, Linking, and Initialization" for definitions of these terms.) When defineClass() returns a Class instance, the binary file for the type has definitely been located and imported into the method area, but not necessarily linked and initialized. ) Java Virtual Machine implementations make sure the resolveClass() method of class ClassLoader can cause the class loader subsystem to perform linking and initialization.
5.5.4 Name Spaces
Each class loader maintains its own name space populated by the types it has loaded. see Chapter 3, the "Security" chapter described that there are different unique Name Spaces contained by each different Class Loader.
Java - JVM: 读书笔记 Chapter 05 The Java Virtual Machine ( Part II Method Area )