Garbage Collection Optimization

Unnecessary calls to the garbage collector should not add much overhead than call to empty function. Moreover efficient allocation of memory also falls under this class of optimization.

Example:

In the code fragment below, the second and successive calls to System.gc() should not add much overhead than calling an empty function.

	System.gc() ; // First call to the garbage collector
	System.gc() ;
	System.gc() ;
	System.gc() ;
	System.gc() ; // Fifth call to the garbage collector


  

Below is the effect of the code fragment after Garbage Collection Optimization.

	System.gc() ; // First call to the garbage collector
	emptyFunction() ; 
	emptyFunction() ;
	emptyFunction() ;
	emptyFunction() ;
  

Notes:

Garbage Collection is the automatic recycling of heap memory that can be proven to be never used again. Garbage collection is done through a daemon garbage collection thread. One can even invoke the garbage collector explicitly through the System.gc() call.

While it is uncommon on the part of the programmers to place calls to System.gc(), such code may arise as a results of other optimizations.