Wednesday, December 31, 2014

Java String and String Constant pool

String is the very common class which every one is using frequently. Most of us aware that, String is an immutable object. As we already discussed that, for an each object memory will be allocated in heap memory area. But String objects are stored in a special memory area called "String Constant Pool" inside the heap memory area.

What is immutable object?

When state of an object can't be changed after the construction, those kind of objects are called immutable objects. For an example, all the wrapper classes are immutable objects.

String Constant Pool

All the String objects are stored in "String Constant Pool". As already discussed, "String Constant Pool" is a special memory area which is inside the heap memory. We will discuss how string objects are stored in "String Constant Pool".

When we create a new string object using string literal, that string literal is added into string pool, if the same string is not present already.

     String str = "Hello";

When we create an another string literal with same value, there will not be a new object for this. Instead of creating new object, reference of existing object will be return.

     String str2 = "Hello";

But if we try to modified the string object which is in String Constant pool, existing object will not be modified. Instead of modifying existing object, new object will be created and return the newly created string object's reference.

     str2 = str2.concat("world");

In above cases, we are creating String objects using String literal. But if we area creating String object using new keyword, that will be different. Because, all the string literals are stored in String Constant Pool and all the string literals are loaded into memory when class loading itself. But String objects which are created using new keyword will be stored in heap memory.

     String str = "Hello";
     String str2 = new String("Hello");

Tuesday, December 30, 2014

Java interface

Interface is a powerful concept of java. Interface is playing a vital role in many real time projects. Interface is used to design a loose coupling software system which can be interfaced with any other software systems. As we had discussion already that, multiple inheritance is achieved by interface concept.
There are certain rules for an interface,

  1. Interface does not contains any method implementation
  2. All the methods in interface are public and abstract
  3. Any field declared in interface are by default public, static and final
  4. If an interface is implemented by a class, that class should implements all the methods which are declared an interface
These are the important rule with respect to interface in java

interface example:
     public interface Father {
           int age = 50;
           public void maintaineFamily();
           public void earnMoney();
     }

In above interface "Father" having a field called "age". "age" is a int variable and by default it is a "public static final int age = 50". Here we have a question, why it should be by default "public static final". We will discuss the reason later.
All the methods are in above interface is an abstract methods. In interface we no need to mention the abstract key for methods. By default all the methods are abstract methods.

Need of an interface

Multiple inheritance is not supported by java because of "Diamond problem". This problem can be overcome by interface.
An object may need IS-A relationship with many types,

For example,
      Modi is a PrimeMinister
      Modi is a Citizen
      Modi is a Politician

So, here if we create class "Modi", then "Modi" class needs the properties of "PrimeMinister", "Citizen" and "Politician". But unfortunately we can extends only one class in java. But there is no restriction if that an interface.

Here we have one more question, anyway interface does not have any implementation. All the implementations are given in class only. Then why interface is required? We will discuss for this question below with real time example

Real time example

There are many Databases in the market, each Database is working on different platform, different language and different environment. Java can't develop a code to connect with each Database, because java doesn't know how that Database is being designed and functioning. But java is having the capability to connect with all the Databases without separate implementation for each Database, how? that is because of interface.

Let we take it up Oracle Database for an example,

We are using below interfaces for Database related operations,

     Connection
     Statement
     ResultSet

For a Database connection,
The fisrt step is load the Driver class:
Driver class is nothing but a java class which is developed by the Database vendor with implementation of "java.sql.Driver" interface. So as per interface rule, that vendor driver class must implements all the methods of "java.sql.Driver" interface.

Next step is to get the reference of "Connection" interface:
To get the reference of "Connection" interface, we are calling "DriverManager.getConnection(DB_URL,USER,PASS)". If you look at the "getConnection" method, that is invoking the "connect" method which is declared in "java.sql Driver" interface and implemented in Database driver class. This "connect" method is returning the Database vendor's concrete class which implements the "Connection" interface. Now we got the reference of "Connection" interface.

Next step is to get the reference of "Statement" interface:
When we are calling the "createStatement" method with the reference of "Connection" interface, it will the returns the Database vendor's concrete class which implements the "Statement" interface.

Next step is to get the reference of "ResultSet" interface:
When we are calling the "executeQuery" method with the reference of "Statement" interface, it will the returns the Database vendor's concrete class which implements the "ResultSet" interface.

If you see all the above steps, all the methods of "Connection", "Statement" and "ResultSet" interfaces are implemented by Database vendor. So we no need to worry about Database design or language used in the Database. So java has defined the interfaces for the Database globally, all the Database should implements these interfaces to work with java.

We had a one question earlier of this session, that is why all the fields in interface must be public static and final.
From this above example, you can come to know that, interface is the mutual agreement between the two software system. So the values which are defined in the interface should not be modified during the run time by the either of the system. Thats why all the fields in interface are by default public, static and final.

This is how interface concept works with respect to Database connection.

Saturday, December 27, 2014

Difference between final, finally and finalize

What is the difference between final, finally and finalize? is the most repeating question by the all interviewers. These three words are seems to be similar, that is the reason most of the interviewer shooting this question. As we had good idea about java concepts and we had a discussion about Garbage Collection mechanism also. So understanding the differences of these three words is not a big deal.

final

  1. final is a access modifier
  2. final is applicable for classes, methods and variables
  3. If a variable is declared as final, that variable becomes a constant. We can't perform re-assignment for that variable
  4. If a method is declared as final, we can't override that method in sub class
  5. If a class is declared as final, we can't extends that class any other classes. Which means, we can't create a sub class for that

finally

  1. finally is a block
  2. finally block always associated with try-catch block to perform clean up
  3. The purpose of the finally block is to de-allocate the resources. Suppose DataBase connection is created inside the try block. So DataBase connection should be closed once its usage is over. But there may an exception try block before closing the DataBase connection. But finally block guarantee that, irrespective of whether try block is executed completely or not, finally block will execute

finalize

  1. finalize is a method which is in Object class
  2. finalize method will be called by Garbage Collector just before destroy an object
  3. We know already that, when an object is ready for Garbage Collection. When there is no reference to an object, then that object is ready for Garbage Collection. But de-referenced object may hold some resources which needs to be de-allocated. So we can override the finalize method is our class, there we can write clean up code for an object

Java Coding Standards

Introduction

We every one knows that, how to write the code to meet the requirement. But apart from the requirement many things need to be considered during software development. One of the important thing is Coding Standard.

Why Java Coding Standards?

Software industry is not like other industries to complete project at certain period of time. Software development will never end. Today you can develop a software, but that software should be maintained through out the software's life time. So the code what you have developed should be understandable by other programmers. The ultimate aim of this Coding Standard is to improve the quality of the Coding and more readability

Naming Conventions

Naming Conventions are important to makes programmers more understandable.
Below table describe the Naming Conventions,


Identifier Type Rules of Naming Conventions Example
Package Package name should be written in lower case. Prefix of the package should be any one the top domain name, like com, org, net, edu, gov. Sub package names can be vary based on the organizations standards. com.accounts.bean
com.utility.form
com.payments.action
Classes Class name should be noun. If your class name contains more than word, first letter of each word should be capitalized. Keep class name simple and meaningful. class TransactionManager
class Dog
Interfaces Interface name should be capitalized like class name interface MessagingService
interface Printing
Methods Method name should be verb. First letter of method should be small case. In case of more than one word in a method, each internal word should be capitalized. run();
play();
getConnection();
Variables Variable name should start with small case, in case of more than one word in a variable, each internal word should be capitalized. Variable should not start with underscore(_) or dollar sign($). Variable name should be short and meaningful. Avoid declare the variable with one word. One word variable can be used for temporary purpose. int rollNumber;
double amount;
char c;
Constants Constant name should be all upper case. In case of more than one word, each word should be separated with underscore(_). static final int MIN_LENGTH = 10;
static final int MAX_WIDTH = 100;


File Organization

  1. Creating a class or interface on right package is an import thing. Suppose if you are creating a class for the purpose Utility Payments. So that class should be placed under payments package. You should not place that class in Reports package
  2. Creating separate source file for each class and interface is the best practise. If there is any private class or interface which is associated with public class, you can put them in single source file.
  3. All source files should begin with a c-style comment that lists the class name, version information, date, and copyright notice:

    /*
    * Classname
    *
    * Version information
    *
    * Date
    *
    * Copyright notice
    */

Indentation

Indentation also an important thing in Java Coding Standards. Tab space can be used as a unit of Indentation. Avoid lines longer than 80 characters.
Statement which is more than 80 characters, that can be break with some general rules.

  1. Break after comma
  2. Break before operator
  3. Align the new line with the beginning of the expression at the same level on the previous line
  4. If breaking a Statement is leading to confusing code, tab space can be used

Comments

Comments should give the overview of the code. Comments should contains only the relevant information of a program.
Java program having tow types of comments
  1. Implementation Comment
  2. Java Documentation Comment
Implementation Comment
  1. Implementation comments are delimited with /*...*/ and //.
  2. Implementation comments describe about the particular implementation.
Java Documentation Comment
  1. Java doc comments describe the java classes, interfaces, methods, constructors and fields
  2. Java doc comments are delimited with /**...*/
  3. Java doc comment should be written just before the declaration.

Declarations

  1. Declare one variable per line is recommended
  2. Try to initialize the local variable at the time of declaration itself. This is may not possible all the time based on the requirement
  3. Declaration should be in beginning of the block
  4. Declare all the necessary variables at beginning of the block itself, don't wait to declare the variables until its first usage. (for loop index variable declaration is an exceptional case)
  5. No space between a method name and the parenthesis "("
  6. Open brace "{" at end of the same line in declaration statement
  7. Closing brace "}" starts a line by itself indented to match its corresponding opening statement

White Space

Blank Lines

Blank lines improve the readability of the code, Blank Lines can be used below circumstance

  1. Two Blank lines can be used between the section of a source file
  2. Two Blank lines can be used between the class and interface declaration
  3. One Blank line can be used between the methods
  4. One Blank line can be used between local variables of method and its first statement
Blank Spaces

Blank Spaces can be used below circumstance

  1. A keyword followed by parenthesis should be separated by a space
  2. A blank space should be used after comma in argument list
  3. All binary operands except dot(.) should be separated from their operands by a space. Blank space should not separate the unary operators

Java Coding Standard tools

Below open source tools can be integrated with Net Beans IDE for Java Coding Standards
  1. Checkstyle java standard tool
  2. PMD java standard tool

Thursday, December 25, 2014

Collection Framework

Collection is a group of individual objects. If we want to represent a group of individual objects as a single entity, then we should go for Collection.

Collection framework defines several classes and interfaces which can be used to represent a group of individual object as a single entity.

Collection and Collection Framework terminology is related to java, but concept is old one which is already there in old languages.

Terminology in Java Equivalent Terminology in C++
Collection Container
Collection Framework STL (Standard Template Library)

9 Key interfaces of Collection Framework

Collection Framework contains both classes and interfaces, as interfaces will provide the much information compare than classes, so we will discuss 9 Key interfaces of Collection Framework.





1. Collection (I) (v1.2):
* Collection is used to represent a group of individual objects as a single entity
* Collection interface defines very common methods which are applicable for any Collection object
* In general, Collection interface is considered as a root interface of Collection Framework
* There is no concrete class which implements Collection interface directly

2. List (I) (v1.2):
* List is used to represent a group of individual objects as a single entity where duplicate objects are allowed and insertion order is preserved
* List is the child interface of Collection interface
* Implementation classes of List interface are,
  1. ArrayList (C) (v1.2)
  2. LinkedList (C) (v1.2)
  3. Vector (C) (v1.0)
  4. Stack (C) (v1.0)
* Here Vector and Stack classes are coming from older version of java, so these classes are called as "Legacy Classes".

3. Set (I) (v1.2):
* Set is used to represent a group of individual objects as a single entity where duplicate objects are not allowed and insertion order is not preserved
* Set is the child interface of Collection interface
* Implementation classes of Set interface are,
  1. HashSet (C) (v1.2)
  2. LinkedHashSet (C) (v1.4)
4. SortedSet (I) (v1.2):
* SortedSet is used to represent a group of individual objects as a single entity where duplicate objects are not allowed and all objects should be inserted in some sorting order
* SortedSet is the child interface of Set interface


5. NavigableSet (I) (v1.6):
* NavigableSet is the child interface of SortedSet interface
* It defines several methods for navigation purpose
* Implementation class of NavigableSet is,
  1. TreeSet (C) (v1.2)
6. Queue (I) (v1.5):
* Queue is used to represent a group of individual objects prior to processing
* Queue is the child interface of Collection interface
* Implementation classes of Queue are,
  1. PriorityQueue (C) (v1.5)
  2. BlockingQueue (C) (v1.5)
  3. LinkedBlockingQueue (C) (v1.5)
  4. PriorityBlockingQueue (C) (v1.5)
7. Map (I) (v1.2):
* Map is used to represent a group of objects as Key-Value pairs
* Duplicate Keys are not allowed
* Duplicate Values are allowed
* Both Key and Value are Objects
* Map is NOT a child interface of Collection
* Generally people are telling Collection is root interface of Collection Framework, but Map is NOT a child of Collection, So if we look at that strictly, we can not tell Collection is the root interface of Collection Framework
* Implementation classes of Map are,
  1. HashMap (C) (v1.2)
  2. LinkedHashMap (C) (v1.4)
  3. WeakHashMap (C) (v1.2)
  4. IdentityHashMap (C) (v1.4)
  5. Hashtable (C) (v1.0)
  6. Properties (C) (v1.0)
* Here Hashtable and Properties classes are coming from older version of java, so these classes are called as "Legacy Classes".

8. SortedMap (I) (v1.2):
* SortedMap is used to represent a group of objects as Key-Value pairs according to some sorting order of keys
* SortedMap is child interface of Map interface

9. NavigableMap (I) (v1.6):
* NavigableMap is child interface of SortedMap interface
* It defines several methods for navigation purpose
* Implementation class of NavigableMap is,
  1. TreeMap (C) (v1.2)

Wednesday, December 24, 2014

Need of Collection

Collection is the very hot topic in java. Every one would have experienced that, collection will play the important role in interview. Now we will discuss about need of collection.

If we want to represent a value in program, we can declare a variable
           int x = 10;
If we want to represent two values in program, we can declare two variables
           int x = 10;
           int y = 20;

If we want to represent three values in program, we can declare three variables
           int x = 10;
           int y = 20;
           int z = 30;

Suppose there is requirement to represent thousand values, declaring thousand variables, is not a good coding practise and readability of the code will be down.
So, to overcome the above said problem, we should go for an Arrays concept
          int[] s = new int[1000];
          s[0] = 10;
          s[1] = 20;
          s[2] = 30;
          .
          .
          .
          s[999] = 10000;

Advantage in Array is, we can represent group of individual object in a single variable,
but there are some disadvantages in Arrays,

  1. Arrays are Fixed in size:
    Once Array is declared with some size, there is no option to increase or decrease size at run time.
  2. Arrays can hold only Homogeneous data type elements:
    In above example, we have created an int Array 's', so variable 's' can hold only int values.
  3. Array concept is not implemented based on any data structure:
    We can not expect any predefined methods from Arrays, for every requirement we have to write the code.
To overcome these problem, we should go for collections,
  1. Collections are growable in nature:
    Collection size can be increased or decreased at run time
  2. Collections can hold both Homogeneous and Heterogeneous data type elements:
    Collection can hold any data type elements
  3. Collections are implemented based on some standard data structure:
    Collections are providing predefined method support

Tuesday, December 23, 2014

In Java, how does System.out.println() work?

This might be the very basic java class discussion or question. Many of us know that, "System" is a class, "out" is an Object and "println" is method. But there might be an question like, how its really works?
This question is an excellent example of how just some very basic knowledge of Java can lead you to the correct answer. Most interviewers would not expect you to know the answer to do this right away – but would like to see how you think and arrive at an answer. In Java, the dot operator can only be used to call methods and variables so we know that ‘out’ must be either a method or a variable. Now, how do we categorize ‘out’? Well, ‘out’ could not possibly be a method because of the fact that there are no parentheses – the ‘( )’ – after ‘out’, which means that out is clearly not a method that is being invoked. And, ‘out’ does not accept any arguments because only methods accept arguments – you will never see something like “System.out(2,3).println”. This means ‘out’ must be a variable.

What is “out” in System.out.println()?

We now know that ‘out’ is a variable, so we must now ask ourselves what kind of variable is it? Variable could be a static or an instance variable. Because ‘out’ is being called with the ‘System’ class name itself, and not an instance of a class (an object), then we know that ‘out’ must be a static variable, since only static variables can be called with just the class name itself. So now we know that ‘out’ is a static variable belonging to the System class.

Is “out” in System.out.println() an instance variable?

As we already knows ‘println()’ is a method, we can further classify the ‘out’ in System.out.println(). We have already reasoned that ‘out’ is a static variable belonging to the class System. But now we can see that ‘out’ must be an instance of a class, because it is invoking the method ‘println()’. So 'out' variable must be an instance variable.

When and where is the “out” instantiated in System.out.println?

When the JVM is initialized, the method initializeSystemClass() will initializes the System class and sets the out variable. The initializeSystemClass() method actually calls another method to set the out variable – this method is called setOut().

The final answer to how system.out.println() works

The more exact answer to the original question is this: inside the System class is the declaration of ‘out’ that looks like: ‘public static final PrintStream out’, and inside the Prinstream class is a declaration of ‘println()’ that has a method signature that looks like: ‘public void println()’.
Here is what the different pieces of System.out.println() actually look like:

     //the System class belongs to java.lang package
     class System {
           public static final PrintStream out;
           //...
     }

     //the Prinstream class belongs to java.io package
     class PrintStream{
          public void println();
          //...
     }

Saturday, December 20, 2014

Why multiple inheritance is not supported in java?

We all know that, multiple inheritance is not supported in java and same can be achieved by interface concept. But why multiple inheritance is not supported in java? and how same can be achieved by interface?
Before discussing about above said points, we remember one thing about Class and Interface

  1. In Class all the methods are concrete methods
  2. In Interface all the methods are by default abstract methods

Why multiple inheritance is not supported in java?

There are two classes A and B which are having a method with same name called methodTest. Class SubClass is extending both classes A and B, so absolutely compiler will throw an error. Suppose compiler is allowing to generate the byte code, then what will happen during an execution? Absolutely JVM will not knowing to which method needs to be executed either from Class A or from Class B. This problem is called "Diamond Problem".


     class A {
           public void methodTest() {
                System.out.println("Method from Class A");
           }
     }
     class B {
           public void methodTest() {
                System.out.println("Method from Class B");
           }
     }
     public class SubClass extends A,B {
           public static void main(String args[]) {
                SubClass obj = new SubClass();
                obj.methodTest(); // JVM will not knowing to which method needs to be executed either from Class A or from Class B
           }
     }

How multiple inheritance is achieved by interface?

In interface, all the methods are by default abstract methods which means only method declaration will be there, there is no implementation in interface. Whenever interface is implemented by a class, all methods which are declared in the interfaces should be implemented in the class.


     public interface A {
           public void methodTest();
     }

     public interface B {
           public void methodTest();
     }

     public class ImplementationClass implements A,B {
           public static void main(String args[]) {
                ImplementationClass obj = new ImplementationClass();
                obj.methodTest();
           }

           public void methodTest() {
                System.out.println("Method from ImplementationClass class");
           }
     }

