Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theory and algorithm behind Java garbage collection

I read at many places, but did not find a place where I can learn about :

What is java garbage collection all about?

How is it implemented?

When and how is it called ?

What algorithms if follows in order to reclaim memory ??

In short everything about it :)

FIXED!!!

A very good article : http://www.artima.com/insidejvm/ed2/gcP.html

like image 834
daydreamer Avatar asked Nov 10 '10 04:11

daydreamer


People also ask

What is the algorithm used by garbage collector in Java?

The mark-and-sweep algorithm is called a tracing garbage collector because it traces out the entire collection of objects that are directly or indirectly accessible by the program. Example: A. All the objects have their marked bits set to false.

What is the concept of garbage collection?

Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java. A GC-enabled programming language includes one or more garbage collectors (GC engines) that automatically free up memory space that has been allocated to objects no longer needed by the program.

What causes garbage collection in Java?

Excessive garbage collection activity can occur due to a memory leak in the Java application. Insufficient memory allocation to the JVM can also result in increased garbage collection activity. And when excessive garbage collection activity happens, it often manifests as increased CPU usage of the JVM!


2 Answers

The very short version of answers are:

What is java garbage collection all about?

GC is a mechanism of memory management where the system (the JVM in this case) is responsible for automatically reclaiming memory that is no longer in use.

How is it implemented?

There are various ways to implement it. A simple description is that each piece of memory that is allocated is tracked. periodically the system checks the allocated pieces to see if any part of the program (the variables) can still reach the memory. Any memory that cannot be reached is reclaimed.

When and how is it called ?

This is also left up to the implementation. The only guarantee you have in Java is that before an OutOfMemoryError is thrown the system will attempt to reclaim memory. I would expect that most GC implementations also try to do a collection before they ask the underlying operating system for more memory. In general there will be a background thread that deals with running the collector.

What algorithms if follows in order to reclaim memory ??

There are several possible ones. Look at the articles others have posted as a starting point for that.

like image 50
TofuBeer Avatar answered Oct 23 '22 04:10

TofuBeer


The Wikipedia entry for garbage collection covers all your questions:

http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)

like image 23
Dan Grossman Avatar answered Oct 23 '22 02:10

Dan Grossman