Click here to Skip to main content
14,335,232 members
Rate this:
Please Sign up or sign in to vote.
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
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.
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).
yesotaso 24-May-11 16:34pm
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.

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

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100