The JVM is a virtual machine that runs Java bytecode. It provides memory management, code optimization through just-in-time compilation, and allows Java programs to run on different platforms. The JVM optimizes code at runtime by techniques like loop unrolling, reducing unnecessary data loading and method calls. It has two compilation modes - client mode for less optimization and server mode for more aggressive optimization based on runtime profiling.
12. How do computers run programs?
Programming
language
Compiler
Processor
commands
13.
14. 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;
}
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:
23. 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);
}
}
24. 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);
}
}
25.
26. 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");
}
}
27.
28. 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!
29.
30. 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
39. How can we call those?
● C1 tier: 1000 calls to the same method
● C2 tier: 100_000 calls to the same method
Editor's Notes
But that’s not quite right...
There are couple of languages, other than Java, which do run on JVM.
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).
So is JVM an environment, designed to run Java bytecode?
The answer is NO.
Drums...
Guess, where’s the JVM?
That’s JIT. To get closer to JIT we need to go deeper, to the very beginning of programming...
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.