Re: Garbage Collection



The framework collections do not implement IDisposable.
You can implement you own collection class that implements IDisposable
interface. In the dispose method loop throw all objects and dispose those
which are not null and implements IDisposable.

Please note that disposing the whole collection is not a good idea if the
objects in the collections are referenced by another objects.

Best regards,
Sherif


<saurabhgarg2k@xxxxxxxxx> wrote in message
news:1112642758.083224.249660@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Sorry Jon,
> i mistakenly write there is no option looping, of course looping is the
> option but wat my query is that is this the only option, cant i dispose
> the whole clooection and the underlying objects without looping.
>
> Thanks and Regards
> Saurabh Garg
>
> Jon wrote:
>> Saurabh Garg <saurabhgarg2k@xxxxxxxxx> wrote:
>> > I have a query about the garbage collection
>> >
>> > If i an array of an object of size, say 1000, and the object
>> > implements Idisposable, first of all how should i call dispose
> method
>> > on an object array.
>>
>> You'd call Dispose on each element of the array, if it's not null.
>>
>> > To better understand the query i am providing a
>> > snippet here which will create a collection of objects.
>> >
>> > public function Createcollection
>> > {
>> > for(int i=0;i<1000;i++)
>> > {
>> > Object obj=new object();
>> > ObjectCollecion.add(obj);
>> > }
>> > }
>> >
>> > Now how should i dispose all the 1000 objects which are created.I
> cant
>> > wait till the finalize method will be called on all 1000 objects.so
>> > should i loop through all the collection to Release it and what
> will
>> > be the case of unmaaged resources, as there is no finalization
> method
>> > for them, so there is no option looping through the Collection to
> free
>> > the unmanaged resources.
>>
>> Why do you think there isn't a way of looping through the whole
>> collection? For each element of the collection, just check whether
> it's
>> non-null and implements IDisposable, and call Dispose if so.
>>
>> --
>> Jon Skeet - <skeet@xxxxxxxxx>
>> http://www.pobox.com/~skeet
>> If replying to the group, please do not mail me too
>


.



Relevant Pages

  • Re: Garbage Collection
    ... i mistakenly write there is no option looping, ... option but wat my query is that is this the only option, cant i dispose ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Garbage Collection
    ... > option but wat my query is that is this the only option, cant i dispose ... > the whole clooection and the underlying objects without looping. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Garbage Collection
    ... > on an object array. ... You'd call Dispose on each element of the array, ... > wait till the finalize method will be called on all 1000 objects.so ... so there is no option looping through the Collection to free ...
    (microsoft.public.dotnet.framework.clr)