Strange Memory Leak?

R

RobRasti

I'm trying to support some issues with some older code written in
VB.Net for Compact Framework 1.1. The problem I'm having is when a
form is opened the application consumes some amount of memory. When
the form is disposed the memory is never reclaimed.

Upon further research the code was using PictureBox controls instead
of buttons, and I found the image objects being used were not being
disposed properly. However there still appears to be a spike in
memory use when using a SQLCE DataAdapter to fill a DataTable and then
use that DataTable as a datasource for a DataGrid. The basic code is
as follows:

User Taps "Search" button on screen.
- Build SQL command string based on some criteria the user enters.
- Instantiate the DataAdapter
- Call the Fill method
- Set the DataSource of the DataGrid
- Dispose of the SelectCommand object of the DataAdapter
- Dispose of the DataAdapter object

When the form closes I've overridden the Dispose method and I dispose
of all onscreen controls and in the case of the datagrid I dispose the
DataSource property and set it to Nothing, then dispose the datagrid
itself.

Is there something I am missing? It seems as though the application
is hanging onto the data it pulls from the database and displays via
the datagrid. I am explicitly calling the Dispose method of the form
and have some internal application logging that proves it is being
called when the form is closed. However I still can't seem to find
where this memory is being consumed and not released.

Rewriting this code or recompiling it for a later version of the
compact framework is not an option as my client has roughly a couple
thousand handheld computers with this application deployed and are not
looking to perform a massive upgrade that it would take to update the
compact framework.

Thanks for your help.
-R-
 
C

chris-s

Is the application generating any type of 'out of memory errors'? I
assume not. Resources will not be freed up until the garbage collector
deems it necessary or when it won't impact on performance. Resources
are not released immediately for performance reasons. Of course, this
depends on you not having anything which is maintaing a reference or
handle to anything in the object you are expecting to get released.

Why are you 'manually' disposing the datagrid etc? If the controls
were placed on the form during designtime and so are created when the
form is created, then you should not need to dispose of them yourself.
You only really might choose to do this when you create these objects
manually, even then, the GC should tidy them up when they fall out of
scope.

Chris
 
R

RobRasti

Well I am "Manually" disposing of the PictureBox.Image property
because we have seen significant memory improvements by doing so. So
I can only assume that the Image property is not being disposed of by
the default Dispose of the PictureBox.

The application does encounter "OutOfMemoryException" errors during
data updates. In our testing lab we had been unable to duplicate this
error until we were told that the end user was performing a "Search"
and that is what lead me to probe the memory use during this
particular form. It was then found that before the user clicked the
"Search" button the memory in use would be around some X, and after
the button was clicked it would increase (we did worst case scenario
testing and saw an increase close to 15 MB, or roughly the entire
database table they were searching). Then when the form was closed
and all objects disposed of the memory never appeared to be released.

We've actually run some excel graphs of this occurrence and it shows
when the application performs the DataAdapter.Fill the running memory
jumps up (Which is somewhat expected because we are building a
DataTable in memory) and when they close the form out the memory is
never released. Even forcing calls to GC.Collect and putting in
Thread.Sleep to force the GC to think we are idle has not reclaimed
this memory.

The strange thing is we can reload this search form and the memory
does not increase again when the search method is done. That is we do
not see a 15 MB increase the second time we click the Search button.

Is there something anyone can see that I am not doing to remove
references to objects? I've even gone as far as to take the
DataSource property of the DataGrid, cast it back to a DataTable and
call the Clear method since the DataTable does not contain a Dispose
method.

Thanks for you help.
-R-
 

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