Tracing memory leaks and bad application design

D

David Brunning

I have an application which appears to be leaking memory. It appears
to behave the same way under .NET CF SP2 and SP3 Beta.

The application uses SqlCe as a datastore, but also caches memory in
forms using dataset classes that I wrote.
State is managed by a state manager class that I wrote - basically it
reads/writes from an internal XML dataset which gets persisted to disk
at certain key points.

The statemanager fulfils one other very important function - when the
application loads a single login form is created and shown as a dialog
(I don't know why but I've had problems with Application.Run, but
thats another post). When the login is successful this form spawns
child forms in a loop using :

while (this.mNextFormType!=null &&
this.mNextFormType!=typeof(Forms.Login))
{
//Open a form if there is a type specified.
CurrentForm=(Forms.BaseForm)Activator.CreateInstance(this.mNextFormType);
CurrentForm.StateMgr=this.mStateMgr;
CurrentForm.MobileData=this.mMobileData;
this.mNextFormType=null;
CurrentForm.ShowDialog();
CurrentForm.Dispose();
}

The mNextFormType variable is a System.Type which is populated in the
close of the child form by raising an event on the shared statemanager
object.

So - all i have to do is run the application, log in, spawn my first
child form, close it, spawn it again, close it etc. (i.e force the
loop above to repeat using the same form type) and my memory usage
keeps on going up.

I think that I am disposing everything that I can dispose, and if I
can't dispose it then I find the next best option (e.g. dataset
objects get cleared and then set to null).

I'm using P/Invoke on CoreDll.dll to log the memory usage on the PDA
to a file (And yes, I've adjusted the figures to include the effect of
the log file and tested that the logging on its own isn't responsible
for any leaks!) and can see a quite clear trend in available physical
memory (downward) and memory load (upward).

Can anyone give me help in the following areas:
a) Application design - the loop above is ok, but how should I really
do it?
b) Tracing objects which still reside in memory and haven't yet been
finalised.
c) Confirming whether this is a memory leak or not.

I guess b) is my main interest - I can see garbage collections
happening from my memory usage data but can also see that the trend is
downwards despite that - are there any profiling tools available for
the .NET CF which will help me to see what is actually happening?

Any help gratefully received.
 
D

Duncan Mole

Hi,

AFAIK there aren't any tool for reported uncollected object although I
wished there was myself not too long ago. The only thing available in the
Compact Framework is the PerfMonitor report described below:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/netcfperf.asp

Do the forms register on any event of the statemanger object? Doing so would
mean that the statemanager object would keep hold of a reference to the form
and therefore prevent the forms being collected until the statemanger object
was collected.
 
D

David Brunning

There are indeed events being handled in the state manager class - I
am now dropping them in the Dispose of the form and will do some more
monitoring. I'll post back if it makes a difference.

Thanks
 
D

Duncan Mole

Try adding code to unregistering the event hander for the statemanger object
on the form closing.
 

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