3

Just have a few questions in my mind...

  1. Is it a good practice to create an object without reference like the one below. If so, why?

    LoadIt(new myClass()); where LoadIt is some method

  2. What happens to the object created above. Will that be garbage collected. If so when? ie, is its scope same as other objects

  3. Is there any chance of referring the same object again?

6 Answers 6

3

The scope is decided by the Method (here LoadIt)...

If the Load it methods stores its myClass parameter in a global variable, then until the global variable goes out of scope, the Object will stay in the memory... ie. It will not be garbage collected as the global variable is still referencing to it...

Objects are generally stored in heap and can be referrenced by many variables that are in Stack... Here you dont want to hold the reference in the stack of your method... But it is referenced in the stack of LoadIt method by its parameter... Hence the object will not be garbage collected until the Load it method's parameter goes out of scope... In the mean time the LoadIt method can try to reference it again in a global variable or pass it on as a parameter to another method... On a whole, only when all references for a object in stack (or in other objects) goes out of scope, the object is garbage collected...

Getting back the reference to this object, purely depends on what the Load it method does with this object... If the method does nothing other than referencing it with the parameter variable, then you cant get it back... But if the method copies reference to other variable which is available public, then you can use that variable and get the reference back.

Sign up to request clarification or add additional context in comments.

6 Comments

It's not an anonymous object! new { propOne = "hello" }; is.
Yes, the word anonymous is not right... I just used it for I dont know a better word...
There isn't a word for it because the concept doesn't really exist they way you describe it in the CLR. Either objects are reachable - that is, a reference is rooted - or it isn't. If an object doesn't have any reachable references, it is called "unreachable" and is thus a candidate for collection.
That comment was really useful. By the way, can you suggest any book on 'reachable and unreachable references' stuffs
@Veer - I'd go for "CLR via C#" by Jeffery Richter. It looks like the 3rd edition, which covers .Net 4.0 is out: amazon.com/CLR-via-C-3rd-Pro-Developer/dp/0735627045
|
2

If LoadIt was defined as

public MyClass LoadIt(MyClass myClass)
{
    ... do somthing
    return myClass;
}

You can reference it again.

Comments

2

2) By default there is no way to know when this (or any) object will be garbage collected as it is a nondeterministic process.

3) Absolutely. It all depends on what happens in the LoadIt method.

1 Comment

#2 is a bit misleading - there are ways to know, and it can be deterministic, but these things need to be configured specifically. The default behavior is that the GC is non-deterministic.
1

To make sure that you get a complete answer for #2, understand that you do have a reference to that object. You've created it and passed the reference to the method, which, in the method, is referred to as a parameter. That is a valid reference to the object, and it won't be collected as long as the method has an outstanding code-path to that parameter, since the reference is considered reachable (it can be traced to via some graph of objects starting at one of the 5 GC roots, in this case, probably the stack or a CPU register).

Comments

1
  1. It doesn't matter unless (and you aluded to this in #3) you need to reference the same instance of MyClass later.

  2. It has the same scope as if you had done var a = new MyClass(). It will be GCed just like any other object; that is when there are no more references to it and GC runs.

  3. No. Unless of course LoadIt were to return the object.

Comments

0

1)it depends on what you are trying to do, though as you are passing the new instance of the object it is always better to create new instance right where it is needed. but I am sure you are aware of that.

2)Any managed code is garbage collected so yes it will be garbage collected, and when will be decided by CLR.

3)yes you can reference it in LoadIt method but not outside LoadIt.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.