FTD JVM Internals

Post on 17-Jun-2015

328 views 8 download

Tags:

transcript

JVM InternalsFTD Group

acknowledgment

● God

● iMasters

● FTD Group

who we are?

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

JVM? WORA? What?

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)

Stack based VM

1 + 2 = ?

(+ 1 2)

Lisp? RPN?

operand stack

PUSH 1 11

PUSH 2 2ADD 3

operand stack - bytecode

iconst_1 11

iconst_2 2iadd 3

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

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

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”

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

Runtime Constant Pool

The constant pool contains the following types:

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

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

Java Code Java Bytecode Expression

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

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

javap -v SimpleClass

Not only Java Stack lives, but also Heap.

Java Heap memory

Code Cache

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

compiler

if >= java1.8

no PermGenyes Metaspace

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

which the size of a Java String with "Java"

content?

String java = “Java”;

answer: 64 bytes

Ahh… how.. WATTT???

Java memory consumption

8 bytes Object Header8 bytes Object reference

(x86 may use XX:+UseCompressedOops)

8 bytes alignment (WAT?)

data alignment

8 bytes alignment

5 bytes used 3 bytes paddind

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

which the size of a java.lang.Boolean?

Boolean b = false;

answer: 24 bytes

Ahh… how.. WATTT???

Ok .. But what about classes? OOP?

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];}

javap -v SimpleClass

And what about the Classloader?

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

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

Java debug without ide? Is it possible?

jdb (included in the JDK 1.2

package)

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)