In above example, interface A and B are having the same abstract method methodTest. ImplementationClass implements both the interfaces and methodTest method is implemented. So there is no conflict to calling the methodTest method, because we have only one implementation for methodTest method which is in ImplementationClass class.

Friday, December 19, 2014

Why non-static member can not be accessed from static block?

We have faced an issue when writing a program, like "compiler error: non-static variable cannot be referenced from a static context".


     public class TestProgram {
           int rollNumber;
           public static void main(String args[]) {
                rollNumber = 123; //compiler error: non-static variable count cannot be referenced from a static context
           }
     }

Now, let we discuss the reason for above said error
As we already know about Classes and Objects concepts, just remember below points
  1. When a variable is declared as static, memory and value of that variable will be assigned at the time of class loading itself and static value will be shared across the instances and static block can be executed without an object.
  2. When a variable is declared as non-static, memory and value of that variable will be assigned at the time of object creation only.

Now look at the above code, inside main method we are trying to assign a value for rollNumber non-static member of TestProgram class. Suppose compiler is allowing to generate a byte code for above code, what will happen during execution? JVM doesn't knowing that,in which object value needs to be assigned, because we were not giving any object reference. Thats why compiler is not allowing to access a non-static variable from static block.

Non-static members can be accessed by static block with object reference only, so that JVM will come to know in object data needs to be modified


     public class TestProgram {
           int rollNumber;
           public static void main(String args[]) {
                TestProgram obj = new TestProgram()
                obj.rollNumber = 123;
           }
     }

Above code will not thrown any error, because we have created an object for TestProgram class and through that object reference only we are referring its member.
I hope, this will help you to clear your doubt

Thursday, December 18, 2014

Serialization

Serialization Mechanism

Serialization is the mechanism which is used to represent the object as sequence of bytes that includes object's data, type of object and type of data stored in the object. Serialized object can be transferred over the network and serialized object can be stored in system as a file. Serialized objects can be read from the file and de-serialized that which means serialized objects can be loaded into JVM through de-serialization process.

ObjectOutputStream and ObjectInputStream classes are the high-level stream classes that contains the methods for serialization and de-serialization. ObjectOutputStream class contains write method for serialization as below

          public final void writeObject(Object x) throws IOException -->This method will serialize the object and it can be transferred over the network or it can be stored as a file.

ObjectInputStream class contains read method for de-serialization as below

          public final Object readObject() throws IOException, ClassNotFoundException -->This method will de-serialize the object and it returns the value as Object, the we have cast it to appropriate object type.

For successful serialization below two condition must be met,

  1. The class must implement the java.io.Serializable interface.
  2. All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.
If a class is implementing java.io.Serializable interface, it can be serialize, otherwise its not.

public class Employee implements java.io.Serializable {
      public String name;
      public String address;
      public transient int SSN;
      public int number;
      public void mailCheck() {
           System.out.println("Mailing a check to " + name + " " + address);
      }
}
In above example, Employee class is implementing java.io.Serializable interface, so we can able to serialize the Employee object, but in Employee class, SSN field is declared as transient, so SSN field value can't be serialized.


Serializing an Object

ObjectOutputStream class is being used to serializing an object. We will see in below example that, how Employee object can be serialized

import java.io.*;
public class SerializeObject {
      public static void main(String [] args) {
            Employee e = new Employee();
            e.name = "Kannan";
            e.address = "Chennai";
            e.SSN = 12345;
            e.number = 333;
            try {
                  FileOutputStream fileOut = new FileOutputStream("/tmp/employee.ser");
                  ObjectOutputStream out = new ObjectOutputStream(fileOut);
                  out.writeObject(e);
                  out.close();
                  fileOut.close();
                  System.out.printf("Serialized data is saved in /tmp/employee.ser");
            } catch(IOException i) {
                  i.printStackTrace();
            }
      }
}

