jaffa
jaffa

Reputation: 31

Garbage collection java

how does the garbage collector in java determine that objects are no longer referenced by the program?

Upvotes: 3

Views: 1502

Answers (4)

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46395

Check this out.

  1. Easy way to explain how GC works...
  2. How GC works ?

Reference :

Fig : General Collection of Object

General Collection


Fig : Memory Collection of Objects

Memory Collection

Upvotes: 3

Ayusman
Ayusman

Reputation: 8719

JVM maintains a map of all referenced objects. Every GC cycle (there are a number of GC methods in java, train, mark and sweep etc.) the entire list of object references are traversed (NOTE object references live in stack, data lives in heap) and all the object references that are no longer referenced are marked as ready to be garbage collected/are garbage collected.

This is a simplified way of understanding GC, most developers don't need to know the internals of the GC process though; but it's good to have some understanding.

Here are some links that might interest you:

http://chaoticjava.com/posts/how-does-garbage-collection-work/

http://java.sun.com/docs/hotspot/gc1.4.2/

http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html

http://www.oracle.com/technetwork/java/javase/tech/ts-3153-coomes-19899-dsf-150093.pdf#search=%22garbage%20collection%22

Hope this helps...

Upvotes: 1

Jessica
Jessica

Reputation: 6957

Here's a previous question on much the same subject: logic of Garbage collector in java

The link from there (which I now want to read for myself!) is: http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html

Upvotes: 1

Chris Thompson
Chris Thompson

Reputation: 35598

It depends on the VM but there are a number of ways it could be done.

Upvotes: 3

Related Questions