Memory Leak with (WPF) Frame

  • Thread starter Thread starter Lloyd Dupont
  • Start date Start date
L

Lloyd Dupont

I have a very simple WPF Application (code inline below).
If, as a user, I click "Web Frame" then "Nothing" (then, optionally,
"GC.Collect")
A Frame object has been created and removed from all hierarchy yet it stays
in memory (as a memory profile will tell you).

Now, what can I do about that?
As my application run, I lose memory from multiple Frame object created
dynamically (which are not collected)!!!

Worst than that on some of them I navigate to multimedia content and the
music keeps playing without way of stopping it!

== Window1.xaml ===
<Window x:Class="FrameMemLeak.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Memory Leak in XAML"
Height="400" Width="400">
<DockPanel>
<StackPanel DockPanel.Dock="Left" Orientation="Vertical">
<Button Content="Web Frame" Click="OnWebFrame"/>
<Button Content="Nothing" Click="OnNothing"/>
<Button Content="GC.COllect()" Margin="0,10,0,10" Click="OnCollect"/>
</StackPanel>
<Border x:Name="carea" BorderThickness="1" BorderBrush="Black" />
</DockPanel>
</Window>
=== Windows1.xaml.cs ===
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

private void OnWebFrame(object sender, RoutedEventArgs e)
{
carea.Child = new Frame() { Source = new Uri("http://www.google.com") };
}

private void OnNothing(object sender, RoutedEventArgs e)
{
carea.Child = null;
}

private void OnCollect(object sender, RoutedEventArgs e)
{
GC.Collect();
}
}
================
 
It's not necessarily a memory leak if it isn't collected, there may be root
around depending on the implementation of the object. Here are a couple
things you can try. Wait around 11 minutes and perform a GC collect and then
analyze the heap. If it is still around, dump the heap and trace the root of
the object to see where it is.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
 
Your memory profiler of choice should be able to tell you what
reference is keeping you instances from being collected.

If not you can get this information using WinDbg + sos.dll. There are
numerous how-tos available so just do a search (very short how-to:
find the object on the heap and do a !gcroot).

Brian
 
I had an answer from another forum.
as long as Frame.Content is not null it's referenced by NavigtionService.

I think you could put that into the category (ahum, cough): "it's not a bug,
it's a feature"

Frame as memory leak but that's normal, so we don't tell our customer!
Seriously, this issue should be mentioned in the documentation, in my app I
have heaps of "unreferenced" Frame reference dangling around, and I was
wondering where from this memory leak come from.

And no: I'm not happy about it, I am not happy I had to research a lot to
find out what the cause and that THIS cause was no where mentionned.

Now I know I should set all my Frame to be
var f = new Frame()
f.Unloaded += (o, e) { f.Content = null; };

to avoid memeory leak, otherwise I will get memory leak.
Now I don't consider acceptable that this is a "known bug" (or so I was
told) which is mentionned nowhere!!!!


(Google "Frame memory leak" doesn't bring up anything)



Alvin Bruney said:
It's not necessarily a memory leak if it isn't collected, there may be
root around depending on the implementation of the object. Here are a
couple things you can try. Wait around 11 minutes and perform a GC collect
and then analyze the heap. If it is still around, dump the heap and trace
the root of the object to see where it is.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------


Lloyd Dupont said:
I have a very simple WPF Application (code inline below).
If, as a user, I click "Web Frame" then "Nothing" (then, optionally,
"GC.Collect")
A Frame object has been created and removed from all hierarchy yet it
stays in memory (as a memory profile will tell you).

Now, what can I do about that?
As my application run, I lose memory from multiple Frame object created
dynamically (which are not collected)!!!

Worst than that on some of them I navigate to multimedia content and the
music keeps playing without way of stopping it!

== Window1.xaml ===
<Window x:Class="FrameMemLeak.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Memory Leak in XAML"
Height="400" Width="400">
<DockPanel>
<StackPanel DockPanel.Dock="Left" Orientation="Vertical">
<Button Content="Web Frame" Click="OnWebFrame"/>
<Button Content="Nothing" Click="OnNothing"/>
<Button Content="GC.COllect()" Margin="0,10,0,10" Click="OnCollect"/>
</StackPanel>
<Border x:Name="carea" BorderThickness="1" BorderBrush="Black" />
</DockPanel>
</Window>
=== Windows1.xaml.cs ===
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

private void OnWebFrame(object sender, RoutedEventArgs e)
{
carea.Child = new Frame() { Source = new
Uri("http://www.google.com") };
}

private void OnNothing(object sender, RoutedEventArgs e)
{
carea.Child = null;
}

private void OnCollect(object sender, RoutedEventArgs e)
{
GC.Collect();
}
}
================
 
Back
Top