When executing an above program Employee object will be serialized and stored as file in given directory.
Note: When serializing an object to a file, the standard convention in Java is to give the file a .ser extension.


De-Serializing an Object

We will de-serialize the Employee object which we have serialized already in above example. ObjectInputStream will be used to de-serialize on object

import java.io.*;
public class DeserializeObject {
      public static void main(String [] args) {
            Employee e = null;
            try {
                  FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
                  ObjectInputStream in = new ObjectInputStream(fileIn);
                  e = (Employee) in.readObject();
                  in.close();
                  fileIn.close();
            } catch(IOException i) {
                  i.printStackTrace();
                  return;
            } catch(ClassNotFoundException c) {
                  System.out.println("Employee class not found");
                  c.printStackTrace();
                  return;
            }
            System.out.println("Deserialized Employee...");
            System.out.println("Name: " + e.name);
            System.out.println("Address: " + e.address);
            System.out.println("SSN: " + e.SSN);
            System.out.println("Number: " + e.number);
      }
}

When we are executing above program, it will give the below result,
Deserialized Employee...
Name: Kannan
Address:Chennai
SSN: 0
Number:333

Here are following important points to be noted:

  1. The try/catch block tries to catch a ClassNotFoundException, which is declared by the readObject() method. For a JVM to be able to deserialize an object, it must be able to find the bytecode for the class. If the JVM can't find a class during the deserialization of an object, it throws a ClassNotFoundException.
  2. Notice that the return value of readObject() is cast to an Employee reference.
  3. The value of the SSN field was 11122333 when the object was serialized, but because the field is transient, this value was not sent to the output stream. The SSN field of the deserialized Employee object is 0.

Tuesday, December 16, 2014

Garbage Collection

What is Garbage Collection?

We already know what is Garbage Collection, one line answer for what is Garbage Collection; "Garbage Collection is the process to reclaim the memory of unreachable objects." One of the most important achievement in java is Garbage Collection with respective to memory management. It allows developers to create new objects without worrying explicitly about memory allocation and deallocation, because the garbage collector automatically reclaims memory for reuse. Most of the memory-management issues are resolved by Garbage Collection process. We will discuss about how Garbage Collection really works.


How Garbage Collection Really Works?

Many of us thinking that, Garbage Collection collects all unreachable objects and discards. But really Garbage Collection is doing opposite to our thought. Garbage Collection will track only live objects and rest all will be marked as garbage. As we many of us aware of various memory areas in JVM, one of the most important memory area with respect to developer is Heap area. This Heap area is used for dynamic memory allocation by JVM. In most configurations the operating system allocates the heap in advance to be managed by the JVM while the program is running. An every object creation some portion of memory will be allocated and moves the offset pointer forward.(Figure 1) Next allocation will start with this offset and claims next some portion of array. When an object is no longer referenced, garbage collection will reclaim the memory and reuse it for future objects.

Figure 1

Until this, we are clear that, every object created by developer will stored in heap area which is managed by JVM. As long as objects are referenced, JVM will consider those objects are alive. If objects are unreachable Garbage Collection will reclaim the memory. Here there will be a question araise, if Garbage Collection is tracking only live objects, then what is the root of the tree?


Garbage-Collection Roots (The root of the all the objects)

Every object will have one or more root objects. If application can reach the root object, then whole tree is reachable. But we have a question here, when root objects are reachable?. There are special objects called garbage-collection roots. (Figure 2) These garbage-collection root objects are always reachable.


There are four kinds of GC root objects

  1. Local variables
  2. Active Java threads
  3. Static variables
  4. JNI References
Figure 2

In simple java application have below GC roots

  1. Local variables in main method
  2. The main thread
  3. Static variables of the main class

Marking and Sweeping Away Garbage

JVM is running an algorithm internally to determine the unreachable objects. This algorithm is called "mark-and-sweep" algorithm. It is a straightforward and two step process
  1. The algorithm traverses all object references, starting with GC root and mark every object found as alive
  2. All of the heap memory which are all not marked as alive will be reclaimed by Garbage-Collection

