JVM
A quick view about Java Virtual Machine
Once upon a time...
Java as lang and Platform
Java is also a Programming Language and a Platform. We’ll speak about Java
as Platform in this presentation.
As Language, Java is pretty nice despite of verbosity.
Why use a VM?
Once a program is created, it must be compiled
for each OS. If you want your program for Mac,
compile for Mac. Windows? Same. Linux?
Same…
Furthermore, memory allocation needed be done
manually.
Using JVM, all responsability to handle memory
or run in many OSs is delegated to it.
The JVM abstracts not only the hardware layer
but also the communication with the Operating
System.
How does it work?
Hello.java Hello.class
Javac
JRE
JRE
Java Bytecode
Compiled from "Hello.java"
class HelloJava {
HelloJava();
public static void
main(java.lang.String[]);
}
class HelloJava {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Kotlin Bytecode
Compiled from "Hello.kt"
public final class Hello {
public Hello();
}
class Hello
fun main(args: Array<String>) {
println("Hello World!")
}
Heap
Heap
The JVM has a heap, a runtime data
area from which memory for all
class instances and arrays are
allocated. Is shared by all Threads.
It is where the Objects born, grow
and die. Example:
$ java -Xms128m -Xmx512m -jar
homeboy-1.0-SNAPSHOT.jar
To set the maximum Java heap size
we use the option -Xmx.
to set the initial Java heap size
-Xms.
Heap
Eden Survivor Old
PermGen
PermGen
The JVM also allocate objects apart the heap, at
Permgen.
This area stores class related data from class
definitions, structures, methods, field, and
constants.
PermGen
Despite standing in his name, the memory space
from PermGen is also collected in a FullGC.
IT can cause java.lang.OutOfMemoryError:
PermGen space if it runs out if space. This
happens normally when the exaggerated
amount of classes that are loaded into memory.
“PermGen is Dead. Long live PermGen”
-JDK 8 HotSpot JVM
Metaspace
Metaspace is a new memory space - since java
8 - replacing Permgen.
What makes it different of Permgen?
Instead of allocate memory inside JVM, it
allocates in the OS memory.
Garbage Collector
Garbage Collector - a quick view on Heap (again)
Eden Survivor Old
Heap
Garbage collector - Mark and sweep
On the first cycle, this algorithm marks all accessible objects in all
current threads.
On the second cycle Sweeps all objects which were not marked in
the last cycle.
The Mark and Sweep is outdated, but it was a great learning to deal
with non-referenced objects.
Garbage collector - generational copying
● The idea of this algorithm is to divide objects into generations, young and old generation.
● The new generation is usually smaller than the old one (1/3 of the old).
● The GC scans the younger generation, which does not paralyze the JVM.
● Who "survives" is copied to the Old generation and the space of the Young Generation is
made available again.
● The discard does not remove from the memory yet, it just marks the memory as
available.
● Although it is costly to copy an object to the old generation to another part of the Heap,
few survive, so the cost is low.
● Also, copying fewer objects is still "cheaper" than removing objects.
● When Young is crowded, a minor collect runs. At this point objects are sent to the old
generation.
Garbage collector - generational copying
● Depending on the JVM, large objects have the behavior of already being copied to the Old
generation;
● Generational copying copies the objects to the next in a clustered fashion, optimizing
memory and ensuring that there is no memory fragmentation.
● It is possible to increase the proportion of the space allocated to Young Generation by
the option -XX: NewRatio = 1 (50%). By default, 2/3 will be old and 1/3 will be young; by
changing the value to 1, we will have 1/2 for each area.
Garbage collector - Garbage First (G1)
Although it is considered a generational
algorithm, it does not separates the heap in
two generations, but in many small regions.
Dynamically, the G1 chooses some regions
to be collected (which gives the logical
sense of those regions to be a young gen).
JIT Compiler
JIT Compiler (Just in time) or slow is your granny
JIT compiles Java code in Runtime to improve the application
performance.
But I said before that JVM read Bytecode and runs it in a VM,
right?
The first JVMs were the same, but with each version, the JVM is
increased and brought this new feature to the VM.
JIT Compiler (Just in time)
JIT has the ability to do optimization according to the use of the
program and the current conditions. The so-called hotspots.
What JIT does behind the scene?
It compiles for native platform and runs native code optimizing
the application running.
Thanks =)
By João Santana
joaosantana.ti@gmail.com
github.com/jonss

