-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJVM Notes
More file actions
127 lines (88 loc) · 7.47 KB
/
JVM Notes
File metadata and controls
127 lines (88 loc) · 7.47 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
118
119
120
121
122
123
124
125
126
127
Complete Java Notes
-
Basic Java , JVM JRE and JDK topic /// for detail refer JAvaTpoint
-----------------------------
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java
---------------------------------------------------------------------------------------------------------------------------------------------
Secured
Java is best known for its security. With Java, we can develop virus-free systems. Java is secured because:
No explicit pointer
Java Programs run inside a virtual machine sandbox
Classloader: Classloader in Java is a part of the Java Runtime Environment (JRE) which is used to load Java classes into the Java Virtual Machine dynamically. It adds security by separating the package for the classes of the local file system from those that are imported from network sources.
Bytecode Verifier: It checks the code fragments for illegal code that can violate access rights to objects.
Security Manager: It determines what resources a class can access such as reading and writing to the local disk.
Java language provides these securities by default. Some security can also be provided by an application developer explicitly through SSL, JAAS, Cryptography, etc.
High-performance
Java is faster than other traditional interpreted programming languages because Java bytecode is "close" to native code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted language that is why it is slower than compiled languages, e.g., C, C++, etc.
---------------------------------------------------------
Q. difference between compiler and interpreter and assembler
A compiler is a software that converts programs written in a high level language into machine language. An interpreter is a software that translates a high level language program into machine language line by line while an assembler is a software that converts programs written in assembly language into machine language
What is meant by assembly language?
An assembly language is a type of low-level programming language that is intended to communicate directly with a computer's hardware. Unlike machine language, which consists of binary and hexadecimal characters, assembly languages are designed to be readable by humans.
eg ADd ... MATLAB , c
---------------------------------------------------------
Q.. why JVM is an abstract machine ?
VM = JAVA VIRTUAL MACHINE:- The word virtual itself indicates that it doesn't exists PHYSICALLY.
Elaborated here:
Abstract means HIDDEN.
When assembly programs runs on a computer, it is executed in the system runtime environment:
Properties
Platform dependent (if compiled for windows, a program will run only in windows not in Linux/UNIX etc.)
Not portable (same as above)
Systems runtime (in user's PC mainly under OS's control)
When Java Program runs into a computer, it is executed in another virtual machine (JVM) for which, the runtime environment is provided by JRE (JAVA Runtime Environment), it is installed automatically when you install JDK (Java Development Kit).
Without JRE, it is impossible to run Java Programs (Update: You can bundle your custom JRE with your code, in that case no need to install JDK or JRE separately but only in JDK9 & above)
This JVM itself runs in the system runtime (in user's PC/OS) but when Java program runs it is loaded into this running JVM.
For more practical AND visual experience:(for Windows only)
Open Task Manager
Go to Processes Tab
Find Java(TM) Platform SE Binary (This is JVM) - Java Instance
And now run a java program and write some code to delay the execution, like multi-threading with wait and notify (you can find such programs on google), due to this running program your Java Instance will show a little high memory and disk usage (not much high but slightly higher than that when no program was running in JRE/JVM). These processes you see in Task Manager, are running in System Runtime, and your Java program will not be listed there.
Instead it will be running inside this already running JRE.
This is the reason why JVM is ABSTRACT.
Now, do a little cross check and prove it..
Run 2-3 java programs, either keep them in longer waiting or just write Input Scanner and don't provide input, program will continue to run in blocking state so that we can see them later.
Once gain, confirm that only one instance of JVM/Java is running in task manager (Depends on how many JREs you have installed, sometimes IDEs can also create one instance, so better close it first for clear observation)
Now, where are those 2-3 Java Programs running?? Open VisualVM (it's under the same package/folder where your java executable resides)..
In this VisualVM, you can clearly see that, all your RUNNING Java Programs are listed.
Now, open side by side windows... Task Manager, VisualVM and one of your Running Code's Console.
Verify,
in Task manager - 1 instance of Java Binary.
in VisualVM - 3 different instances of your programs (if you run 3 java porgram)
Now, provide input in one of your code so that blocking state goes away and program terminates successfully.
Now verify,
in Task Manager - still 1 instance of Java Binary.
in VisualVM - 2 instances, because 1 code terminated/finished.
So, all your Java programs run under a Virtual Machine, a machine that is hidden, physically not available, abstract.
-------------------------------------------------------------------------
It is:
A specification where working of Java Virtual Machine is specified. But implementation provider is independent to choose the algorithm. Its implementation has been provided by Oracle and other companies.
An implementation Its implementation is known as JRE (Java Runtime Environment).
Runtime Instance Whenever you write java command on the command prompt to run the java class, an instance of JVM is created. insdie JRE
-----------------------------------------------------------------------------
Q. Changes in JVM from different versions
Java 7 Changes
Few things were moved out of PermGen MemorySpace
Symbols were moved to native memory
Interned String were moved to Java Heap
Class statics were also moved to Java Heap.
Above changes can also be reverted back in JDK 1.7 using JVM options.
Java 8 Changes
PermGen removed and replaced with MetaSpace.
Class and metadata moved to MetaSpace.
MetaSpace is not contiguous with Java Heap and allocated out of Native memory. This means it can use available memory from system upto max memory, until... read below
MetaSpace size max limit can be configured using JVM options.
G1 starts supporting concurrent unloading of Classes.
Code Cache is introduced. It stores compiled code by JIT compilor.
Compressed Class Space is also introduced.
Java 9 Changes
G1 is default garbage collector
Concurrent Mark Sweep(CMS) is planned for removal.
Some GC combinations for Young and Old Generation collections are also removed.
------------------------------------------------------------------------
Q . Why is Java slow?
The two main reasons behind the slowness of Java are
Dynamic Linking: Unlike C, linking is done at run-time, every time the program is run in Java.
Run-time Interpreter: The conversion of byte code into native machine code is done at run-time in Java which furthers slows down the speed
However, the latest version of Java has addressed the performance bottlenecks to a great extent.
https://www.guru99.com/java-virtual-machine-jvm.html --- VVIP go throug hthis link for interview