Date Archives August 8, 2016

Garbage Collection


How Garbage Collection Really works

All objects are allocated on the heap area managed by the JVM. Every item that the developer uses is treated this way, including class objects, static variables, and even the code itself. As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory.


Garbage Collector root

  • Local variables in the main method
  • The main thread
  • Static variables of the main class


Mark and Sweep Algorithm

  • The algorithm traverses all object references, starting with the GC roots, and marks every object found as alive.
  • All of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free, essentially swept free of unused objects.
  • When objects are no longer referenced directly or indirectly by a GC root, they will be removed. There are no classic memory leaks.



Java Memory Structure

types of memory area


Static memory (Method area)

  • Method information and Field information.
  • Global constants and other data generated by the compiler(e.g. info to support garbage collection) are allocated static storage.
  • Static variables are bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. E.g., static variables.


  • Names local to a procedure and Parameters are allocated space on a stack. The size of stack can not be determined at compile time.


  • Data that may outlive the call to the procedure that created it is usually allocated on a heap. E.g. new to create objects that may be passed from procedure to procedure.
  • The size of heap can not be determined at compile time. Referenced only through pointers or references, e.g., dynamic objects in C++, all objects in Java
  • Garbage collection cleans up data(not using) on heap.



class Point {
   int x, y;

   void move(int x, int y) {
      this.x = x;
      this.y = y;

   int getX() {
      return x;

   int getY() {
      return y;

   void setX(int x) {
      this.x = x;

   void setY(int y) {
      this.y = y;

   void draw() {
      System.out.println("Point(" + x + ", " + y + ")");

class PointTest {
   public static void main(String args[]) {
      Point p1, p2 = null;

      p1 = new Point();
      p1.move(4, 4);
      // Point 1
      p2 = new Point();
      p2.move(2, 4);

      // Point 2

[ PointTest class loaded ]



[ JVM calls Main method ]



[ At Point 1 in Main method ]



 [ At Point 2 in Main method ]



Java features



  • Java is Easy to write and more readable and eye catching.
  • Java has a concise, cohesive set of features that makes it easy to learn and use.
  • Most of the concepts are drew from C++ this making Java learning simpler.


  • Java program cannot harm other system thus making it secure.
  • Java provides a secure means of creating Internet applications.
  • Java provides secure way to access web applications.


  • Java programs can execute in any environment for which there is a Java run-time-system.(JVM)
  • Java programs can be run on any platform (Linux, Window, Mac)
  • Java programs can be transferred over world wide web (e.g applets)


  • Java programming is pure object-oriented programming language. (while C++ is semi object oriented)
  • Like C++, java provides most of the object oriented features.


  • Java encourages error-free programming by being strictly typed and performing run-time check.


  • Java provides integrated support for multithreaded programming.


  • Java is not tied to a specific machine or operating system architecture.
  • Machine independent i.e Java is independent of hardware.


  • Java supports cross-platform code through the use of Java bytecode.
  • Bytecode can be interpreted on any platform by JVM.

High Performance

  • Bytecodes are highly optimized.
  • JVM can be executed them much faster.


  • Java was designed with the distributed environment.
  • Java can be transmit, run over internet.


  • Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve access to objects at run time.