Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / April 2005

Tip: Looking for answers? Try searching our database.

Garbage Collection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Saurabh Garg - 04 Apr 2005 18:20 GMT
Hi there,

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

Or is there any way i can dispose the whole collection and the
underlying objects at aone go.

Thanks and Regards
Saurabh Garg
Jon Skeet [C# MVP] - 04 Apr 2005 18:54 GMT
> 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.
[quoted text clipped - 14 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

saurabhgarg2k@gmail.com - 04 Apr 2005 20:25 GMT
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:
> > I have a query about the garbage collection
> >
[quoted text clipped - 31 lines]
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
Jon Skeet [C# MVP] - 04 Apr 2005 20:33 GMT
> 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.

No. You can easily write a helper method to do that though, and call
that from various places.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Sherif ElMetainy - 10 Apr 2005 23:45 GMT
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

> Sorry Jon,
> i mistakenly write there is no option looping, of course looping is the
[quoted text clipped - 46 lines]
>> http://www.pobox.com/~skeet
>> If replying to the group, please do not mail me too

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.