Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "garbage Collection" Watch "garbage Collection" New topic

garbage Collection

praveen parlapalli

Joined: Nov 01, 2007
Posts: 1
I have a doubt in Garbage Collection. What I understood about GC is "when an object is not reachable by any valid reference the object will be eligible for Garbage Collection" .My doubt is if I have a valid reference to an object and I dont use it will it ever be eligible for GC or will it be eligible after sometime .

Let's say I have mistakenly created an arraylist with "values" as reference variable(List values =new ArrayList() and I dont use in my system. When will "values" be eligible for Garbage collection
Henry Wong

Joined: Sep 28, 2004
Posts: 20531

An instance is GC'ed when it is no longer reachable from a "root". If an object is reachable from an array list that is, in turn, not reachable, then the object is not reachable. If this array list is in a class variable, that the classloader will not remove, then it will not be GC'ed. Even if your system doesn't use the variable.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Amir Iqbal
Ranch Hand

Joined: Jun 23, 2007
Posts: 97
well my friend.there is little bit change in your concept. brother, actually garbage collection is a very good feature of java language and by this feature the memory utilization can be well managed.
Now the main concept of GC is if you have created an object but that object do not have its refrence, than that object is eligible for GC. for example,
you have a clss name as MyClass. and instead of making its object like MyClass mc=new MyClass(); you created its object as:"new myclass();"
than that meanz the object created by the second way is eligible for GC because it doesnot have any refrence......
if you find something more abt it then please do tell me. i will appreciate it.

i am Java +ve Now !
I agree. Here's the link:
subject: garbage Collection
jQuery in Action, 3rd edition