ftd jvm internals

41
JVM Internals FTD Group

Upload: felipe-mamud

Post on 17-Jun-2015

339 views

Category:

Technology


8 download

TRANSCRIPT

Page 1: FTD JVM Internals

JVM InternalsFTD Group

Page 2: FTD JVM Internals

acknowledgment

● God

● iMasters

● FTD Group

Page 3: FTD JVM Internals

who we are?

FTD Grouphttp://friendstechday.wordpress.com/about/

Page 4: FTD JVM Internals

JVM? WORA? What?

Page 5: FTD JVM Internals

Java Virtual Machine

● The Java Virtual Machine Specification● Written in C/C++● Stack based VM● Different implementations● Symbolic reference● Garbage Collected● Java Bytecode● JIT (Just-In-Time)

Page 6: FTD JVM Internals
Page 7: FTD JVM Internals

Stack based VM

Page 8: FTD JVM Internals

1 + 2 = ?

Page 9: FTD JVM Internals

(+ 1 2)

Lisp? RPN?

Page 10: FTD JVM Internals

operand stack

PUSH 1 11

PUSH 2 2ADD 3

Page 11: FTD JVM Internals

operand stack - bytecode

iconst_1 11

iconst_2 2iadd 3

Page 12: FTD JVM Internals

Java bytecode is the instruction set of the Java virtual machine

opcode = 1 or 2 bytes256 possible opcodes198 are currently in use51 are reserved for future use3 are set aside as permanently unimplemented

list of all opcodes: http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html

Page 13: FTD JVM Internals

variable declaration

int i;

0: iconst_0 // push 0 to top of the operand stack

1: istore_1 // Pop value from top of operand stack and

store as local variable 1

Page 14: FTD JVM Internals

object instantiation

Object ftd = new Object();

0: new #2 // Class java/lang/Object

1: dup // duplicate the value on top of the stack

2: invokespecial #3 // Method java/lang/Object “<init>()V”

Page 15: FTD JVM Internals

#1, #4, #100? #Wat?

Page 16: FTD JVM Internals

Runtime Constant Pool

The constant pool contains the following types:

● Integer, Long, Float, Double● String, Uft8● Class● NameAndType● Fieldref, Methodref, InterfaceMethodref

Page 17: FTD JVM Internals

Java Bytecode Type Description

B byte signed byte

C char Unicode character

D double double-precision floating-point value

F float single-precision floating-point value

I int integer

J long long integer

L<classname> reference an instance of class <classname>

S short signed short

Z boolean true or false

[ reference one array dimension

Page 18: FTD JVM Internals

Java Code Java Bytecode Expression

double d[][][]; [[[D

Object mymethod(int I, double d, Thread t) (IDLjava/lang/Thread;)Ljava/lang/Object;

Page 19: FTD JVM Internals

javap -v SimpleClass

Page 20: FTD JVM Internals

Not only Java Stack lives, but also Heap.

Page 21: FTD JVM Internals

Java Heap memory

Page 22: FTD JVM Internals

Code Cache

used for compilation and storage of methods that have been compiled to native code by the JIT

compiler

Page 23: FTD JVM Internals

if >= java1.8

no PermGenyes Metaspace

Page 24: FTD JVM Internals

everything is wonderful in Java?.... it depends bro!

Page 25: FTD JVM Internals

which the size of a Java String with "Java"

content?

String java = “Java”;

Page 26: FTD JVM Internals

answer: 64 bytes

Ahh… how.. WATTT???

Page 27: FTD JVM Internals

Java memory consumption

8 bytes Object Header8 bytes Object reference

(x86 may use XX:+UseCompressedOops)

8 bytes alignment (WAT?)

Page 28: FTD JVM Internals

data alignment

8 bytes alignment

5 bytes used 3 bytes paddind

Page 29: FTD JVM Internals

String explanation

Field Type Size (bytes)

HEADER 16

value “Java” char[] (16 + 8) + (4 * 2) = 32

offset int 4

count int 4

hash int 4

PADDING = 60 + 4

TOTAL 64 bytes

Page 30: FTD JVM Internals

which the size of a java.lang.Boolean?

Boolean b = false;

Page 31: FTD JVM Internals

answer: 24 bytes

Ahh… how.. WATTT???

Page 32: FTD JVM Internals

Ok .. But what about classes? OOP?

Page 33: FTD JVM Internals

Class File FormatClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info contant_pool[constant_pool_count – 1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count];}

Page 34: FTD JVM Internals

javap -v SimpleClass

Page 35: FTD JVM Internals

And what about the Classloader?

Page 36: FTD JVM Internals

1. Create a initial class using Bootstrap classloader;

2. links the initial class;3. initializes it;4. and invokes the public class method void

main(String[])

JVM initialization order

Page 37: FTD JVM Internals

find, check headers class file, super loading, binary created

3 steps, verifying and preparing the type and its direct supers

structurally correct, and obeys the semantic requirements

allocation of memory foe data structures

executing initialization method <clinit>

(optional) checking corrected symbolic references

Page 38: FTD JVM Internals
Page 39: FTD JVM Internals

Java debug without ide? Is it possible?

Page 40: FTD JVM Internals

jdb (included in the JDK 1.2

package)

Page 41: FTD JVM Internals

FTD Group

THANK YOU!

JOIN US!

“And be not conformed to this world: but be ye transformed by the renewing of your mind, that ye may prove what {is} that good, and acceptable, and perfect, will of God.” (Romans 12:2)