2011-09-14 42 views
1

我創建了兩個包含單個文本框的簡單XAML文件。使用綁定時的內存使用情況

第一個模板使用一個靜態文本:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid> 
     <TextBox Height="20" Width="120" Text="Static Text" /> 
    </Grid> 
</Page> 

第二模板使用Text屬性的綁定:不斷

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid> 
     <TextBox Height="20" Width="120" Text="{Binding Path=Test}" /> 
    </Grid> 
</Page> 

當我在一個循環中加載模板,內存使用情況當我用綁定的模板增加:

while (true) 
{ 
    // Memory usage increases 
    var binding = Application.LoadComponent(new Uri("/ConsoleApplication1;component/Binding.xaml", UriKind.Relative)); 

    // Memory usage stays constant 
    //var noBinding = Application.LoadComponent(new Uri("/ConsoleApplication1;component/NoBinding.xaml", UriKind.Relative)); 
} 

任何想法如何記憶使用綁定時的用法可以是常量?

+0

你如何測量記憶?你確定你沒有測量死的GC沒有收集的綁定對象嗎? –

+0

我使用ProcessExplorer來測量內存使用情況。如果循環運行時間足夠長,則會發生OutOfMemoryException。 – Daniel

回答

0

我們通過實施自定義MarkupExtension來解決問題。在關閉應用程序之前,默認的綁定不會被垃圾收集。