Saturday, July 9, 2011

What is Garbage Collector in Java?

Garbage collector is an important component of the JVM. It's responsible to reclaim the memory from objects which are no longer in use. Thus reclaimed memory is made available for new objects.

Any object which is no longer in use is marked or made eligible for garbage collection.

The garbage collector component runs as a low priority demon thread inside the JVM.

A programmer may request the JVM to run garbage collector by calling System.gc() or Runtime.gc().

The garbage collector is automatically detects when JVM detects low memory.

During the process of garbage collection, the garbage collector component ensures that no object who's reference count is greater than zero is claimed.

There is no stack or queue order of garbage collection. i.e., the objects are garbage collected as per the memory location and convenience of the garbage collector.

Difference between JRE, Java SE and JDK

What is the difference between the JRE and the Java SE platform?

JRE
(Java Runtime Environment)
Java SE
(Java Platform, Standard Edition)
Who needs it? Computer users who run applets and applications written using Java technology Software developers who write applets and applications using Java technology
What is it? An environment required to run applets and applications written using the Java programming language A software development kit used to write applets and applications using the Java programming language
How do you get it? Distributed freely and is available from:
java.com
Distributed freely and is available from:
oracle.com/javase


What is the difference between the JRE and the JDK ?
JRE
(Java Runtime environment)
JDK
(Java Development Kit)
It is an implementation of the Java Virtual Machine* which actually executes Java programs. It is a bundle of software that you can use to develop Java based applications.
Java Runtime Environment is a plug-in needed for running java programs. Java Development Kit is needed for developing java applications.
The JRE is smaller than the JDK so it needs less Disk space. The JDK needs more Disk space as it contains the JRE along with various development tools.
The JRE can be downloaded/supported freely from
java.com
The JDK can be downloaded/supported freely from
oracle.com/technetwork/java/javase/downloads/
It includes the JVM , Core libraries and other additional components to run applications and applets written in Java. It includes the JRE, set of API classes, Java compiler, Webstart and additional files needed to write Java applets and applications. 

Java : JVM

A Java Virtual Machine (JVM) is a Virtual Machine capable of executing java byte code.

Creation and Loading

The Java virtual machine dynamically loads, links, and initializes classes and interfaces.
 
Loading is the process of finding the binary representation of a class or interface type with a particular name and creating a class or interface from that binary representation.

Loading: finding and importing the binary data for a type

There are two types of class loaders: user-defined class loaders and the bootstrap class loader supplied by the Java virtual machine. Every user-defined class loader is an instance of a subclass of the abstract class ClassLoader

Linking

Linking is the process of taking a class or interface and combining it into the runtime state of the Java virtual machine so that it can be executed.

Linking: performing verification, preparation, and (optionally) resolution
  1. Verification: ensuring the correctness of the imported type
  2. Preparation: allocating memory for class variables and initializing the memory to default values
  3. Resolution: transforming symbolic references from the type into direct references.

Initialization

Initialization: invoking Java code that initializes class variables to their proper starting values.  

Initialization of a class or interface consists of executing the class or interface initialization method <clinit>

A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class.

When the JVM is started, three class loaders are used
  1. Bootstrap class loader
  2. Extensions class loader
  3. System class loader

Default Class Loader Hierarchy




 Bootstrap
The bootstrap class loader loads the core Java libraries[5] (<JAVA_HOME>/lib directory). This class loader, which is part of the core JVM, is written in native code.

The extensions class loader loads the code in the extensions directories (<JAVA_HOME>/lib/ext or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

The system class loader loads code found on java.class.path, which maps to the system CLASSPATH variable. This is implemented by the sun.misc.Launcher$AppClassLoader class.

Explanation:

When a new JVM instance is started , the bootstrap class loader is responsible for loading key java classes like java.lang.Object and other runtime code into memory. The runtime classes are packaged inside jre/lib/rt.jar file. We cannot find the details of the bootstrap class loader in the java language specification, since this is a native implementation. For this reason the behavior of the bootstrap class loader will differ across JVMs.

next comes the Extensions (Loads jar files from JDK extensions directory (as defined in the java.ext.dirs system property – usually lib/ext directory of the JRE) and then System (Loads classes from system classpath (as defined by the java.class.path property, which is set by the CLASSPATH environment variable or –classpath or –cp command line options) ClassLoaders.

Class loaders are hierarchical and use a delegation model when loading a class. Class loaders request their parent to load the class first before attempting to load it themselves. When a class loader loads a class, the child class loaders in the hierarchy will never reload the class again. Hence uniqueness is maintained. Classes loaded by a child class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not true