30
JAVA VIRTUAL MACHINE THE REAL THING: JAVA VIRTUAL MACHINE

The Real Thing: Java Virtual Machine

Embed Size (px)

Citation preview

Page 1: The Real Thing: Java Virtual Machine

JAVA VIRTUAL MACHINETHE REAL THING: JAVA VIRTUAL MACHINE

Page 2: The Real Thing: Java Virtual Machine

http://frontech.com.tr

HIZIR SEFA İRKEN [email protected]

Page 3: The Real Thing: Java Virtual Machine

THE REAL THING: JAVA VIRTUAL MACHINE

WHAT?

▸ VM is a layer of abstraction

▸ once only Java, but not anymore (after JDK 5)

▸ write once, run anywhere

▸ memory managed

▸ concurrent

▸ secure

Page 4: The Real Thing: Java Virtual Machine

THE REAL THING: JAVA VIRTUAL MACHINE

OUTSIDE

Page 5: The Real Thing: Java Virtual Machine

THE REAL THING: JAVA VIRTUAL MACHINE

INSIDE .CLASS FILE

JAVA VIRTUAL MACHINE

CLASS LOADER EXECUTION ENGINE

RUNTIME DATA AREAS

Page 6: The Real Thing: Java Virtual Machine

THE REAL THING: JAVA VIRTUAL MACHINE

INSIDE - KEY COMPONENTS

Page 7: The Real Thing: Java Virtual Machine

INSIDE

COMPONENTS OF JVM

1. bytecode verifier

2. class loader

3. execution engine

4. garbage collector

5. security manager

Page 8: The Real Thing: Java Virtual Machine

BYTECODE VERIFIER

BYTECODE VERIFIER

‣ first step in JVM is inspection

‣ checks for unusual code

‣ crucial for security

Page 9: The Real Thing: Java Virtual Machine

BYTECODE VERIFIER

WHAT BYTECODE CHECKED FOR

‣ variables are initialized before they are used

‣ method calls match the types of object references

‣ rules for accessing private data and methods are not violated.

‣ local variable accesses fall within the runtime stack

‣ the runtime stack does not overflow

Page 10: The Real Thing: Java Virtual Machine

BYTECODE VERIFIER

HOW BYTECODE INSTRUCTIONS LOOK LIKE

Page 11: The Real Thing: Java Virtual Machine

CLASS LOADER

CLASS LOADER

‣ JVM doesn't need to know anything about runtime classes

‣ performs three main functions of JVM:

‣ loading

‣ linking

‣ initialization

‣ loads from anywhere

‣ every runtime has at least three class loaders

Page 12: The Real Thing: Java Virtual Machine

CLASS LOADER

THE LOADERS

‣ bootstrap class loader:loads the system classes (typically from rt.jar)

‣ extension class loader:loads “standart extensions” from the jre/lib/ext directoryjre/lib/endorsed will force override standard APIs

‣ system class loader:loads the application classes from classpath.

Page 13: The Real Thing: Java Virtual Machine

EXECUTION ENGINE

EXECUTION ENGINE

‣ convert bytecode to machine code

‣ responsible for executing instructions contained in the methods of loaded classes

‣ It has two parts:

‣ interpreter (ahead-of-time)

‣ JIT compiler aka just-in-time interpreter

Page 14: The Real Thing: Java Virtual Machine

JIT

JUST IN TIME - JIT

‣ advanced part of JVM

‣ starts after the program has started and compiles the code on the fly

‣ JIT has access to dynamic runtime information whereas a standard compiler doesn't

‣ optimizes by compiling similar bytecodes at same time

‣ reduces overall execution time, improves performance

Page 15: The Real Thing: Java Virtual Machine

JIT

OPTIMIZATIONS

‣ inlining

‣ local optimizations

‣ control flow optimizations

‣ global optimizations

‣ native code generation

‣ reference: http://www.ibm.com/support/knowledgecenter/SSYKE2_7.0.0/com.ibm.java.lnx.70.doc/diag/understanding/jit_optimize.html

Page 16: The Real Thing: Java Virtual Machine

JIT

IN ACTION

Page 17: The Real Thing: Java Virtual Machine

GARBAGE COLLECTOR

GARBAGE COLLECTOR‣ garbage collecting: freeing objects that are no longer referenced by the program

‣ in short checks periodically for not-needed objects

