Memory not released

G

Guest

Can someone explain what I understood wrong.

I thought that the Framwork garbage collection takes care of the memory
management in a DotNet application.

I have however an application that keeps eating up memory and not releasing
it, to the point where all the resources of the computer are consumed.

Example, I open up a MDI child windows that has all kinds of object. When I
monitor this in Task Manager, I can see that the amount of memory my
application is consuming increases when the window is opened, but when I
dispose the window tha amount is not decreased.

Where did I go wrong. As far as I can tell the application uses only managed
resources nad dataconnections to SQL server

Petri
 
M

Markus

Petri said:
Can someone explain what I understood wrong.

I thought that the Framwork garbage collection takes care of the memory
management in a DotNet application.

I have however an application that keeps eating up memory and not releasing
it, to the point where all the resources of the computer are consumed.

Example, I open up a MDI child windows that has all kinds of object. When I
monitor this in Task Manager, I can see that the amount of memory my
application is consuming increases when the window is opened, but when I
dispose the window tha amount is not decreased.

Where did I go wrong. As far as I can tell the application uses only managed
resources nad dataconnections to SQL server

- First of all, do not fully trust the TaskManager, rather use perfmon
(Start -> Run -> perfmon), and add a new Performance Monitor for your
Application, watching "Private Bytes" (and maybe some others).
This is by far correcter.

- Second: If you hold a reference to a resource, then it cannot be
disposed/garbage collected. So be sure, that you really get rid of
the references to your resources, and use disposed, when the
documentation wants you to use it, as behind the scene there might
be accessed some unmanaged resources.

hth
Markus
 
G

Guest

Markus said:
Petri wrote:
- Second: If you hold a reference to a resource, then it cannot be
disposed/garbage collected. So be sure, that you really get rid of
the references to your resources, and use disposed, when the
documentation wants you to use it, as behind the scene there might
be accessed some unmanaged resources.

I think I've missed that part in my App ;-(

I don't have any Dispose methods for my objects, that's bad right ?
Now if I start making these methods, what exactly am I supposed to do in
them. Call the Dispose method of all the DataAdapters, Datasets, variables
etc. or what.

My App has a database connection open all the time when the App is running.
Is that a good practice or should I close that down when no queries take
place.
hth
Markus

Petri
 
M

Markus

I don't have any Dispose methods for my objects, that's bad right ?

Depending on the objects. Forms get disposed automatically, if they are
closed (Form.Close()). Also all containing UserControls. But e.g. images
are not gettings disposed, so when you are using a PictureBox!

Now if I start making these methods, what exactly am I supposed to do
in them. Call the Dispose method of all the DataAdapters, Datasets,
variables etc. or what.

Generally, it is a good practice to call the Dispose() method, if there
is one... because it normally exists as there are any things to be
disposed ;-) but most of them are handled by the framework automatically
(as I said, the Form.Close() method as an example).

My App has a database connection open all the time when the App is
running. Is that a good practice or should I close that down when no
queries take place.

Well, it is depending on the applications. Generally spoken it is better
to close the connection and re-establish it again, but if it is a small
application or if the user is connection to the database all the time,
it doesn't really make THAT difference.

What I suspect is, that perhaps you have some kind of ArrayList with
objects, which never is set to null, and there are more and more objects
added or something similar.

Finally, don't be afraid, that task-manager is going up, just be
careful, if it doesn't get down again. My applications often go up about
30% of the memory used (display in taskmanager), and then they go down
again (I think, this is because the garbage collector is in action)...


However, make sure, the application does not go up endless, then you are
most probably fine. If it goes up and never down with memory, you have
some memory leak anywhere in your code.... and be sure to stay focussed,
if it is just a small app, which is used only for 5min. and then closed
again, memory doesn't really make a problem, as after 5min. the
application gets restartet... but there are also other application...
and generally spoken: The better the application performs the better you
have done your job ;-).

Markus
 
G

Guest

Markus said:
Finally, don't be afraid, that task-manager is going up, just be
careful, if it doesn't get down again. My applications often go up about
30% of the memory used (display in taskmanager), and then they go down
again (I think, this is because the garbage collector is in action)...


However, make sure, the application does not go up endless, then you are
most probably fine. If it goes up and never down with memory, you have
some memory leak anywhere in your code.... and be sure to stay focussed,
if it is just a small app, which is used only for 5min. and then closed
again, memory doesn't really make a problem, as after 5min. the
application gets restartet... but there are also other application...
and generally spoken: The better the application performs the better you
have done your job ;-).

This is a kind of application that runs all day, hence my worry ;-(

Any idea how ofteh (roughly) the GC runs ? if I stare the Task Manager for
10 minutes should it run at least once during that time ?

Also I know at which point of the execution a lot of memory is consumed that
should be released later. Is there an easy way to see that which objects do
not get released.

I also have objects there that are not supposed to release, but rather reset
themselves to empty and then wait for next transaction. Could it be that some
of these objects consume more and more memory each round ?
 

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

Top