JVM: a brief introduction
by 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 APIs
Class loader
Runtime
JIT compiler
GC
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) = %lun", 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 example
public 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("%sn", option);
}
}
Loops unrolling
public 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("%sn", option);
}
}
Reducing unneeded data loading
public class Main {
public static void main(String[] args) {
System.out.printf("%sn", "Eggs");
System.out.printf("%sn", "Chickens");
System.out.printf("%sn", "Philosophers");
}
}
Reducing unneeded data loading
public class Main {
public static void main(String[] args) {
System.out.printf("%sn", "Eggs");
System.out.printf("%sn", "Chickens");
System.out.printf("%sn", "Philosophers");
}
}
And unneeded method calls!
Reducing unneeded data loading
public class Main {
public static void main(String[] args) {
System.out.printf("%sn", "Eggs");
System.out.printf("%sn", "Chickens");
System.out.printf("%sn", "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
Jvm  a brief introduction

Jvm a brief introduction

  • 1.
    JVM: a briefintroduction by Artem Shubovych
  • 2.
    What is JVM? JVM(Java Virtual Machine) - is an environment, designed to run Java programs.
  • 3.
  • 4.
  • 5.
  • 6.
    What is JVM? JVMis a environment to run Java bytecode..?
  • 7.
  • 8.
  • 9.
  • 10.
    What is JVM? JavaAPIs Class loader Runtime JIT compiler GC OS + Hardware Application JVM JRE
  • 11.
  • 12.
    How do computersrun programs? Programming language Compiler Processor commands
  • 14.
    Let’s check outthe example... #include <stdio.h> int main() { int *a = new int[10]; printf("size(a) = %lun", sizeof(a) / sizeof(int)); return 0; }
  • 15.
    Well, screw it!It’s 2016 already!
  • 16.
  • 17.
    JVM puts itthe level higher Programming language Compiler Virtual Machine commands
  • 18.
    What’s the purposeof having Virtual Machine?
  • 19.
    It can handlemany things for us
  • 20.
    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:
  • 21.
    Memory management Java willfree the memory for you. If it can’t do this - it just kills your process.
  • 22.
    Code optimization JIT canoptimize your code in many different ways.
  • 23.
    Short example public classMain { 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("%sn", option); } }
  • 24.
    Loops unrolling public classMain { 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("%sn", option); } }
  • 26.
    Reducing unneeded dataloading public class Main { public static void main(String[] args) { System.out.printf("%sn", "Eggs"); System.out.printf("%sn", "Chickens"); System.out.printf("%sn", "Philosophers"); } }
  • 28.
    Reducing unneeded dataloading public class Main { public static void main(String[] args) { System.out.printf("%sn", "Eggs"); System.out.printf("%sn", "Chickens"); System.out.printf("%sn", "Philosophers"); } } And unneeded method calls!
  • 30.
    Reducing unneeded dataloading public class Main { public static void main(String[] args) { System.out.printf("%sn", "Eggs"); System.out.printf("%sn", "Chickens"); System.out.printf("%sn", "Philosophers"); } } Reduced memory cost here
  • 32.
    How can wecall those?
  • 33.
  • 34.
    Client mode (C1) ●Less aggressive inlines ● Fewer optimization possibilities
  • 35.
    Server mode (C2) ●Aggressive inlines ● Based on rich runtime profiling
  • 36.
  • 37.
    Because they’re fast! ●Inlining is cheap (in means of time) ● Inlining gives great speed-up
  • 38.
    How can wecall those?
  • 39.
    How can wecall those? ● C1 tier: 1000 calls to the same method ● C2 tier: 100_000 calls to the same method

Editor's Notes

  • #4 But that’s not quite right...
  • #5 There are couple of languages, other than Java, which do run on JVM.
  • #6 So how these all languages work is they are compiled into runtime-environment-compatible bytecode. This is how .NET works (compiles F#, C#, VB, VC and many others into Common Intermediate Language, CIL). This is how JVM works (compiles all those showed on a slide into a JRE-compatible bytecode).
  • #7 So is JVM an environment, designed to run Java bytecode?
  • #8 The answer is NO.
  • #10 Drums...
  • #11 Guess, where’s the JVM?
  • #12 That’s JIT. To get closer to JIT we need to go deeper, to the very beginning of programming...
  • #16 Yes, programs are run exactly like that. IF you write them in C or something that low-level. But when we go level up, we notice those “managed languages” out there.