开发者

How to "clear" an object?

What's the better way to clear an object:

  1. Write a method inside the class that clears all the members of the class: MyObject.Clear();

  2. Inside my code, in the business logic, null the object: MyObject开发者_JAVA百科 = null;


They do different things. Setting a variable to null does nothing to the object itself. It may mean that the object is now eligible for garbage collection, but it will make no difference if there are other "live" reference to the same object.

What should you do? Well, it entirely depends on your situation. If you have lots of different places with reference to the same object, and they should all see the "clearance", then modify the object. If you just need one variable to refer to a different object or no object at all, just change the value of the variable.

The important thing is to know the difference between the two - be very aware that the value of a variable is never an object... it's either a reference or a value type value.


I would recommend just constructing a new instance of the object, instead of clearing it.

If you are done with an object, but not the class containing its reference, setting it to null may be appropriate. However, if it's just being used within a method, you don't have to do anything (the garbage collector will clean it up sometime after there are no references to the object) - just let it "go away" by letting it fall out of scope. If it's in a collection, you can just remove it from the collection.

The one exception is if the object implements IDisposable. In this case, you may want to call it's Dispose() method, or try to structure your code to use it within a using block, in order for its resources to be cleaned up appropriately.


Second one. Be careful not to persist any references to your object. Any bound event handlers, references in collections make your instance "unfreeable".

If you come from C++, don't bother with Garbage Collector. I'm aware that the obsession of memory performance may lead to manipulating memory collection process. In fact you should not ever have the need to use it. Just unbind any references.


The answer to your question is, "it depends." It depends on what you're trying to do with it. If you want to keep a reference to that particular instance (i.e. others have a reference to it, or constructing it is expensive), then you're probably best off with a Clear method. But only if you're going to continue to use that particular object instance.

If you don't care about an object anymore, then just stop referring to it. In general, you don't have to set it to null, since the code generator can determine the object lifetime and make the object available for garbage collection. But if you want to make sure that the object is collected as soon as possible (i.e. during the next garbage collection cycle), then setting MyObject = null is the way to go. Again, that's not normally necessary, but it won't hurt anything.


I do not think so. While working with C # language course we have GC (garbage collection) for up to promote a change in this very difficult, if you use C + +, it proved quite easy

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