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
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.
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.