Saturday, April 8, 2017

Java Memory Management Interview Questions

In this post i am sharing memory management in java interview questions. These are frequently asked interview questions for java developers. Let us read what are those?

1) What is memory management in java?

Ans: Memory management is the process of allocating new objects and removing unused objects to make space for those new object allocations. However, the java developer does not need to explicitly allocate and deallocate memory. Since JVM has responsibility to handle management in java.

2)What are the different segments of memory?

Ans: There are 3 types of segments of memory. They are

  1. Stack
  2. Heap
  3. Code
stack segment: It contains local variables and reference variables
heap segment: It contains all created objects at runtime and instance variables
Code segment: This segment loads the java code that means where actual compiled java byte codes resides when loaded.

3) What is Garbage Collection?Advantages of it?

Ans: In ‘C++’ the programmer is responsible for both creation and destruction of objects but usually the programmer is giving very much importance for creation of objects and he is ignoring the destruction of objects.But in java programmer is responsible only for creation of objects but sun people has introduced one assistance which is running continuously in the background for destruction of objects. Due to this assistance there is no chance of failing the java program due to memory problem, this assistance is nothing but "Garbage Collector".

 The main advantage of Garbage Collection is it removes the burden of manual memory allocation/deallocation from us,so that we can focus on solving the problem

4) How Garbage Collector Works?

Ans: The Java runtime environment delete objects when it determines that they are no longer being used. This is nothing but Garbage collection.The JVM uses the Mark and Sweep Garbage collection model for performing Garbage collection of the whole heap.
                These consists of two phases they are mark phase and sweep phase. During the mark  phase all objects that are reachable from java threads,native handlers and other resources are marked as alive. This process identifies and marks all objects that are still used and the rest can be considered Garbage. During sweep phase the heap is traversed to find the gaps between the live objects. These gaps are recorded in free list and are made available for new object allocation.

5) What is class loader?different types of class loaders used by JVM?

Ans: Class loader is part of JVM which is used  to load classes and interfaces. There are 3 types of Class loaders used by JVM. They are

  • Bootstrap
  • Extension
  • System
Bootstrap class loader loads JDK internal classes,java*packages
Extension class loader loads jar files from JDK extensions directory
System class loader loads classes from system classpath

6) What is difference between static Vs Dynamic class loading?

Ans: In static class loading  classes are statically loaded with java's "new" operator whereas dynamic class loading is a technique for programatically invoking the functions of a class loader at run time.

7) What is OutOfMemoryError?

Ans: This Error is thrown that JVM has run out of memory and that the garbage collector is unable to claim any more free memory.

8)How an object becomes eligible for Garbage Collection?
Ans: An object is eligible for garbage collection when no object refers to it, An object also becomes eligible when its reference is set to null. The objects referred by method variables or local variables are eligible for garbage collection when they go out of scope. 

Example:
Integer i = new Integer(7);
i = null; 

8)What is permGen or Permanent Generation?

Ans: The memory pool containing all the reflective data of the Java Virtual Machine itself,such as class and method objects.The permanent generation contains meta data required by the JVM to describe classes and methods used in the application.The permanent generation is populated by the JVM at runtime based on the classes in use by the application. 

9) How subString() method of String class create memory leaks?

Ans: The String class of SubString() method would be create new String object keeping a reference to the whole char array,to avoid copying it. Therefore, you can inadvertently keep a reference to a very big character  array with just a one character String.

10) How Java provides high performance?

Ans: Java uses Just-In-Time compiler(JIT) to provide high performance. It is a program that convert byte code into instructions that can be sent directly to the processor.

Also Read:

What is Java Bean with examples?
MVC Architecture in Java
Hibernate Interview Questions
Spring Interview Questions
Struts Interview Questions
JDBC Interview Questions


No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...