Thursday 13 February 2014

Java Memory management

Memory management is the process of allocating memory/space to newly created objects and recognizing the objects which are no longer needed and freeing memory used by such objects and making it available for subsequent allocation.

Explicit memory management :

In some programming languages like C/ C++, memory management is the programmer's responsibility. For example in C, malloc() function is used to allocate memory and free() function is used to free the memory. This memory management is very complex task and may lead to many common errors that can cause unexpected or erroneous program behavior and crashes. As a result, a large proportion of developer time is often spent debugging and trying to correct such errors.

Below are two major problems which could occur due to explicit memory management :--

1.  Dangling References
2.  Space Leaks

Dangling References :
Dangling reference is the scenario when space is freed from an object to which some other object still has reference. If the object that has a reference tries to access the original object (for which memory is deallocated programmatically) , but the space has been allocated to new object, may lead to undesired behavior.

Space Leaks:
Space leaks occurs when memory is allocated but object is no longer needed and memory is not released.

Automatic memory management:

To deal with problems which are mentioned above,  modern day programming languages uses automatic memory management through garbage collector. Garbage collection avoids the dangling reference problem, because an object that is still referenced somewhere will never be garbage collected and so will not be considered free. Garbage collection also solves the space leak problem described above since it automatically frees all memory no longer referenced.

A garbage collector is responsible for following tasks :--

1. Allocating memory
2. Ensuring that any referenced object remains in memory
3. Recovering memory used by objects that are no longer reachable from references in executing code.

In Java, JVM divides memory in to 5 parts including Heap. The heap is created when JVM starts up and it may increase or decrease while application execution. Objects are created on heap and when heap is full, garbage is collected.

Note :--  Whenever we talk about JVM, what comes to our mind is that there is only one vendor (supplier) of JVM and that is the Oracle HotSpot JVM (formerly Sun JVM), but that is not the case. apart from Sun there are other vendors of JVM like IBM, Oracle etc.  In fact, the two most popular application servers, IBM WebSphere and Oracle WebLogic, each come with their own JVM.  There are 3 most prominent JVMs, Oracle HotSpot JVM (formerly Sun JVM), he IBM WebSphere JVM, and the Oracle JRockit JVM. 

In Oracle JRockit JVM, Heap is divided in to two areas/generations called the nursery (or young space) and the old space. The nursery is a part of the heap reserved for allocation of new objects. When nursery becomes full, garbage is collected by a process called Young collection which moves objects which have lived long enough in young memory to old space of heap, freeing space for new allocations. When old space becomes full, garbage is collected by a process called Old collection.  
 
In Oracle HotSpot JVM (formerly Sun JVM), the Heap is is organized into three generations: a young generation, an old generation, and a permanent generation. In permanent generation space, holds objects that the JVM finds convenient to have the garbage collector manage, such as objects describing classes and methods,as well as the classes and methods themselves.

The reason behind keeping nursery is that while program execution most objects are new and short lived and thus Young generation collections occur relatively frequently and are efficient and fast because the young generation space is usually small.

There are various types of garbage collection algorithms:--
  
1. Mark and sweep garbage collection algorithm
2. Generational garbage collection algorithm.
3. Compaction garbage collection algorithm

Mark and Sweep garbage collection mechanism:--

A mark and sweep garbage collection consists of two phases, the mark phase and the sweep phase. During the mark phase all objects that are reachable (reachable though references from Java threads, Native methods etc) are marked as alive and remaining objects can be considered as garbage.

During Sweep phase, all identified garbage is reclaimed.

Oracle's JRockit JVM uses two improved versions of mark and sweep garbage collection mechanism:--
1. Mostly Concurrent Mark and Sweep
2. Parallel Mark and Sweep

  
Generational garbage collection mechanism:--

Generational garbage collection mechanism is used when heap memory is divided in to different areas /generations. Different algorithms can be used to perform garbage collection in the different generations, each algorithm optimized based on commonly observed characteristics for that particular generation. 

As like Oracle's JRockit JVM divides heap in to two areas, called Young space (nursery) and Old space. when young space (nursery) becomes full, Young collection process moves objects which have lived long enough in young memory to old space of heap, freeing space for new allocations.


Compaction garbage collection mechanism:--
 
In memory, Objects that are allocated next to each other will not necessarily become unreachable and garbage collected at the same time. Some objects are garbage collected at one time, others are garbage collected later on, thus heap memory becomes fragmented after garbage collection.This fragmented heap memory after garbage collection have many empty space which is scattered may not even sufficient for allocation to large objects. 

Compaction is the mechanism moving all the live objects together and completely reclaiming the remaining memory, thus removing fragmentation from heap. 
. To reduce fragmentation, the Oracle' JRockit JVM compacts a part of the heap at every garbage collection (old collection).
   
The above garbage collection mechanism can be used together for better performance in  a particular JVM implementation.


No comments:

Post a Comment