Download ppt - Java Virtual Machine

Transcript

1

Java Virtual Machine

Student Name: Wei Liu

Student ID: 104076

2

Java Virtual Machine

JVM 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.

3

Why JVM?

An ordinary language can not create a system independent program.

Java’s 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.

4

Java Runtime Environment.

5

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.

6

How JVM works?

Other JVM tasks include: Object creations of Java programs. Garbage collection. Security responsibility.

7

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.

8

Byte Code Instruction Set

JVM 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.

9

Registers.

The registers of the Java virtual machine are just like the registers inside a “real” computer. (32 bit wide)

PC: program counter OPTOP: Pointer to operation stack. FRAME: Pointer to execution environment of current

method. VARS: Pointer to the first local variable of current

method.

10

Stacks

JVM 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.

11

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.

12

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 method’s implementation.

13

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.

14

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.

15

JIT Example.

( Loop with 1000 times ) for(int i=0;i<1000;i++){ do_action( ); } Without JIT, JVM will interpret do_action() method

1000 times. (A waste of time!) With JIT, JVM interprets do_action() method only

once and keeps it in log, and the binary native code will execute for the rest 999 loops.

16

JIT Compiler.

Java Compiler Java Virtual Machine

Machine

Native Machine Code

CompiledByte Code JIT Compiler

Optimized

& Kept in Log

17

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

18

Security Holes of JVM

Netscape 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.

19

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.

20

References.

http://java.sun.com http://www.cs.princeton.edu http://www.research.IBM.com http://www.javaworld.com http://www.sans.org http://www.javacoffeebreak.com http://www.computerworld.com http://www.zdnet.com

21

Questions?