Created
August 24, 2022 09:45
-
-
Save luke10x/f9ca7d152ac8e6e970cc9c185b388460 to your computer and use it in GitHub Desktop.
All about JVM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
======================================================================================================= | |
======================================================================================================= | |
All about java jvm | |
======================================================================================================= | |
======================================================================================================= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) Garbage Collector in java inbuild functionality and run automatically to free un-referenced objects from heap. | |
2) Those objects which are not referenced with any variable from stack and subsequent reference(stack -> Object1 -> Object2) are eligible | |
for garbage collections. | |
3) What garbage collection do, it scans all the heap memory and collect objects those are referenced with stack. And it not consider other | |
objects those are unreferened. | |
https://www.geeksforgeeks.org/mark-and-sweep-garbage-collection-algorithm/ | |
4) there is java.lang.gc() method tell jvm to start collecting unreachable objects. It just notify jvm that collect objects, jvm will do these | |
things by it's own. gc() will not gurantee garbage will be collected within this time period. | |
5) java.lang.Object.finalize() method just call before object will be collected by garbage collector. | |
Heap Memory - | |
Heap divides into | |
1) Eden(250%) - Whenever any new objects created they stored into Eden space, when eden gets full garbage gets collected and survival objects | |
passed to Survivors. | |
2) Survivors(s1, s2)(5%) - Whatever objects moved to survivors, are shifted each other like s1 to s2 vice versa whenever gc happens. If | |
it survives for 8th time then those objects moved to old gen. (Even though we get heap out of memory error these s1 and s2 are not actually | |
occupied for these survived thing) | |
3) Old gen (70%) - All old objects store here in old generation. When this get full then we received Out of memory issue. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Java is both compiled and interpreted programming language. Javac compiler just produces the bytecode, which is later validated, verified, and executed by the JVM. | |
(So this entire C/C++ architecture is totally different from JAVA architecture) | |
Java is a platform-independent programing language. Some points about JVM are | |
•The output of the java compiler is Bytecode. | |
•The Bytecode are executed by JVM. | |
•It is an interpreter that converts the bytecode to machine-specific instructions and executes. | |
•JVM is platform-specific | |
•Most of the modern programming languages are designed to be Compiled. | |
•Compilation is a one-time exercise and executes faster. | |
•It is very difficult to execute compiled code over the internet. | |
•Interpreting java bytecode facilitates its execution over a wide variety of platforms. | |
•Only JVM needs to be implemented for each platform. | |
•If a machine comprises JVM any java program can be executed on it. | |
•JVM differs from platform to platform and is platform-specific. | |
•Interpreted code runs much slower. | |
•However, bytecode enables us to run java programs much faster. | |
•JAVA supports on-the-fly execution of bytecode into native code. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First of all JDK contains JRE+development tools, JRE contains JVM+class libraries, where JVM contains (class loader & byte code verifier) | |
and the Execution engine (interpreter & JIT). | |
The compiler will convert the source code into intermediate byte codes. Where this byte codes is given to JVM for execution, | |
the class loader in the JVM will load the byte codes and does linking with class libraries provided by the JRE. | |
Then the code will be given to the execution engine in the JVM which interprets the unrepeated code and compiles the repeated code (for example loops) finally converting into machine code (object code). | |
Then it will give the machine code to the microprocessor for execution. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When we compile a .java file, .class files(contains byte-code) with the same class names present in .java file are generated by the | |
Java compiler. This .class file goes into various steps when we run it. These steps together describe the whole JVM. | |
This .class file goes in below steps | |
1) Class Loader - The Class loader reads the “.class” file. Class loaders are responsible for loading dynamically Java classes into the | |
data areas during runtime to the JVM. | |
There are three built-in class loaders available in Java. Bootstrap Class Loader, Extension Class Loader, Application Class Loader. | |
To get which class loader you can use below snippet - | |
User u = new User(); | |
Class<? extends User> a = u.getClass(); | |
System.out.println(a.getClassLoader()); | |
There are three phases in the class loading process: loading, linking, and initialization. | |
- Loading - The Class loader reads the “.class” file, generate the corresponding binary data and save it in the method area. For each “.class” file, | |
JVM stores the following information in the method area. | |
The fully qualified name of the loaded class and its immediate parent class. | |
Whether the “.class” file is related to Class or Interface or Enum. | |
Modifier, Variables and Method information etc. | |
- Linking- After a class is loaded into memory, it undergoes the linking process. Linking verifies the generated bytecode and it validates if the classname is correct or not. | |
if it is not correct then it will thorws verify error. Linking will allocates memory for class variable and default values. | |
Linking a class or interface involves combining the different elements and dependencies of the program together. | |
- Initialisation - In this phase, all static variables are assigned with their values defined in the code and static block(if any). | |
This is executed from top to bottom in a class and from parent to child in the class hierarchy. OR | |
Involves executing the initialisation method of the class or interface. This can include calling the class’s constructor, | |
executing the static block, and assigning values to all the static variables. This is the final stage of class loading. | |
2) Runtime Memory/Data Area - | |
Prior to java 8 - | |
- Method area: In the method area, all class level information like class name, immediate parent class name, methods and variables | |
information etc. are stored, including static variables. There is only one method area per JVM, and it is a shared resource. | |
- Heap area: Information of all objects is stored in the heap area. There is also one Heap Area per JVM. It is also a shared resource. | |
- Stack area: For every thread, JVM creates one run-time stack which is stored here. Every block of this stack is called activation record/stack frame which stores methods calls. All local variables of that method are stored in their corresponding frame. After a thread terminates, its run-time stack will be destroyed by JVM. It is not a shared resource. | |
- PC Registers: Store address of current execution instruction of a thread. Obviously, each thread has separate PC Registers. | |
- Native method stacks: For every thread, a separate native stack is created. It stores native method information | |
After java 8 - | |
Here is the runtime data storage for HotSpot VM In Java 8 | |
- Heap | |
Has got all your objects created using new, including String constant pool | |
Contains your fields/instance variables | |
- MetaSpace(Method Area) | |
Contains static data(Class variables and static methods) | |
Data in here is accessible by Heap, JVM stack | |
Unlike <=Java7 PermGen which takes JVM process memory which is limited and can't be expanded at runtime. MetaSpace uses native memory | |
-Stack area: For every thread, JVM creates one run-time stack which is stored here. | |
Every block of this stack is called activation record/stack frame which stores methods calls. | |
All local variables of that method are stored in their corresponding frame. | |
After a thread terminates, its run-time stack will be destroyed by JVM. It is not a shared resource. | |
-PC Registers: Store address of current execution instruction of a thread. Obviously, each thread has separate PC Registers. | |
Holds the JVM memory addresses(Not Native address) for each JVM instruction in your stack | |
Generally each entry in JVM/native stack refers to PC registers for addresses to get actual data from Heap/MetaSpace | |
Each stack is associated with a PC register | |
-Native method stacks: For every thread, a separate native stack is created. It stores native method information. | |
3) Execution Engine | |
Some good to read notes : | |
- PermmGen - Objects describing classes and methods are stored in the permanent generation and method area is subset of perm gen. | |
Where static/class variables stored - Prior java 8 static variables stored in method area/perm gen, From Java 8 onwards | |
- The static variables are stored in the Heap itself.From Java 8 onwards the PermGen Space have been removed and new space | |
named as MetaSpace is introduced which is not the part of Heap any more unlike the previous Permgen Space. Meta-Space is present on | |
the native memory (memory provided by the OS to a particular Application for its own usage) and it now only stores the class meta-data. | |
The interned strings and static variables are moved into the heap itself. | |
ref link - https://binarycoders.dev/2021/06/08/jvm-deep-dive/ | |
https://www.geeksforgeeks.org/jvm-works-jvm-architecture/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There is new keyword in Java used very often to create a new object. But what new does is allocate memory for the object of class you are making and returns a reference. That means, whenever you create an object as static or local, it gets stored in heap. | |
All the class variable primitive or object references (which is just a pointer to location where object is stored i.e. heap) are also stored in heap. | |
Classes loaded by ClassLoader and static variables and static object references are stored in a special location in heap which permanent generation. | |
Local primitive variables, local object references and method parameters are stored in stack. | |
Local Functions (methods) are stored in stack but static functions (methods) goes in permanent storage. | |
All the information related to a class like name of the class, object arrays associated with the class, internal objects used by JVM (like Java/Lang/Object) and optimization information goes into the Permanent Generation area. | |
To understand stack, heap, data you should read about Processes and Process Control Block in Operating Systems. | |
https://stackoverflow.com/questions/13624462/where-does-class-object-reference-variable-get-stored-in-java-in-heap-or-in-s#:~:text=That%20means%2C%20whenever%20you%20create,are%20also%20stored%20in%20heap. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) Permgen - Before java 8 permgen was used by java to store class related metadata and static objects as well as string pools. But problem | |
with permgen is that there is no garbage collector there to collect unused objects, hence whenever you redeploy your tomcat server(WAR servers) | |
old class related data will be not clear and new data will be generated. Eventually you will be out of memory whenever you will be redeploy application. | |
2) Metaspace = After java 8 java removed permgen and moved all static objects plus string pool to heap memory. And created new metaspace storage | |
which will use space from your operating system. All class related data will be store in metaspace and even when you redeploy application old | |
class related data will be clear from metaspace. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String pool = | |
- String pool is special storage present in heap memory, where all strings values stored. Which was created by below syntax. | |
String abc = "I am part of string pool"; | |
- So when we are creating other string variable with same value, it will again point to same string value in string pool. | |
String abc = "I am part of string pool"; | |
String bcd = "I am part of string pool"; | |
if(abc == bcd) { print(true)} | |
above code will print true, because bot string points to same object of string which present in string pool. | |
In other words, the string constant pool exists mainly to reduce memory usage and improve the re-use of existing instances in memory | |
String with new keyword = | |
When we create string with new String("") keyword, it creates String object in heap memory. Hence below if condition will failed. | |
String abc = new String("I am part of string pool"); | |
String bcd = new String("I am part of string pool"); | |
if(abc == bcd) { print(true)} | |
Above code will not print true, because object which created in heap for both strings are different. | |
Ref = https://www.geeksforgeeks.org/string-constant-pool-in-java/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Reference variable (pass by value) -https://www.google.com/amp/s/www.geeksforgeeks.org/reference-variable-in-java/amp/ | |
JVM Deep Dive - https://binarycoders.dev/2021/06/08/jvm-deep-dive/ | |
JVM - https://www.geeksforgeeks.org/jvm-works-jvm-architecture/ | |
JVM vs JRE - https://stackoverflow.com/questions/2812549/what-is-the-difference-between-the-jre-and-jvm/2812653 | |
Tuning JVM = | |
-Xmx used to set maximum heap size = -Xmx512m | |
-Xms used to set starting heap size = -Xms150m | |
-XX:MaxPermSize=256M used to set permsize(After java 8 no use) | |
-Xmn use to set young generation heap size = -Xmn180m | |
-verbose:gc use to print on console when garbage collection take place | |
-XX:HeapDumpOnOutOfMemory - Use this paramter if you want to get heap dump file when application gets crashed with oom error | |
-XX:+PrintCommandLineFlags - This use to print get all command line flags that java decied while running your application |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment