Why do we need garbage collection ?



Why do we need garbage collection ?

  • Java garbage collection is the method by which Java programs achieve automatic memory management.
  • Java programs compile to bytecode which will be run on a Java Virtual Machine.
  • Once java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program. Finally, some objects cannot be required.
  • The garbage collector finds these unused objects and deletes them to free up memory.

Read Also

How Java Garbage Collection Works

  • 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. Every JVM will implement garbage collection however it satisfies the only requirement is that it meets the JVM specification.
  • Although there are many JVMs, Oracle’s HotSpot is the most common. It offers a robust and complete set of garbage collection options.
  • While HotSpot has multiple garbage collectors that are optimized for various use cases, all its garbage collectors follow the same basic process.
    • In the first step, unreferenced objects are identified and marked as ready for garbage collection.
    • In the second step, marked objects are deleted.
    • Optionally, memory can be compacted after the garbage collector deletes objects, so remaining objects are in a contiguous block at the start of the heap. The compaction process makes it easier to allocate memory to new objects sequentially after the block of memory allocated to existing objects.
  • All of HotSpot’s garbage collectors implement a generational garbage collection strategy that categorizes objects by age. The rationale behind generational garbage collection is that most objects are short-lived and will be ready for garbage collection soon after creation.
 garbage

Garbage Collection

The heap is divided into three sections:

Young Generation

  • Newly created objects start in the Young Generation. The Young Generation is further subdivided into an Eden space, where all new objects start, and two Survivor spaces, where objects are moved from Eden after surviving one garbage collection cycle.
  • When objects are garbage collected from the Young Generation, it is a minor garbage collection event.

Old Generation:

  • Objects that are long-lived are ultimately moved from the Young Generation to the Old Generation. When objects are garbage collected from the Old Generation, it is a major garbage collection event.

Permanent Generation:

  • Metadata classes and methods are stored in the Permanent Generation. Classes that are no longer in use may be garbage collected from the Permanent Generation.

Needs of Java Garbage Collection

  • The benefit of Java garbage collection is that it automatically handles the deletion of unused objects or objects that are out of reach to free up memory resources.
  • Programmers working in languages without garbage collection (like C and C++) must implement manual memory management in their code.
  • Some programmers maintain in favor of manual memory management over garbage collection, primarily for reasons of control and performance.
  • While the discussion over memory management approaches continues to rage on, garbage collection is now a standard component of many popular programming languages.
  • For situations in which the garbage collector is negatively impacting performance, Java offers several options for tuning the garbage collector to improve its efficiency.


Related Searches to Why do we need garbage collection?