Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Java Runtime environment (JRE) provides automatic memory management and this is implemented at the programming language level. The programmer need not specify which objects to deallocate and return to the memory.

Memory Management takes care of allocation of portions of memory to store objects (or variables) requested by the program, and freeing the memory for reuse when these objects are no longer required.

Area in the memory where objects are stored is called Heap. The heap implementation support relocation of objects in memory thus enabling memory to be reorganized to improve efficiency without breaking references (it takes care of redirection of references).

Memory used by objects that will never be read or written again by the program is called garbage. 

Types of garbage:

1. Syntactic Garbage - it is clear from the code itself that an object leads to garbage.

2. Semantic Garbage - whether the memory used by an object is garbage or not can only be determined at runtime. 

Garbage collector (GC) is a background process which provides automatic memory management for Java environment by taking care of deallocation of computer memory resources for a program. 

Garbage collector performs basically two main tasks:

  1. Determine what objects of the program will never be accessed in future
  2. Deallocate the memory used by those objects

Benefits:

Garbage collection frees the programmer from manually performing memory allocation and deallocation in the program code. As a result, certain categories of bugs are eliminated or substantially reduced:

  • Dangling pointer bugs - a piece of memory is freed but there are still references to the object and one of those reference is used in program.
  • Double free bugs, the program attempts to free a piece of memory that have already been freed.
  • Memory leaks if a program does not free memory which is no longer referenced by any object, then this can lead to memory exhaustion over time.

Drawbacks:

Garbage collectors bring some runtime overhead which is out of control of the programmer, and can lead to performance problems for large applications that scales to large number of threads or processors or sockets, or consumes large amount of memory.

Therefore for large applications it becomes critical to choose the right garbage collector and to tune it to optimize the performance.