‣ no memory management needed from programmer, but be careful

‣ types:

‣ serial collector

‣ parallel collector

‣ parallel compacting collector

‣ concurrent mark-sweep (CMS) collector

‣ garbage-first (G1) garbage collector

Page 18: The Real Thing: Java Virtual Machine

GARBAGE COLLECTOR

G1 IS DESIGNED FOR‣ can operate concurrently with applications threads like the CMS collector

‣ compact free space without lengthy GC induced pause times

‣ need more predictable GC pause durations

‣ do not want to sacrifice a lot of throughput performance

‣ do not want to require a much larger Java heap

Page 19: The Real Thing: Java Virtual Machine

HEAP

HEAP STRUCTURE

Page 20: The Real Thing: Java Virtual Machine

HEAP

AFTER YOUNG GENERATION COLLECTION

Page 21: The Real Thing: Java Virtual Machine

SECURITY MANAGER

SECURITY MANAGER

‣ not the security you think

‣ an object responsible for guarding security policies

‣ constantly monitors the code

‣ always consulted before any potentially dangerous operation

Page 22: The Real Thing: Java Virtual Machine

SECURITY MANAGER

EXAMPLE

Page 23: The Real Thing: Java Virtual Machine

SECURITY MANAGER

OUTPUT

Page 24: The Real Thing: Java Virtual Machine

OUT OF MEMORY!BONUS TOPIC

Page 25: The Real Thing: Java Virtual Machine

OUT OF MEMORY

TYPES

‣ java heap space

‣ permgen space

‣ gc overhead limit exceeded

‣ unable to create new native thread

‣ and more..

Page 26: The Real Thing: Java Virtual Machine

OUT OF MEMORY

PERMGEN

‣ occurs when JVM wants to load new class definitions, but there is not enough space in PermGen space

‣ you are trying to use more than you have - increase PermGen space

‣ possible class loader leak in your application or app server - needs code fix

Page 27: The Real Thing: Java Virtual Machine

OUT OF MEMORY

GC OVERHEAD LIMIT EXCEEDED

‣ overhead limit is the policy to allow jam to detect potential OOM conditions beforehand

‣ caused by:

‣ too many full GC iterations

‣ too much time spent in GC

‣ fine tune jvm params and your GC, use GC logging

Page 28: The Real Thing: Java Virtual Machine

OUT OF MEMORY

UNABLE TO CREATE NEW NATIVE THREAD

‣ JVM ask for new thread from OS, OS can not allocate a new thread anymore

‣ check ulimit, this is a OS config

‣ use thread-pools if possible

‣ reduce heap space

‣ perform vertical scaling

Page 29: The Real Thing: Java Virtual Machine

OUT OF MEMORY

JAVA HEAP SPACE

‣ you ran out of allocated memory for your application

‣ memory can be increased by JVM params

‣ check your code for memory leaks and unused datasets

‣ diagnose problem with heap dump -jmap

Page 30: The Real Thing: Java Virtual Machine

REFERENCES

REFERENCES‣ https://en.wikipedia.org/wiki/Memory_management#DYNAMIC

‣ https://en.wikipedia.org/wiki/Java_virtual_machine

‣ http://www.oracle.com/technetwork/java/javase/tech/memorymanagement-whitepaper-1-150020.pdf

‣ https://books.google.com.tr/books?id=K7i6AgAAQBAJ

‣ https://docs.oracle.com/javase/8/docs/technotes/guides/#javavm

‣ https://blog.frankel.ch/java-security-manager/#gsc.tab=0

‣ https://docs.oracle.com/javase/tutorial/essential/environment/security.html

‣ http://www.slideshare.net/surbhiiiii/javajava-virtual-machine

‣ http://www.slideshare.net/BaabtraMentoringPartner/java-virtual-machine-43141334

‣ http://www.slideshare.net/pritybhudolia/jvm-20513648

‣ http://www.slideshare.net/aparnachaudhary/understanding-jvm

‣ https://docs.oracle.com/javase/specs/jvms/se8/html/index.html

‣ https://docs.oracle.com/javase/specs/jvms/se7/jvms7.pdf

‣ http://www.oracle.com/technetwork/tutorials/tutorials-1876574.html

‣ https://docs.oracle.com/javase/8/docs/technotes/guides/vm/

‣ https://docs.oracle.com/javase/8/docs/technotes/guides/vm/G1.html