Dispose Generic List

  • Thread starter Thread starter Radenko Zec
  • Start date Start date
R

Radenko Zec

I have filled generic list with data on one form.
When I close form generic list stay in memory waiting GC to collect.
I want to implement code on form close to dispose generic list.
 
I have filled generic list with data on one form.
When I close form generic list stay in memory waiting GC to collect.
I want to implement code on form close to dispose generic list.

Calling Dispose won't collect any memory. You could call GC.Collect
when you close the form, after making sure you no longer have any
references to the data. GC.Collect is generally not worth calling, but
when closing a form it may be reasonable.

Jon
 
Calling Dispose won't collect any memory. You could call GC.Collect
when you close the form, after making sure you no longer have any
references to the data. GC.Collect is generally not worth calling, but
when closing a form it may be reasonable.

Maybe. Though, I have to say...before I actually did any .NET
programming, I inferred all sorts of resource-management headaches caused
by garbage collection that would require calls to the GC.Collect() method,
all of which turned out to be non-issues once I started doing actual .NET
stuff.

When you have unmanaged stuff that has to be released at a specific point
in time, there's the Close() and/or Dispose() paradigm to take care of
that. For anything else, you really just don't care about something being
released. In neither case does GC.Collect() need to be called explicitly.

The one exception I can think of is after a form shown using ShowDialog()
has been closed or the form is an MDI form (both of these are mentioned
specifically in the docs for Form.Close()), and IMHO explicitly calling
Dispose() or using the "using" statement is better than calling the GC
directly. And of course this scenario doesn't relate directly to the
original post anyway, since they are not talking about unmanaged resources
(the managed List<> will be automatically released at such time as the
memory is needed).

Pete
 
Peter Duniho said:
Maybe. Though, I have to say...before I actually did any .NET
programming, I inferred all sorts of resource-management headaches caused
by garbage collection that would require calls to the GC.Collect() method,
all of which turned out to be non-issues once I started doing actual .NET
stuff.

When you have unmanaged stuff that has to be released at a specific point
in time, there's the Close() and/or Dispose() paradigm to take care of
that. For anything else, you really just don't care about something being
released. In neither case does GC.Collect() need to be called explicitly.

The one exception I can think of is after a form shown using ShowDialog()
has been closed or the form is an MDI form (both of these are mentioned
specifically in the docs for Form.Close()), and IMHO explicitly calling
Dispose() or using the "using" statement is better than calling the GC
directly. And of course this scenario doesn't relate directly to the
original post anyway, since they are not talking about unmanaged resources
(the managed List<> will be automatically released at such time as the
memory is needed).

The reason why it may be worth calling GC.Collect after a form is
closed is that in that special situation you *may* know more than the
garbage collector does - that a lot of memory is now reclaimable, even
if a lot of that memory was due to object in generation 2.

I'd love to claim that these were my original thoughts, but they're not
- they're the thoughts of someone rather smarter, Rico Mariani:

http://blogs.msdn.com/ricom/archive/2004/11/29/271829.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top