Reputation: 31
how does the garbage collector in java determine that objects are no longer referenced by the program?
Upvotes: 3
Views: 1502
Reputation: 46395
Check this out.
Reference :
Fig : General Collection of Object
Fig : Memory Collection of Objects
Upvotes: 3
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
Hope this helps...
Upvotes: 1
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
Reputation: 35598
It depends on the VM but there are a number of ways it could be done.
Upvotes: 3