Garbage Collection in Java – You Need to Know

In Java, the meaning of garbage is unreferenced objects. So, garbage collection is the process of destroying these unused objects. In other words, it is the process that automatically reclaims the runtime unused memory.

To destroy the unused objects, the free() function is used in C language and the C++ uses the delete() function. But this function is carried by the Java itself so it is said that the memory management of Java is better than other languages.

Benefits of garbage collection

  • Since the garbage collector destroys the unused objects from the heap memory, it makes Java memory efficient.
  • No extra efforts are needed as the JVM automatically performs the garbage collection function.

How can an object be unreferenced?

There are many ways that can make an object unreferenced and a few of them are:

  • By nulling the reference
  • By assigning a reference to another
  • By anonymous object

The finalize() method

The finalize() method is called on before each object is garbage collected. It is used to perform clean up processing. The syntax of this method is:

  • protected void finalize(){}

The gc() method

The gc() method is used to call the garbage collector to carry out the cleanup processing. This method is present in the system and the runtime classes and its syntax is:

  • public static void gc(){}


public class GarbageCollectionDemo {

	int objId;

	GarbageCollectionDemo(int objId) {

		this.objId = objId;
		System.out.println("Created" + this.objId);
	public static void main(String[] args) {

		new GarbageCollectionDemo(1);
		new GarbageCollectionDemo(2);

		for (int i = 1; i <= 50000; i++) {


	protected void finalize() {

		System.out.println(this + "Finalized" + objId);


Java Runtime class

The Java runtime class interacts with the Java runtime environment. The class provides various methods that are used in executing a process, calling on the garbage collector, and getting total and free memory. A single instance of the java.lang.Runtime class is there for a single Java application.

The Runtime.getRuntime() method is used to return the singleton instance of the runtime class.

Important methods of Java Runtime class

  1. public static Runtime getRuntime():

This method is used to return the instance of the runtime class.

  1. public void exit(int status):

This method is used to destroy the current virtual machine.

  1. public void addShutdownHook(Thread hook):

This method is used to register a new hook thread.

  1. public Process exec(String command)throws IOException:

This method is used to execute a specific command in a separate process.

  1. public int availableProcessors():

This method is used to figure out the total number of processors that are available.

  1. public long freeMemory():

This method is used to figure out the amount of free memory in the JVM.

  1. public long totalMemory():

This method is used to figure out the total memory in the JVM.

How to shutdown the system in Java?

The shutdown -s command is used to shut down the system in Java. The -r switch is used to restart the system and the -t switch is used to specify the time delay.

Java synchronization

In Java, synchronization is the capability that controls the access of multiple threads to every shared resource.  When the programmer wants to allow only a single thread to access the shared resource, the Java synchronization is the best option to use.

Why is synchronization used?

The main uses of synchronization in Java are:

  • In order to prevent the interference of the thread
  • In order to prevent the problem of consistency

Type of synchronization:

The following are the two types of synchronization:

  • Process synchronization
  • Thread synchronization

Only thread synchronization will be dealt with in this chapter.

Thread synchronization

The following is the classification of the thread synchronization:

  1. Mutual Exclusive:
  2. Synchronized method
  3. Synchronized blocked
  4. Static synchronization
  5. Cooperation (Inter-thread communication)

Mutual Exclusive

The mutual exclusive is a type of thread synchronization that prevents the threads from interfering with each other while the data is being shared. There are three ways to achieve this in Java and they are:

  • By synchronized method
  • By synchronized block
  • By static synchronization

The concept of lock in Java

The lock is an internal entity in Java around which the synchronization is built. It is also known as a monitor and the lock is associated with all the objects. Through convention, a thread must first acquire an object’s lock before accessing the object’s field. After achieving consistent access to the object’s fields, the thread must then release the lock of the object.

The java.util.concurrent.locks package has several lock implementation added since the Java 5.

Java synchronized method

A synchronized method is a method that is declared as synchronized. It locks an object for any shared resource. A thread automatically acquires the lock for an object when it calls on a synchronized method and the thread then releases the lock after its job has been completed.

Synchronized block in Java

The synchronized block is used to perform synchronization on any particular resource of the method. The synchronized block can be used to synchronize only a few lines when there are many lines of code in the method. The program will work as same as the synchronized block when the programmer puts all the codes of the method inside the synchronized block.

Main points to remember for the synchronized block

  • The main function of the synchronized block is to lock an object for any shared resources.
  • The scope of the synchronized block is smaller than that of the method.

Static synchronization

If the programmer declared any static method as synchronized, then the lock will be on the class rather than on the object.

The problem without static synchronization

The static synchronization helps to remove the problem of interference between objects of a shared class.

Leave a Reply

Your email address will not be published. Required fields are marked *