Java Virtual MachineStudent Name: Wei LiuStudent ID: 104076
Java Virtual MachineJVM is part of Java programming language.JVM is a software, staying on top of Operating System, such as UNIX, Windows NT.It help Java create high level of portability by hiding the difference between the Operating System implementations. It creates an environment that Java language lives.
Why JVM?An ordinary language can not create a system independent program.Javas goal is Write-Once-Run-Anywhere. Java programs are not computer, operating system dependent.Need to create an abstract computer of its own and runs on it, a kind of virtual machine which hiding the different OS implementations.
Java Runtime Environment.
How JVM works?Java programs are compiled into byte code. JVM interprets and converts Java byte code into machine code in order to execute on a CPU.Most web browser has an integrated JVM to run applets.
How JVM works?Other JVM tasks include:Object creations of Java programs.Garbage collection.Security responsibility.
JVM Fundamental Parts.A byte code instruction set A set of registers A stack A garbage-collected heap An area for storing methods
All five parts are necessary, may be implemented by a compiler, an interpreter or a hardware chip.
Byte Code Instruction SetJVM keeps a compact set of Byte Code Instructions in order to interpret byte code into native binary code.Java compilers do not translate programs directly into native binary code, which is system dependent. Instead, programs are translated into byte code, just in its mid-way to a runnable.JVM interprets these half-cooked byte code into executable machine code on different computer systems and platforms.
Registers.The registers of the Java virtual machine are just like the registers inside a real computer. (32 bit wide)PC: program counterOPTOP: Pointer to operation stack.FRAME: Pointer to execution environment of current method.VARS: Pointer to the first local variable of current method.
StacksJVM is stack based.The stack is used to supply parameters to byte codes and methods, and to receive results back from them.Each stack frame contains three (possibly empty) sets of data: the local variables for the method call, its execution environment, and its operand stack.
Heaps.The heap is that part of memory from which newly created instances (objects) are allocated.The heap is often assigned a large, fixed size when the Java run-time system is started, but on systems that support virtual memory, it can grow as needed, in a nearly unbounded fashion.Objects in heap are automatically garbage-collected when they are not needed.
The Method Area.The method area stores the Java byte codes that implement almost every method in the Java system.The method area also stores the symbol tables needed for dynamic linking, and any other additional information debuggers or development environments might want to associate with each methods implementation.
Drawbacks of JVM.JVM is a layer on the top of your operating system that consumes additional memory.JVM is additional layer between compiler and machine. (Comparing Java program and fast C program!)Byte code is compiled for system independence so it does not take advantage of any particular operating system.
JIT Compiler.JIT stands for Just In Time.10 years ago, a smart idea was discovered by Peter Deutsch while trying to make Smalltalk run faster. He called it dynamic translation during interpretation. Every time JIT compiler interprets byte codes, it will keep the binary code in log and optimize it. Next time, when the same method is running, the optimized code will run. Experiments show Java programs using JIT could be as fast as a compiled C program.
- JIT Example.( Loop with 1000 times ) for(int i=0;i
JIT Compiler.CompiledByte CodeJava CompilerJava Virtual MachineMachine
Native Machine Code
JIT CompilerOptimized& Kept in Log
JVM Security Capability.JVM has many capabilities to keep the security of the computer system.Sandbox: prohibit a Java applet:Reading or writing to the local disk Making a network connection to any host, except the host from which the applet came Creating a new process Loading a new dynamic library and directly calling a native method
Security Holes of JVMNetscape 4.X consists of JVM that has flaws. A hostile applet could turn the client browser into an http server that allows almost anyone in the world to read/modify/delete files residing on the client (turned server) machine. Ways to deal with malicious applets.
Current JVM.Microsoft Java Virtual Machine.Netscape Java Virtual Machine.Sun Java Virtual Machine.All these Java Virtual Machines implement Java core class packages and their own specific class packages.