Garbage Collection

  • Thread starter João Santa Bárbara
  • Start date
J

João Santa Bárbara

Hi all, lets get start.

i have a few questions to ask perhaps someone can help me.
i´m doing a simple aplication. some thing like this
i have a form with a button that opens another form in dialog mode.

' button code ...
Dim frm As New frmAbout
frm.ShowDialog(Me)
frm.Dispose()

first , when the application start, it uses (ex 17 mb of memory see using
the windows taskmanager), and after i close the dialog form, it uses even
more memory.
is there any sample that shows how realy gc works, i need to save memory not
spend an waiting for a stupid function todo his job.

second. when i use a form and open inside a function , the finaliza method
is never called. why ? see below ..

private sub Button1_click (...... )
Dim frm As New frmAbout
frm.ShowDialog(Me)
frm.Dispose()
end sub

i think it is suposed after the end sub, it calls the finalise of the
frmAbout ??? or It is'nt ?? the finalise is never called in this situation.
:(
what is microsoft thinking when they do the GC.

thks
JSB
 
H

Henning Krause [MVP]

Hello,
first , when the application start, it uses (ex 17 mb of memory see using
the windows taskmanager), and after i close the dialog form, it uses even
more memory.

Don't trust the taskmanager. Instead use either a CLR Profiler or check the
performance counter.
second. when i use a form and open inside a function , the finaliza method
is never called. why ? see below ..

By Finalizer you mean the destructor? That is not called when you call
dispose yourself.

Finalizers are very expensive, because the GC has to promote objects to
finalize to the next generation on the heap and run the finalizer. The
memory can only be reclaimed until the GC has run the next time.


Greetings,
Henning
 
D

David Browne

João Santa Bárbara said:
Hi all, lets get start.

i have a few questions to ask perhaps someone can help me.
i´m doing a simple aplication. some thing like this
i have a form with a button that opens another form in dialog mode.

' button code ...
Dim frm As New frmAbout
frm.ShowDialog(Me)
frm.Dispose()

first , when the application start, it uses (ex 17 mb of memory see using
the windows taskmanager), and after i close the dialog form, it uses even
more memory.
is there any sample that shows how realy gc works, i need to save memory
not spend an waiting for a stupid function todo his job.

' button code ...
Dim frm As New frmAbout
frm.ShowDialog(Me)
frm.Dispose()
frm = nothing
gc.Collect()

This will reduce the memory. In a fat client application, when you _know_
that the managed heap is mostly garbage, and you want to keep it from
getting any bigger, feel free to force a garbage collection.

David
 
R

Ranjan

Henning,
Are you sure that destructors are not called when you call a Dispose
yourself. I think this is done only when you GC.SuppressFinalize() from the
Dispose method that this happens.
 
D

David Levine

Ranjan said:
Henning,
Are you sure that destructors are not called when you call a Dispose
yourself. I think this is done only when you GC.SuppressFinalize() from
the
Dispose method that this happens.
A destructor is not called when a Dispose method is called - internally in
..NET they have nothing to do with each other.

When an object is no longer reachable via a root and a garbage collection
cycle occurs, the GC determines that the object has a finalizer and puts the
object on a special finalizer queue. This is serviced by a thread that runs
in the background. After the finalizer for the object has run and the next
GC cycle has occurred the memory is reclaimed.

When a Dispose method is called all that happens is that a method (called
Dispose) is invoked on the object; the intent of this method is to cleanup
the object (whatever that means). It does not involve the finalizer at all.
Typically after an object has been disposed it should not be used again -
however, this is not a requirement, it is a convention.

Quite often the Dispose method also calls GC.SuppressFinalize(). This has
the effect of removing the object from the list of objects that require
finalization so that the extra GC cycle to reclaim the memory does not
occur. However, calling GC.SuppressFinalize() from the Dispose method is not
done by the CLR, nor is it a requirement, it is only a convention that most
programmers follow.

For example, if a Dispose is called on an object but GC.SuppressFinalize()
is not called, and the object remains reachable, then the Finalizer is never
called and the memory is never reclaimed. Conversely, if the object is no
longer reachable then at the next GC cycle the Finalizer gets queued, and on
the following GC it is called, even if the Dispose method is never called.

This proves that the two operations are unrelated unless specifically tied
together via user logic.
 
R

Ranjan

I got the context wrong it think.
What Henning wrote was it is not inherent that a destructor is called when a
Dispose is called.
I misunderstood, misread the "not" part. Sorry.
 

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

Similar Threads


Top