All About Java Garbage Collection : Types, Algorithms, Advantages and Disadvantages

What is Garbage Collection?
Garbage Collection in computer science is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program. Garbage collection does not traditionally manage limited resources other than memory that typical programs use, such as network sockets, database handles, user interaction windows, and file and device descriptors.

Garbage collection was invented by John McCarthy around 1959 to solve problems in Lisp.

Basic principle of Garbage Collection:

The basic principles of garbage collection are:

  • Find data objects in a program that cannot be accessed in the future
  • Reclaim the resources used by those objects

It is kind of interesting to know how the objects without reference are found. Java normally finds all the objects that have reference and then regards rest of the objects are reference less – which is in fact a very smart way of finding the unreferenced java objects.

Types of Java Garbage Collectors/Garbage Collection Algorithms:
On J2SE 5.0 and above, one can normally find the following types of Java Garbage collectors that the programmers can normally choose to do a garbage collection through JVM Parameters.

The Serial Collector:

  • JVM Option Parameter: -XX:+UseSerialGC

The Throughput Collector or The Parallel Collector:

  • JVM Option Parameter: -XX:+UseParallelGC
  • Young Generation GC done in parallel threads
  • Tenured Generation GC done in serial threads.

Parallel Old Generation Collector:

  • JVM Option Parameter: -XX:+UseParallelOldGC
  • Certain phases of an ‘Old Generation’ collection can be performed in parallel, speeding up a old generation collection.

The Concurrent Low Pause Collector:

  • JVM Option Parameter -Xincgc or -XX:+UseConcMarkSweepGC
  • The concurrent collector is used to collect the tenured generation and does most of the collection concurrently with the execution of the application. The application is paused for short periods during the collection.
  • A parallel version of the young generation copying collector is used with the concurrent collector.
  • The concurrent low pause collector is used if the option -XX:+UseConcMarkSweepGC is passed on the command line.
  • -XX:+UseConcMarkSweepGC -XX:+UseParNewGC
    • Selects the Concurrent Mark Sweep collector.
    • This collector may deliver better response time properties for the application (i.e., low application pause time).
    • It is a parallel and mostly-concurrent collector and and can be a good match for the threading ability of an large multi-processor systems.

The incremental (sometimes called train) low pause collector:
JVM Option Parameter: -XX:+UseTrainGC
This collector has not changed since the J2SE Platform version 1.4.2 and is currently not under active development.
It will not be supported in future releases.

Note that -XX:+UseParallelGC should not be used with -XX:+UseConcMarkSweepGC .

The argument parsing in the J2SE Platform starting with version 1.4.2 should only allow legal combination of command line options for garbage collectors, but earlier releases may not detect all illegal combination and the results for illegal combination are unpredictable.

Benefits of Garbage Collection:
Garbage collection frees the programmer from manually dealing with memory deallocation. As a result, certain categories of bugs are eliminated or substantially reduced:

  • Dangling pointer bugs, which occur when a piece of memory is freed while there are still pointers to it, and one of those pointers is then used. By then the memory may have been re-assigned to another use, with unpredictable results.
  • Double free bugs, which occur when the program tries to free a region of memory that has already been freed, and perhaps already been allocated again.
  • Certain kinds of memory leaks, in which a program fails to free memory occupied by objects that will not be used again, leading, over time, to memory exhaustion.

Disadvantages of Garbage Collection:

  • Consumes computing resources in deciding what memory is to be freed, reconstructing facts that may have been known to the programmer often leading to decreased or uneven performance.
  • Interaction with memory hierarchy effects can make this overhead intolerable in circumstances that are hard to predict or to detect in routine testing.
  • The moment when the garbage is actually collected can be unpredictable, resulting in stalls scattered throughout a session.
  • Memory may leak despite the presence of a garbage collector, if references to unused objects are not themselves manually disposed of. This is described as a logical memory leak.The belief that garbage collection eliminates all leaks leads many programmers not to guard against creating such leaks.
  • In virtual memory environments, it can be difficult for the garbage collector to notice when collection is needed, resulting in large amounts of accumulated garbage, a long, disruptive collection phase, and other programs’ data swapped out.
  • Garbage collectors often exhibit poor locality (interacting badly with cache and virtual memory systems), occupy more address space than the program actually uses at any one time, and touch otherwise idle pages.
  • Garbage collectors may cause thrashing, in which a program spends more time copying data between various grades of storage than performing useful work.


Java Static Import Explained With Easy Example With Source Code

What is Static Import?

Static Import is a feature in Java that allows you to access the methods and variables that are defined as public static to be used in a class without having to use the class name.

Let’s say there is a following class with a static field and a static method.

package com.sample;
public Class MyClassOne() {
	public static int a_number = 3;
	public static void doSomething(int number) {
		System.out.println("Did Something " + number);

Now let’s create another class that use variable and method from this class. I am not suggesting you should make your variable public – this is for demonstration purpose only. A good programming practice is to declare your variables private and expose them via getters and setters method as necessary.

Regular Way (Non Static Import)

package com.sampletwo;

import com.sample.MyClassOne;

public Class MyClassTwo() {
	public static void doSomethingTwo() {

With Static Import, the code becomes

package com.sampletwo;

import com.sample.MyClassOne.*;

public Class MyClassTwoStatic() {
	public static void doSomethingTwo() {

Note: Do not statically import methods with a name that is identical to a method in Object, because methods that are naturally in scope take precedence over static imports

SQL Example To Create Table On MySql DB

This is a very basic script that creates a table in the database. This has been tested to work with mysql, but similar format can be applied to other databases such as MS SQL Server, Oracle, IBM DB2 etc.

Here is the database schema that we want to create:

The code to create this table is: