Java Virtual Machine

21
1 Java Virtual Machine Student Name: Wei Liu Student ID: 104076

description

Java Virtual Machine. Student Name: Wei Liu Student ID: 104076. 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. - PowerPoint PPT Presentation

Transcript of Java Virtual Machine

Page 1: Java Virtual Machine

1

Java Virtual Machine

Student Name: Wei Liu

Student ID: 104076

Page 2: Java Virtual Machine

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.

Page 3: Java Virtual Machine

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.

Page 4: Java Virtual Machine

4

Java Runtime Environment.

Page 5: Java Virtual Machine

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.

Page 6: Java Virtual Machine

6

How JVM works?

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

Page 7: Java Virtual Machine

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.

Page 8: Java Virtual Machine

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.

Page 9: Java Virtual Machine

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.

Page 10: Java Virtual Machine

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.

Page 11: Java Virtual Machine

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.

Page 12: Java Virtual Machine

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.

Page 13: Java Virtual Machine

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.

Page 14: Java Virtual Machine

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.

Page 15: Java Virtual Machine

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.

Page 16: Java Virtual Machine

16

JIT Compiler.

Java Compiler Java Virtual Machine

Machine

Native Machine Code

CompiledByte Code JIT Compiler

Optimized

& Kept in Log

Page 17: Java Virtual Machine

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

Page 18: Java Virtual Machine

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.

Page 19: Java Virtual Machine

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.

Page 20: Java Virtual Machine

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

Page 21: Java Virtual Machine

21

Questions?