Part 5
Interview questions on JVM for experienced covered in this post:
How Stack and Heap correlated to each other while creating
object?
-
JVM has bifurcated memory into two parts. The first one is
stack memory and the second one is heap memory.
-
Stack memory is mainly used for storing the order of method
execution and local variables.
-
Heap memory is mainly used for storing actual objects created
with a new keyword.
-
Stack and heap memory are correlated while managing the
objects life cycle. Once created object on heap memory will be referenced to
the reference variable which is stored on stack memory.
- Please refer to the below diagram.
What is the difference between Stack Memory and Heap Memory?
|
Stack Memory |
Heap Memory |
1. |
Stack memory is mainly used for storing the order of method
execution and local variables. |
Heap memory is mainly used for storing actual objects created with
a new keyword. |
2. |
Stack memory works with the LIFO mechanism. |
There is no mechanism to allocate and deallocate the memory in
heap. |
3. |
Stack variables are visible only to the owner thread. |
Objects are visible to all threads. |
4. |
We can increase stack memory size by using –XSS parameter. |
We can modify the size of heap memory by using Xms and –Xmx. |
5. |
If the stack gets full and still trying to add more elements in it
then it will throw StackOverFlowError. |
If JVM gets full then it will throw OutOfMemoryError. |
What the Strong reference, Weak reference, phantom reference and soft reference?
-
Strong Reference
- is a kind of reference, which makes the referenced object not eligible for
GC. builder classes. eg - StringBuilder
-
Weak Reference -
is a reference that is eligible for GC.
-
Soft Reference -
is a kind of reference whose object is eligible for GC until memory is
available. Best for image cache. It will hold them till the memory is
available.
-
Phantom Reference
- is a kind of reference whose object is directly eligible for GC. Used only
to know when an object is removed from memory.
What are the steps JVM needs to take while loading class in memory?
-
Load - The class
file is loaded to JVM memory.
-
Verify -
Verifies that the class follows the Java language and JVM
specifications.
-
Resolve - Change
symbolic references to direct references.
-
Initialize -
Initializes class variable and static variables.
What are the components of JVM or Explain Java Architecture?
-
Please refer to the below diagram first.
-
JVM’s prime responsibility is to convert byte code into
machine code.
-
JVM load the code into memory and verifies it and execute the
code and provides a run-time environment.
- Please refer to the below diagram for java architecture.
- Following are the details of JVM components.
1.
ClassLoader:
ClassLoader is used to load the classes in the main memory. JVMs primary
task is to load the classes into memory.
2.
Method Area: In
the method area class data is stored during the code’s execution. Class
method area holds the information of static variables, static methods,
static blocks and instance methods.
3.
Heap: Heap
memory is mainly used for storing actual objects created with a new keyword.
There is no mechanism to allocate and deallocate the memory in heap. We can
modify the size of heap memory by using Xms and –Xmx. If JVM gets full then
it will throw OutOfMemoryError.
4.
Stack: Stack
memory is mainly used for storing the order of method execution and local
variables. Stack memory works with the LIFO mechanism. We can increase stack
memory size by using –XSS parameter. If the stack gets full and still trying
to add more elements in it then it will throw StackOverFlowError.
5.
Native Stack:
This contains the data about native methods. The Methods which are written
other than java language are called Native methods.
6.
Execution Engine:
This is the main part of JVM. Its main task is to execute the byte code and
execute the java classes. There are three components in the Execution
engine.
§
Interpreter: The
interpreter sequentially executes the code. The interpreter interprets the
code continuously line by line.
§
JIT Compiler:
JIT stands for Just-In-time compiler. The performance of the JIT compiler is
superior to Interpreter. We can replace the JIT compiler with an
Interpreter.
§
Garbage Collector:
Garbage collector is used to removing the unreferenced objects and reclaim
the memory.
7.
Native Interface:
Native interface is a mediator between java methods and native
libraries.
- Java interview questions and answers all MNC - Click here
- Basic core java interview questions and answers for freshers - Click here
- Core java interview questions for 3 years experience - Click here
- Core java interview questions and answers for 3-5 years exp - Click here
- Core java interview questions and Answers for 5 - 7 Years exp - Click here
- Basic Java Interview Questions and Answers - Click here
- Java interview questions and answers on oops - Click here
- Java interview questions and answers on Strings - Click here
- Java interview questions on exception handling - Click here
- Interview questions on multithreading in java for experienced - Click here
- Interview questions on serialization in java for experienced - Click here
- Interview questions on inner class in java for experienced - Click here
- Interview questions on Collections in java for experienced - Click here
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions. My total experience is 10. Initially I had worked on some support projects and then I moved to java projects. I had worked with many multi-national companies like R-Systems, Tata Consultancy Services, Cybage Softwares. Fortunately, TCS and Cybage has given me an opportunity to take interviews for experienced candidates. I have conducted more than 1000 interviews by now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation. You can reach out to me at mncjavainterview@gmail.com. |
0 Comments