2012-04-22 21 views
1

的BitmapImage泄漏內存時CreateOptions是什麼,但DelayCreation,是擺在視覺樹(在一個圖像或圖像刷)WP7 BitmapImage的內存泄漏時CreateOptions不DelayCreation

我與SDK 7.1見狀在模擬器上以及手機(HTC泰坦) 步驟:

  1. 打開新的WP項目
  2. 開始在App.xaml.cs新dispatchertimer,打印內存使用(DeviceStatus)每秒和做GC.Collect的() 。 (考慮newing了計算出的尺寸的字節數組爲比(MemoryUsageLimit少一點 - CurrentMemoryUsage))
  3. 添加一個StackPanel名爲ImagePanel內部的ContentPanel
  4. 添加2個按鈕的ContentPanel(1用於創建和加入10個圖像 與代碼和一個ImagePanel以清除ImagePanel的孩子)
  5. 查找高分辨率圖像(只是這樣的內存使用情況是很容易 可見),並放置在您的本地IIS
  6. 當按鈕1被點擊寫:

    Random rand = new Random(); 
    for(int i=0 ; i<10 ; i++) 
    { 
        //use 192.168.55.100 instead of localhost when running on the phone 
        //to be able to see traffic in fiddler.Change localhost to machinename for the emulator 
        var uri = "http://localhost/images/bigimage.jpg?tc=" + rand.Next(Int32.MaxValue); 
        var bitmap = new BitmapImage 
            { 
             UriSource = new Uri(uri, UriKind.RelativeOrAbsolute), 
             CreateOptions = BackgroundCreation //or None or DelayCreation 
            }; 
        ImagePanel.Children.Add(new Image { Source = bitmap }); 
    } 
    
  7. 當按鈕2被點擊寫:

    ImagePanel.Children.Clear(); 
    GC.Collect(); 
    GC.WaitForPendingFinalizers(); 
    GC.Collect(); 
    

嘗試用CreateOptions的所有變化,看在內存中的差異。 繼續添加和刪除元素,並查看您將使用哪個CreateOption獲取OutOfMemoryException。

有人可以驗證這一點嗎?

+0

這是什麼問題? – thumbmunkeys 2012-04-22 16:29:18

+0

@atomaras - 正因爲你要求它消失並不意味着GC真的會這樣做。這裏沒有泄漏。 – 2012-04-22 17:57:36

+1

Isnt得到OutOfMemoryException是一個很好的指標,它不可收集? – atomaras 2012-04-22 18:46:31

回答

0

由於BitmapImage依賴於GDI +,這不是預期的行爲嗎? (因此本質上是非託管代碼?)IIRC它不是ImagePanel泄漏,而是反覆創建的BitmapImage。

如果將值分配給相同的BitmapImage對象,則不應出現該問題。您需要在繪圖線程上執行此操作,否則GDI將適合) 個人上,每當我使用WP7中的圖像時,我都使用ImageTools而不是本機類。輸出可以直接分配給一個圖像控制,但它可以異步加載,使用較少的內存和操縱要快得多