Dispatcher Memory Leak

  • Thread starter Thread starter hufaunder
  • Start date Start date
H

hufaunder

I have a WPF application that spins off a thread. That thread needs to
show a dialog (InputDlg) to get some user input. The From my thread I
am displaying the dialog as shown bellow. The problem is that each
time I display the dialog (when I call Invoke) some memory is
allocated but it is never being freed even when the thread terminates.
How can I get this memory to be freed?

Thanks

delegate Boolean DelegateUserInputWindow();

void MyThread()
{
if((Boolean)this.Dispatcher.Invoke(DispatcherPriority.Normal,
new DelegateUserInputWindow(GetUserInput))
{...}
}

Boolean GetUserInput()
{
InputDlg inputDlg = new InputDlg();
Boolean returnValue;
inputDlg.Owner = App.Current.MainWindow;
returnValue = (Boolean)inputDlg.ShowDialogWin();
inputDlg.Close();
return returnValue;
}
 
How are you determining that memory is being allocated and not being
freed? Are you looking at the .NET performance counters, or some memory
profiling tool?

If you are looking at Task Manager, then that's the wrong place to look,
as it is giving you the working set, not the actual memory consumption of
your app.
 
    How are you determining that memory is being allocated and not being
freed?  Are you looking at the .NET performance counters, or some memory
profiling tool?

    If you are looking at Task Manager, then that's the wrong place tolook,
as it is giving you the working set, not the actual memory consumption of
your app.

--
          - Nicholas Paldino [.NET/C# MVP]
          - (e-mail address removed)




I have a WPF application that spins off a thread. That thread needs to
show a dialog (InputDlg) to get some user input. The From my thread I
am displaying the dialog as shown bellow. The problem is that each
time I display the dialog (when I call Invoke) some memory is
allocated but it is never being freed even when the thread terminates.
How can I get this memory to be freed?

delegate Boolean DelegateUserInputWindow();
void MyThread()
{
  if((Boolean)this.Dispatcher.Invoke(DispatcherPriority.Normal,
          new DelegateUserInputWindow(GetUserInput))
  {...}
}
Boolean GetUserInput()
{
  InputDlg inputDlg = new InputDlg();
  Boolean returnValue;
  inputDlg.Owner = App.Current.MainWindow;
  returnValue = (Boolean)inputDlg.ShowDialogWin();
  inputDlg.Close();
 return returnValue;
}- Hide quoted text -

- Show quoted text -

Nicholas,

Thanks for the reply. Indeed, I do look at the Task Manager. What
should I look at then and what is a "working set"? In any case, if I
show the dialog often enough the app crashes with an out of memory
exception (each time I show the dialog Task manager shows 5MB more
consumption).

Thanks
 
Here is what the working set column means in Task Manager:

http://msdn2.microsoft.com/en-us/library/ms684891.aspx

If you are running out of memory, then something else is going on, IMO.
Where is the line that the OOM exception is being thrown? Or is it a
different exception?

Granted, you are using the Dispatcher to invoke this on the UI thread,
which could be an issue here, but I don't see anything in the code that
indicates that is the case.

Then again, I don't know what the definition of the InputDlg class is,
or ShowDialogWin, so I can't say what kind of interaction is going on.

Can you post a complete example which shows the problem?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

How are you determining that memory is being allocated and not being
freed? Are you looking at the .NET performance counters, or some memory
profiling tool?

If you are looking at Task Manager, then that's the wrong place to look,
as it is giving you the working set, not the actual memory consumption of
your app.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I have a WPF application that spins off a thread. That thread needs to
show a dialog (InputDlg) to get some user input. The From my thread I
am displaying the dialog as shown bellow. The problem is that each
time I display the dialog (when I call Invoke) some memory is
allocated but it is never being freed even when the thread terminates.
How can I get this memory to be freed?

delegate Boolean DelegateUserInputWindow();
void MyThread()
{
if((Boolean)this.Dispatcher.Invoke(DispatcherPriority.Normal,
new DelegateUserInputWindow(GetUserInput))
{...}
}
Boolean GetUserInput()
{
InputDlg inputDlg = new InputDlg();
Boolean returnValue;
inputDlg.Owner = App.Current.MainWindow;
returnValue = (Boolean)inputDlg.ShowDialogWin();
inputDlg.Close();
return returnValue;
}- Hide quoted text -

- Show quoted text -

Nicholas,

Thanks for the reply. Indeed, I do look at the Task Manager. What
should I look at then and what is a "working set"? In any case, if I
show the dialog often enough the app crashes with an out of memory
exception (each time I show the dialog Task manager shows 5MB more
consumption).

Thanks
 
The OOM exception happens when I allocate (new) some memory.

I downloaded a memory profiling tool. There I can also see the memory
increasing but only by a few megabytes. The strange thing is that this
tool shows the memory as about 70MBytes whereas the task manager shows
an increase from about 120MBytes to 800MBytes over a period of about 2
hours. The TM actually shows a higher memory usage right from the
beginning compared to this other tool (.Net Memory Profiler from
SciTech). Is it possible that the process in the TM also includes all
dlls and the other profiler only the program itself? In that case
could the leak actually be in one of the dlls?

The ShowWindow is very simple, i.e. it only does a return
(Boolean)ShowDialog().

Thanks

    Here is what the working set column means in Task Manager:

http://msdn2.microsoft.com/en-us/library/ms684891.aspx

    If you are running out of memory, then something else is going on,IMO.
Where is the line that the OOM exception is being thrown?  Or is it a
different exception?

    Granted, you are using the Dispatcher to invoke this on the UI thread,
which could be an issue here, but I don't see anything in the code that
indicates that is the case.

    Then again, I don't know what the definition of the InputDlg classis,
or ShowDialogWin, so I can't say what kind of interaction is going on.

    Can you post a complete example which shows the problem?

--
          - Nicholas Paldino [.NET/C# MVP]
          - (e-mail address removed)


How are you determining that memory is being allocated and not being
freed? Are you looking at the .NET performance counters, or some memory
profiling tool?
If you are looking at Task Manager, then that's the wrong place to look,
as it is giving you the working set, not the actual memory consumption of
your app.
- Show quoted text -

Nicholas,

Thanks for the reply. Indeed, I do look at the Task Manager. What
should I look at then and what is a "working set"? In any case, if I
show the dialog often enough the app crashes with an out of memory
exception (each time I show the dialog Task manager shows 5MB more
consumption).

Thanks- Hide quoted text -

- Show quoted text -
 
The ShowWindow is very simple, i.e. it only does a return
(Boolean)ShowDialog().

I haven't had a chance to use WPF very much, but in traditional indows
Forms when showing a dialog using ShowDialog, you must
explicitly .Dispose of the dialog afterwards.

Could something similar be happing here? Have you made sure that your
dialogs are being disposed (if necessary)?

Chris
 
I would say it is possible. Is the OOM exception being thrown
consistently in the same place? If so, where?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

The OOM exception happens when I allocate (new) some memory.

I downloaded a memory profiling tool. There I can also see the memory
increasing but only by a few megabytes. The strange thing is that this
tool shows the memory as about 70MBytes whereas the task manager shows
an increase from about 120MBytes to 800MBytes over a period of about 2
hours. The TM actually shows a higher memory usage right from the
beginning compared to this other tool (.Net Memory Profiler from
SciTech). Is it possible that the process in the TM also includes all
dlls and the other profiler only the program itself? In that case
could the leak actually be in one of the dlls?

The ShowWindow is very simple, i.e. it only does a return
(Boolean)ShowDialog().

Thanks

Here is what the working set column means in Task Manager:

http://msdn2.microsoft.com/en-us/library/ms684891.aspx

If you are running out of memory, then something else is going on, IMO.
Where is the line that the OOM exception is being thrown? Or is it a
different exception?

Granted, you are using the Dispatcher to invoke this on the UI thread,
which could be an issue here, but I don't see anything in the code that
indicates that is the case.

Then again, I don't know what the definition of the InputDlg class is,
or ShowDialogWin, so I can't say what kind of interaction is going on.

Can you post a complete example which shows the problem?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


How are you determining that memory is being allocated and not being
freed? Are you looking at the .NET performance counters, or some memory
profiling tool?
If you are looking at Task Manager, then that's the wrong place to look,
as it is giving you the working set, not the actual memory consumption
of
your app.
- Show quoted text -

Nicholas,

Thanks for the reply. Indeed, I do look at the Task Manager. What
should I look at then and what is a "working set"? In any case, if I
show the dialog often enough the app crashes with an out of memory
exception (each time I show the dialog Task manager shows 5MB more
consumption).

Thanks- Hide quoted text -

- Show quoted text -
 
Chris,

WPF doesn't work like that. The WPF rendering engine is separated from
the model completely, so that your Window object is just that, a Plain Old
Object. The engine looks at the model and handles all rendering (for the
most part) based on the model. There are no implementations of IDisposable
in WPF on any of the objects or controls.
 
Back
Top