开发者

creating object without reference

Just have a few qu开发者_开发知识库estions 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?


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.


If LoadIt was defined as

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

You can reference it again.


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.


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


  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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