Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Here is a simple one for you guys (well not so simple for me!):

If an class is instantiated in a method within another class but that method does not return a reference, does the object die? In other words, is the object submitted for garbage collection at the close of the loop?

I appreciate the help, as always.
Posted

It would not really be submitted to garbage collection right at this time.
It would be viable for garbage collection whenever the collector runs the next time.
 
Share this answer
 
Comments
Isaiah83 24-May-11 13:30pm    
Let me further this question then for my understanding. What if another class (Foo), which is instantiated elsewhere holds a global (field) reference to this object I am referring to - the object that is viable for garbage collection? Would it still be up for termination? And thanks for the response.
Abhinav S 24-May-11 13:36pm    
AFAIK, No it wont.
Sergey Alexandrovich Kryukov 24-May-11 13:55pm    
This is correct, my 5. This is not so trivial but absolutely transparent to the developer.
I explained in more detail, please see.
--SA
Abhinav S 24-May-11 13:57pm    
Thank you SA.
Isaiah83 24-May-11 14:07pm    
So if in one class(Foo1) I instantiate an ArrayList (ArrayList myArray1 = new ArrayList();)
and in another class(Foo2) I maintain an ArrayList reference (ArrayList myArray1), The ArrayList object of Foo1 won't be eligible for GC, correct?

My big and final question here is: Should the reference that maintains the object of Foo1 should keep the same name (e.g. myArray1).
The idea of GC is this: not to allow the program to reference a destucted object, ever.

When all the object references accessible to the program are lost, this object is a subject to Garbage Collection. However, it does not happen immediately. It is "decided" by GC according to its own strategy. That's why the code cannot rely on the order of constructor calls. Even writing constructors should be quite rare.

The mechanism is more cunning that one may think. For example, the problem of "islands" is resolved. For example, object A references object B, object B references object C and object C references A. References are not lost, but if no code running in the given Application Domain can get reference to any of A, B or C, they are destructed and collected anyway.

—SA
 
Share this answer
 
Comments
Abhinav S 24-May-11 13:57pm    
My 5. The cyclic A,B,C issue is interesting.
Sergey Alexandrovich Kryukov 24-May-11 14:00pm    
Thank you, Abhinav.
--SA
Isaiah83 24-May-11 14:02pm    
Thank you, SA. Good to see you helping me out again. +5
Sergey Alexandrovich Kryukov 24-May-11 14:08pm    
My pleasure.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900