jvm a brief introduction

40
JVM: a brief introduction by Artem Shubovych

Upload: artem-shoobovych

Post on 13-Apr-2017

129 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Jvm  a brief introduction

JVM: a brief introductionby Artem Shubovych

Page 2: Jvm  a brief introduction

What is JVM?

JVM (Java Virtual Machine) - is an environment, designed to run Java programs.

Page 3: Jvm  a brief introduction

What is JVM?

Page 4: Jvm  a brief introduction

What is JVM?

Page 5: Jvm  a brief introduction

What is JVM?

Bytecode

Page 6: Jvm  a brief introduction

What is JVM?

JVM is a environment to run Java bytecode..?

Page 7: Jvm  a brief introduction

What is JVM?

Page 8: Jvm  a brief introduction

What the hell is JVM?

Page 9: Jvm  a brief introduction

What is JVM?

Page 10: Jvm  a brief introduction

What is JVM?

Java APIsClass loader

Runtime

JIT compilerGC

OS + Hardware

Application

JVM

JRE

Page 11: Jvm  a brief introduction

Optimizes code?

Page 12: Jvm  a brief introduction

How do computers run programs?

Programming language Compiler Processor

commands

Page 13: Jvm  a brief introduction
Page 14: Jvm  a brief introduction

Let’s check out the example...#include <stdio.h>

int main() { int *a = new int[10];

printf("size(a) = %lu\n", sizeof(a) / sizeof(int)); return 0;}

Page 15: Jvm  a brief introduction

Well, screw it! It’s 2016 already!

Page 16: Jvm  a brief introduction

Managed languages

Page 17: Jvm  a brief introduction

JVM puts it the level higher

Programming language Compiler

Virtual Machine

commands

Page 18: Jvm  a brief introduction

What’s the purpose of having Virtual Machine?

Page 19: Jvm  a brief introduction

It can handle many things for us

Page 20: Jvm  a brief introduction

Memory management

MyClass *obj = new MyClass();

// ...

delete obj;

MyClass **objs = new (MyClass*)[100];

for (int i = 0; i < 100; ++i) { objs[i] = new MyClass();}

delete objs;

In C++ you were responsible for freeing the memory:

Page 21: Jvm  a brief introduction

Memory management

Java will free the memory for you. If it can’t do this - it just kills your process.

Page 22: Jvm  a brief introduction

Code optimization

JIT can optimize your code in many different ways.

Page 23: Jvm  a brief introduction

Short examplepublic class Main { public static final String[] opts = { "Eggs", "Chickens", "Philosophers" };

public static void main(String[] args) { for (String option : opts) { process(option); } }

public static void process(String option) { System.out.printf("%s\n", option); }}

Page 24: Jvm  a brief introduction

Loops unrollingpublic class Main { public static final String[] opts = { "Eggs", "Chickens", "Philosophers" };

public static void main(String[] args) { process(opts[0]); process(opts[1]); process(opts[2]); }

public static void process(String option) { System.out.printf("%s\n", option); }}

Page 25: Jvm  a brief introduction
Page 26: Jvm  a brief introduction

Reducing unneeded data loadingpublic class Main { public static void main(String[] args) { System.out.printf("%s\n", "Eggs"); System.out.printf("%s\n", "Chickens"); System.out.printf("%s\n", "Philosophers"); }}

Page 27: Jvm  a brief introduction
Page 28: Jvm  a brief introduction

Reducing unneeded data loadingpublic class Main { public static void main(String[] args) { System.out.printf("%s\n", "Eggs"); System.out.printf("%s\n", "Chickens"); System.out.printf("%s\n", "Philosophers"); }}

And unneeded method calls!

Page 29: Jvm  a brief introduction
Page 30: Jvm  a brief introduction

Reducing unneeded data loadingpublic class Main { public static void main(String[] args) { System.out.printf("%s\n", "Eggs"); System.out.printf("%s\n", "Chickens"); System.out.printf("%s\n", "Philosophers"); }}

Reduced memory cost here

Page 31: Jvm  a brief introduction
Page 32: Jvm  a brief introduction

How can we call those?

Page 33: Jvm  a brief introduction

OpenJDK’s JIT (Hotspot) has two modes

Page 34: Jvm  a brief introduction

Client mode (C1)

●Less aggressive inlines

●Fewer optimization possibilities

Page 35: Jvm  a brief introduction

Server mode (C2)

●Aggressive inlines

●Based on rich runtime profiling

Page 36: Jvm  a brief introduction

Why inlines?

Page 37: Jvm  a brief introduction

Because they’re fast!

● Inlining is cheap (in means of time)● Inlining gives great speed-up

Page 38: Jvm  a brief introduction

How can we call those?

Page 39: Jvm  a brief introduction

How can we call those?

● C1 tier: 1000 calls to the same method● C2 tier: 100_000 calls to the same method

Page 40: Jvm  a brief introduction