Garbage Collection is intended to remove the cause of memory leaks. That is, if there is no reference to object, but object is not deleted. However its possible to have unused objects that still reachable by an application, because developer has forgot to dereference them (Figure 3). Such objects can not be garbage-collected.

Figure 3

Monday, December 15, 2014

JVM Architecture

JVM Architecture (Java Virtual Machine):

JVM is a platform-independent execution environment that converts Java byte code into machine language and executes it. Most of the programming languages source code is directly converts machine code that is designed to run on a specific microprocessor architecture or operating system, such as Windows or UNIX.


Various part of JVM architecture:
  1. Class loader sub-system
  2. Various memory areas
  3. Execution Engine

Class loader sub-system:

Class loader sub-system is the responsible to load the class file into memory Class loader sub-system is classified into three,

  1. Loading
  2. Linking
  3. Initialization
Loading:
There are three types of loaders are there
  1. Bootstrap class loader:
    Bootstrap class loader is the responsible to load the class from bootstrap class path (rt.jar) . rt.jar contains all code java API classes.
  2. Extension class loader:
    Extension class loader is the responsible to load the extension class, that is classes present inside jdk/jar/lib/ext folder
  3. Application class loader:
    Application class loader is the responsible to load the classes from application level class path
Bootstrap loaded is high priory loaded among all three loaders

Linking:
Liking process also classified into three,
  1. Verify:
  2. Verify is the process to verify the whether generated byte codes are proper or not, whether byte codes are generated by valid compiler or not. If verification is failed, verify error will be thrown.
  3. Prepare:
  4. In prepare process, memory will be allocated for static variables and assigned with default values.
  5. Resolve:
  6. In resolve process, all symbolic references are replaced with original memory area references
Initialization:
In initialization process, original values are assigned to static variables and static block will the executed

Various memory areas:

Memory areas are classified into five,
  1. Method area
  2. Heap memory
  3. Stack area
  4. PC Registers
  5. Native Method stacks
Method area:
In method area, class level data and static variable data will be present. Whenever variable is declared as static, then memory will be allocated in method area to store that static value.

Heap memory:
In heap memory, object and corresponding instance variables data will be present. Whenever creating an object, memory will be allocated in heap memory to store that object.

Stack area:
For every thread, there will a separate stack created, each entry in the stack is called stack frame Stack frame contains 3 parts,
  1. Local variable array:
  2. Local variable array will have the local variables and its values of a method
  3. Operand stack:
  4. Operand stack act as a workspace for an intermediate operations
  5. Frame data:
  6. All symbols which all are used in that method will be handled in Frame data
PC Registers:
For every thread, there will a separate PC Register created, PC Registers are the responsible to hold address of current executing instruction, once current executing instructions are completed, PC registers will updated automatically to next executing instruction

Native Method stacks:
For every thread, there will a separate Native method stack created Native Method stacks are the responsible to hold native method information

Execution Engine:

Execution Engine is the responsible to executes the program Execution Engine mainly classified as below,
  1. Interpreter
  2. JIT compiler
  3. Garbage collector
  4. Java Native Interface (JNI)
Interpreter:
interpreter is the responsible to read, interpret and execute the program line by line

JIT compiler:
JIT compiler is used to convert the byte code into machine code once and this machine code can be used further instead of interpreting every time So this can be used for most frequently using methods. There is one more component in JVM called Profiler. Profiler is the responsible to identify the most frequently using methods
JIT compiler again classified as below,
  1. Intermediate code generator:
  2. Intermediate code generator is the responsible to produce as intermediate code
  3. Code optimiser:
  4. Code optimiser is the responsible to optimise the code
  5. Target Code generator:
  6. Target Code generator is the responsible to generate machine code or Native code
Garbage Collector:
Garbage collector is the responsible to recognizing when allocated objects are no longer needed, de-allocating (freeing) the memory used by such objects, and making it available for subsequent allocations

Java Native Interface (JNI):
Java Native Interface is the responsible to provide the native method library information.