简体   繁体   English

有什么更好的方法清除与内存有关的数组

[英]What will be better way to clear Array in concern of Memory

**** EDIT ***** ****编辑*****

There is a question - here , I have gone through. 有一个问题- 在这里 ,我经历了。 My Purpose of asking is different (as in that question, asker is asking for solution(s) which he got. and here I'm looking for better way to accomplish the task (I know two solutions and asking better one or even better 3rd option)) 我提出问题的目的有所不同(例如,在这个问题中,问问者正在寻求他得到的解决方案。在这里,我正在寻找一种更好的方法来完成任务(我知道两种解决方案,并要求更好的一种或什至更好的第三种解决方案选项))


I have an array of Obj . 我有一个Obj数组。 I know these two ways which are clearing (or doing similar) an Array. 我知道清除(或执行类似)数组的这两种方法。

int length = arrayOfObj.Length

1: arrayOfObj = new Obj[length] 1: arrayOfObj = new Obj[length]

2: Array.Clear(arrayOfObj, 0, length) 2: Array.Clear(arrayOfObj, 0, length)

they both are apparently doing the same thing. 他们俩显然都在做同一件事。 But which one of this is better in terms of Memory. 但是就内存而言,哪一种更好。 Or I should leave this matter of Internal Garbage collection ? 还是我应该把这件事留给内部垃圾收集?

You don't have to worry about it. 您不必担心。 As you suggested, garbage collection will take care of it for you. 正如您所建议的,垃圾收集将为您处理。 However, if you still have references elsewhere to any of the objects that were in your array, those objects, of course, won't be garbage collected. 但是,如果您在其他地方仍然引用了数组中的任何对象,则这些对象当然不会被垃圾回收。

I should also mention something about IDisposable as suggested by the comments under your question. 我还应该提到一些关于IDisposable正如您的问题下的评论所建议的那样。 The .NET framework's garbage collector will release managed resources when appropriate. .NET框架的垃圾收集器将在适当的时候释放托管资源。 The purpose of the IDisposable interface is to guarantee that unmanaged resources get released, such as open files and streams. IDisposable接口的目的是确保释放非托管资源,例如打开的文件和流。 If the class whose objects are in your array have any such unmanaged resources, the class should implement IDisposable and you should call Dispose on every object before you clear the array. 如果其对象位于数组中的类具有任何此类非托管资源,则该类应实现IDisposable并且应在清除数组之前对每个对象调用Dispose

Normally when dealing with IDisposable , you would utilize the using statement to guarantee that the object was properly disposed when you're done with it. 通常,在处理IDisposable ,您将使用using语句来保证对象在using IDisposable得到正确处理。 However, since you are dealing with a collection of disposable objects in this case, it's unlikely each object was created inside a using statement (it could be possible in some extravagant scenario). 但是,由于在这种情况下您要处理一次性对象的集合 ,因此不太可能在using语句内创建每个对象(在某些奢侈的情况下可能会发生)。 Therefore, you must loop through the elements and call Dispose on each one before you clear the array**. 因此,您必须遍历所有元素并在清除数组之前对每个元素调用Dispose **。 This, again, is only if the objects in your collection have unmanaged resources that need to be dealt with before each object is garbage collected. 同样,仅当集合中的对象具有非托管资源时才需要对这些资源进行管理,然后才对每个对象进行垃圾回收。

** Keep in mind: you'll run into trouble if you have external references to any of those objects and try to use them again afterward since they will have already been disposed. **请记住:如果您对这些对象中的任何一个都有外部引用,并且由于这些对象已经被废弃而尝试再次使用它们,将会遇到麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM