LightBlog

Java Fundamentals – Interview Questions

Difference between interpreter and JIT compiler?
The interpreter interprets the bytecode line by line and executes it sequentially. It results in poor performance. JIT compiler add optimization to this process by analyzing the code in blocks and then prepare more optimized machine code.


Difference between JRE and JVM?
JVM is the specification for runtime environment which executes the Java applications. Hotspot JVM is such one implementation of the specification. It loads the class files and uses interpreter and JIT compiler to convert bytecode into machine code and execute it.


Difference Between JVM & HotSpot VM
JVM : is a Specification, HotSpot : is a implementation of JVM.
HotSpot is an implementation of the JVM concept, originally developed by Sun and now owned by Oracle. There are other implementations of the JVM specification, like JRockitIBM J9, among many others.


How does WeakHashMap work?
WeakHashMap operates like a normal HashMap but uses WeakReference for keys. Meaning if the key object does not devise any reference then both key/value mapping will become appropriate for garbage collection.


How do you locate memory usage from a Java program? 
You can use memory related methods from java.lang.Runtime class to get the free memory, total memory and maximum heap memory in Java.
public static Runtime getRuntime()
returns the instance of Runtime class.
public void exit(int status)
terminates the current virtual machine.
public void addShutdownHook(Thread hook)
registers new hook thread.
public Process exec(String command)
executes given command in a separate process.
public int availableProcessors()
returns no. of available processors.
public long freeMemory()
returns amount of free memory in JVM.
public long totalMemory()
returns amount of total memory in JVM.
public class TestApp {
        public static void main(String[] args) {
                Runtime r = Runtime.getRuntime();
                System.out.println(r.totalMemory()); //16252928
                System.out.println(r.freeMemory()); //15709576
                System.out.println(r.availableProcessors());//24
                r.gc();
        }
}


What is ClassLoader in Java?
When a Java program is converted into .class file by Java compiler which is collection of byte code. ClassLoader is responsible to load that class file from file system, network or any other location
·         Bootstrap ClassLoader - JRE/lib/rt.jar
·         Extension ClassLoader - JRE/lib/ext or any directory denoted by java.ext.dirs
·         Application ClassLoader - CLASSPATH environment variable, -classpath or -cp option, Class-Path attribute of Manifest inside JAR file.


Java heap memory
When a Java program started Java Virtual Machine gets some memory from Operating System.
whenever we create an object using new operator or by any another means the object is allocated memory from Heap and When object dies or garbage collected, memory goes back to Heap space.

How to increase heap size in Java
Default size of Heap space in Java is 128MB on most of 32 bit Sun's JVM but its highly varies from JVM to JVM. change size of heap space by using JVM options -Xms and -Xmx. Xms denotes starting size of Heap while -Xmx denotes maximum size of Heap in Java.

Java Heap and Garbage Collection
As we know objects are created inside heap memory and Garbage Collection is a process which removes dead objects from Java Heap space and returns memory back to Heap in Java.
 For the sake of Garbage collection Heap is divided into three main regions named as New Generation, Old Generation, and Perm space


·         New Generation of Java Heap is part of Java Heap memory where a newly created object is stored,
·         Old Generation During the course of application many objects created and died but those remain live they got moved to Old Generation by Java Garbage collector thread
·         Perm space of Java Heap is where JVM stores Metadata about classes and methods, String pool and Class level details.
·         Perm Gen stands for permanent generation which holds the meta-data information about the classes.
·         Suppose if you create a class name A, it's instance variable will be stored in heap memory and class A along with static classloaders will be stored in permanent generation.
·         Garbage collectors will find it difficult to clear or free the memory space stored in permanent generation memory. Hence it is always recommended to keep the permgen memory settings to the advisable limit.
·         JAVA8 has introduced the concept called meta-space generation, hence permgen is no longer needed when you use jdk 1.8 versions.

  • Garbage collection is performed by a daemon thread called Garbage Collector(GC). This thread calls the finalize() method before object is garbage collected.
  • The Garbage collector of JVM collects only those objects that are created by new keyword. So if you have created any object without new, you can use finalize method to perform cleanup processing (destroying remaining objects).
  • Neither finalization nor garbage collection is guaranteed.
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment