3. 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.
4. 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.
5. How does it work?
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!")
}
9. 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.
12. 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.
13. 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.
15. 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.
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).
23. 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.
24. 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.