A quick view about Java Virtual Machine

  • 1.
    JVM A quick viewabout Java Virtual Machine
  • 2.
    Once upon atime...
  • 3.
    Java as langand Platform Java is also a Programming Language and a Platform. We’ll speak about Java as Platform in this presentation. As Language, Java is pretty nice despite of verbosity.
  • 4.
    Why use aVM? Once a program is created, it must be compiled for each OS. If you want your program for Mac, compile for Mac. Windows? Same. Linux? Same… Furthermore, memory allocation needed be done manually. Using JVM, all responsability to handle memory or run in many OSs is delegated to it. The JVM abstracts not only the hardware layer but also the communication with the Operating System.
  • 5.
    How does itwork? Hello.java Hello.class Javac JRE JRE
  • 6.
    Java Bytecode Compiled from"Hello.java" class HelloJava { HelloJava(); public static void main(java.lang.String[]); } class HelloJava { public static void main(String[] args) { System.out.println("Hello World!"); } }
  • 7.
    Kotlin Bytecode Compiled from"Hello.kt" public final class Hello { public Hello(); } class Hello fun main(args: Array<String>) { println("Hello World!") }
  • 8.
  • 9.
    Heap The JVM hasa heap, a runtime data area from which memory for all class instances and arrays are allocated. Is shared by all Threads. It is where the Objects born, grow and die. Example: $ java -Xms128m -Xmx512m -jar homeboy-1.0-SNAPSHOT.jar To set the maximum Java heap size we use the option -Xmx. to set the initial Java heap size -Xms.
  • 10.
  • 11.
  • 12.
    PermGen The JVM alsoallocate objects apart the heap, at Permgen. This area stores class related data from class definitions, structures, methods, field, and constants.
  • 13.
    PermGen Despite standing inhis name, the memory space from PermGen is also collected in a FullGC. IT can cause java.lang.OutOfMemoryError: PermGen space if it runs out if space. This happens normally when the exaggerated amount of classes that are loaded into memory.
  • 14.
    “PermGen is Dead.Long live PermGen” -JDK 8 HotSpot JVM
  • 15.
    Metaspace Metaspace is anew memory space - since java 8 - replacing Permgen. What makes it different of Permgen? Instead of allocate memory inside JVM, it allocates in the OS memory.
  • 16.
  • 17.
    Garbage Collector -a quick view on Heap (again) Eden Survivor Old Heap
  • 18.
    Garbage collector -Mark and sweep On the first cycle, this algorithm marks all accessible objects in all current threads. On the second cycle Sweeps all objects which were not marked in the last cycle. The Mark and Sweep is outdated, but it was a great learning to deal with non-referenced objects.
  • 19.
    Garbage collector -generational copying ● The idea of this algorithm is to divide objects into generations, young and old generation. ● The new generation is usually smaller than the old one (1/3 of the old). ● The GC scans the younger generation, which does not paralyze the JVM. ● Who "survives" is copied to the Old generation and the space of the Young Generation is made available again. ● The discard does not remove from the memory yet, it just marks the memory as available. ● Although it is costly to copy an object to the old generation to another part of the Heap, few survive, so the cost is low. ● Also, copying fewer objects is still "cheaper" than removing objects. ● When Young is crowded, a minor collect runs. At this point objects are sent to the old generation.
  • 20.
    Garbage collector -generational copying ● Depending on the JVM, large objects have the behavior of already being copied to the Old generation; ● Generational copying copies the objects to the next in a clustered fashion, optimizing memory and ensuring that there is no memory fragmentation. ● It is possible to increase the proportion of the space allocated to Young Generation by the option -XX: NewRatio = 1 (50%). By default, 2/3 will be old and 1/3 will be young; by changing the value to 1, we will have 1/2 for each area.
  • 21.
    Garbage collector -Garbage First (G1) Although it is considered a generational algorithm, it does not separates the heap in two generations, but in many small regions. Dynamically, the G1 chooses some regions to be collected (which gives the logical sense of those regions to be a young gen).
  • 22.
  • 23.
    JIT Compiler (Justin time) or slow is your granny JIT compiles Java code in Runtime to improve the application performance. But I said before that JVM read Bytecode and runs it in a VM, right? The first JVMs were the same, but with each version, the JVM is increased and brought this new feature to the VM.
  • 24.
    JIT Compiler (Justin time) JIT has the ability to do optimization according to the use of the program and the current conditions. The so-called hotspots. What JIT does behind the scene? It compiles for native platform and runs native code optimizing the application running.
  • 25.
    Thanks =) By JoãoSantana joaosantana.ti@gmail.com github.com/jonss