jvm a brief introduction

Post on 13-Apr-2017

129 Views

Category:

Engineering

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

JVM: a brief introductionby Artem Shubovych

What is JVM?

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

What is JVM?

What is JVM?

What is JVM?

Bytecode

What is JVM?

JVM is a environment to run Java bytecode..?

What is JVM?

What the hell is JVM?

What is JVM?

What is JVM?

Java APIsClass loader

Runtime

JIT compilerGC

OS + Hardware

Application

JVM

JRE

Optimizes code?

How do computers run programs?

Programming language Compiler Processor

commands

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

Well, screw it! It’s 2016 already!

Managed languages

JVM puts it the level higher

Programming language Compiler

Virtual Machine

commands

What’s the purpose of having Virtual Machine?

It can handle many things for us

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:

Memory management

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

Code optimization

JIT can optimize your code in many different ways.

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

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

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"); }}

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!

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

How can we call those?

OpenJDK’s JIT (Hotspot) has two modes

Client mode (C1)

●Less aggressive inlines

●Fewer optimization possibilities

Server mode (C2)

●Aggressive inlines

●Based on rich runtime profiling

Why inlines?

Because they’re fast!

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

How can we call those?

How can we call those?

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

top related