-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJVM
More file actions
121 lines (92 loc) · 4.5 KB
/
Copy pathJVM
File metadata and controls
121 lines (92 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
JVM -
Java - Interpreted language.
.java source code compiled to create .class bytecode used in JVM.
JVM Components -
1. Class Loader
2. Runtime Memory/Data Area
3. Execution Engine
Class Loader
when we use class in our program, the class loader loads it into main memory.
the first class to be loaded into memory is the class with main()
three phase in class loading process
loading -
bootstrap class loader - root class loader
superclass of extension class loader and loads standard packages (java.lang,java.net,java.util,java.io)
extension class loader - subclass of bootstrap and super class of application class loader.
loads files in the $JAVA_HOME/jre/lib/ext (ODBC/JDBC connectors)
application class loader - loads the file on the class path, by default the class path is set to the current directory of the application.
linking
verification - checks structural correctness of class file, we get verify exception if this fails.
preparation - JVM allocates memory for the static fields of class or interface, it initializes them with default value.
Resolution - in this phase, symbolic references are replaced with direct references. this optional
initialization - involves executing the initialization method of the class or interface(known as <clinit>)
this include calling class's constructor, executing the static block and assigning values to all static variable.
this is final stage of class loading
Runtime Data Area
Method area - all the class level data such as the run-time constant pool, field and method data and the code for methods and constructor.
if the memory available in the method area is not sufficient for the program start-up JVM throws an outofmemoryerror
this method area is created on the virtual machine start-up and there is only one method area per JVM.
Heap Area - all the objects and their corresponding instance variables and arrays are stored here.
this is the run-time data area from which memory for all class instance and arrays is allocated.
this heap is created on the virtual machine start-up and there is only one heap per JVM.
Stack Area - all local variable and partial result stored here
for every method call one entry is made in the stack memory which is called stack frame
stack frame divided into three
Local variable
operand stack
Frame data
PC Register
JMV supports multiple threads
each threads has its own PC register to hold the address of the currently executing JVM instruction
once the instruction is executed the PC register is updated with next instruction.
Native Method Stack
JVM contains stack that support native methods, written in other lang such as C and C++
Every thread created have separate native method stack
Execution Engine
Interpreter - reads and executes the bytecode instruction line by line
due to this interpreter is comparatively slower
when method is called multiple times, every time a new interpretation is required.
JIT Compiler
execution engine first uses the interpreter to execute the bytecode but when it finds repeated code it uses the JIT compiler to compile entire bytecode and change it to native code. improves performance
has following components
intermediate code generator - generates intermediate code
code optimizer - take intermediate code and optimize
target code generator - takes optimized code and generate native machine code
profiler - hotspot detection
Garbage Collector
GC collects and removes unreferenced objects
it is a process of reclaiming the runtime unused memory automatically by destroying them
phased - Mark and Sweep identify and remove
done automatically by JVM and does not needed to handled separately
Java Native interface
act as a bridge for supporting packages for other programming languages such as C, C++
helpful when you need to write a code that not entirely supported by java
native method libs are libs written in other lang. present in the form of .ddl .so
GC
heap
objects
young gen
eden space (first time created object)
survivor space (referenced objects)
s0
s1
old gen
tenured (object alive from long time)
Process - marking
used
unused
normal deletion
compaction fragmentation
garbage collector in java
serial - execute GC process in serial to app threads
parallel - execute parallel GC thread to app threads
concurrent mark and sweep -
G1 -
key
stop the world - if GC is running it suspends app threads
minor GC - reclaiming memory from young gen
major GC - reclaiming memory from entire heap, survivor space, tenured
stack
variable, call stack, method args
metaspace PG
class metadata, static variables, constant pool, method metadata