320 points
13 7 4

That exception + message means that JVM has decided that too much time is being expended on garbage collection ... relative to doing useful work. It normally means that the heap is nearly full, and the GC is running with increasing frequency in a vain attempt to keep going.

Depending on the situation, the possible solutions are:

  1. Get more physical memory so that increase the heap size.
  2. Find and fix any memory leaks.
  3. Make the problem smaller.

What is Garbage Collection in Java?
Garbage Collection is the process of reclaiming the runtime unused memory by destroying the unused objects.

In languages like C and C++, the programmer is responsible for both the creation and destruction of objects. Sometimes, the programmer may forget to destroy useless objects, and the memory allocated to them is not released. The used memory of the system keeps on growing and eventually there is no memory left in the system to allocate. Such applications suffer from "memory leaks".

After a certain point, sufficient memory is not available for creation of new objects, and the entire program terminates abnormally due to OutOfMemoryErrors.

You can use methods like free() in C, and delete() in C++ to perform Garbage Collection. In Java, garbage collection happens automatically during the lifetime of a program. This eliminates the need to de-allocate memory and therefore avoids memory leaks.

Java Garbage Collection is the process by which Java programs perform automatic memory management. Java programs compile into bytecode that can be run on a Java Virtual Machine (JVM).

When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program.

Over the lifetime of a Java application, new objects are created and released. Eventually, some objects are no longer needed. You can say that at any point in time, the heap memory consists of two types of objects:

  • Live - these objects are being used and referenced from somewhere else
  • Dead - these objects are no longer used or referenced from anywhere

How does Garbage Collection Work in Java?
Java garbage collection is an automatic process. The programmer does not need to explicitly mark objects to be deleted.

The garbage collection implementation lives in the JVM. Each JVM can implement its own version of garbage collection. However, it should meet the standard JVM specification of working with the objects present in the heap memory, marking or identifying the unreachable objects, and destroying them with compaction.

What are Garbage Collection Roots in Java?
Garbage collectors work on the concept of Garbage Collection Roots (GC Roots) to identify live and dead objects.

Examples of such Garbage Collection roots are:

  • Classes loaded by system class loader (not custom class loaders)
  • Live threads
  • Local variables and parameters of the currently executing methods
  • Local variables and parameters of JNI methods
  • Global JNI reference
  • Objects used as a monitor for synchronization
  • Objects held from garbage collection by JVM for its purposes

The garbage collector traverses the whole object graph in memory, starting from those Garbage Collection Roots and following references from the roots to other objects.

More Posts

What causes a Java ArrayIndexOutOfBoundsException and how can I prevent it? Alex Samson - Apr 6, 2020
How to Catch a few exceptions Hasnain_khan - Oct 13, 2020
The attraction of Test::Fatal in Perl mjg - Jun 13
Floating Point Exception (Core Dumped) C++ Lalit Kumar - Aug 15, 2020
exception in thread "main" java.lang.classnotfoundexception: sun.jdbc.odbc.jdbcodbcdriver SharadMagar450 - Sep 5
Java exception in thread "main" java.lang.nullpointerexception amna - Jul 13
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException Being human - Jun 4
Exception in thread main java.lang. nullpointerexception amna - May 18
Attempt to invoke virtual method '' on a null object reference at Cast.writetoParce Sabbir Hossain - Mar 31
Java lang long cannot be cast to java lang integer Hasnain_khan - Oct 13, 2020