The Structure of the Virtual Machine

The Java VM is in many ways similar to a typical machine or computer. It has a set of opcodes that perform operations such as loading and storing data. It has opcodes that can perform arithmetic computations and manipulate data. It distinguishes between floating-point and integer types. It has a program counter (PC) register that keeps track of the current instruction being executed. It also has registers for storing the operand and the result of an instruction.

Many computers use special-purpose registers for some of their instructions. Instead of using special-purpose registers, a stack machine uses a stack that serves as a list of registers. This approach significantly simplifies the design of the machine. The machine instructions simply pop their operands from the stack and push their result back onto the stack. Because a stack can be as large as necessary, a computation can use as many registers as it needs without having to worry about the number of registers available on the machine. Many of the Texas Instruments™ calculators are stack machines. The Java VM is a stack machine. It is actually a virtual stack machine because it is not physical hardware but a machine that is implemented in software.

Threads are structures used by the JVM to keep track of an execution state. Every thread has a program counter that keeps track of the current instruction (or bytecode) being executed. This is, in a way similar, to the PC register of a typical machine. A frame is a structure that represents the state of a method that has not completed or returned. Every thread has a stack of frames. The top frame corresponds to the current method being executed. This stack is typically referred to as a call stack. Every Java frame has an array that stores the values of all local variables. The local variables array is allocated when a frame is created. In Java, every frame also has its own operand stack, which should not be confused with the frame stack (or call stack) of a thread. The operand stack is the stack that acts as a list of registers for the VM instructions and is what makes the VM a stack machine.

The VM allocates a chunk of the system memory when it is launched. This chunk of memory is referred to as the Java heap. The memory consumed by Java objects is allocated from the Java heap. When the new operator is used, it essentially asks the VM to allocate a chunk of memory from the Java heap and return a reference to it. In addition to typical Java objects used in a program, frames (and, hence, local variables) can be allocated from the heap. Depending on the implementation of a VM, the heap may also contain the code (or bytecodes) of the methods of an application. Keep in mind that anyone can implement a JVM as long as it follows the specifications defined by Sun Microsystems.

The JVM has a GC that manages the data created during the lifetime of the VM. In Java, we do not have direct control over the heap, and the reclamation of objects is the responsibility of the GC. The GC is also responsible for compacting the Java heap to reduce memory fragmentation. We will discuss the GC in detail in this chapter.

An implementation of the JVM may choose to have a runtime-optimizing compiler. The compiler can optimize the bytecodes of a method and compile them to the language of the underlying hardware to increase the execution efficiency of an application. The HotSpot VM has this capability. We will have an in-depth discussion of some of the optimizations performed by the HotSpot VM.



Practical Java Game Programming
Practical Java Game Programming (Charles River Media Game Development)
ISBN: 1584503262
EAN: 2147483647
Year: 2003
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net