簡體   English   中英

如何在Windows Phone 8中釋放圖像緩存/內存?

[英]How to free image cache/memory in Windows Phone 8?

在我的Windows Phone 8應用程序中,我有一個帶有LongListSelector的頁面,該頁面綁定到1000個對象的列表,這些對象具有base64string的屬性。 現在要顯示圖像,我編寫了此轉換器以將bas64string轉換為stream

public object Convert(object value, Type targetType, object parameter,  System.Globalization.CultureInfo culture)
{
    if (!value.ToString().Contains("http://"))
    {
        string str = value.ToString();
        byte[] bytes = Converter.FromBase64String(str);

        using (MemoryStream stream = new MemoryStream(bytes))
        {
            stream.Seek(0, SeekOrigin.Begin);

            BitmapImage image = new BitmapImage();
            image.SetSource(stream); 
            bytes = null;
            var memoryusage = string.Format("Memory: {0} bytes", 
            DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage"));
            Debug.WriteLine(memoryusage);
            return image;
        }
    }
    else
    { 
        return null;
    }
}

這是記憶的用法:

Memory: 92549120 bytes
    Memory: 92946432 bytes
    Memory: 92946432 bytes
    Memory: 92946432 bytes
    Memory: 92946432 bytes
    Memory: 93192192 bytes
    Memory: 93192192 bytes
    Memory: 96079872 bytes
    Memory: 100700160 bytes
    Memory: 100700160 bytes
    Memory: 109568000 bytes
    Memory: 111734784 bytes
    Memory: 142852096 bytes
    Memory: 143056896 bytes
    Memory: 143056896 bytes
    Memory: 143261696 bytes
    Memory: 140791808 bytes
    Memory: 141103104 bytes
    Memory: 141529088 bytes
    Memory: 142151680 bytes
    Memory: 146784256 bytes
    Memory: 146784256 bytes
    Memory: 155066368 bytes
    Memory: 156368896 bytes

當內存等於或可能大於此156368896字節的某個字節時,應用程序將由於EngineExecutionException崩潰。 一旦我得到“ OutOfMemoryException”:

image.SetSource(stream);

顯然,這是一個內存問題。 我需要清除圖像緩存,但是如何清除? 我在此答案中看到了鏈接https://stackoverflow.com/a/12267163/1949475 ,但是我無法使用它。

注意:並非所有圖像都同時顯示,在我返回並再次返回頁面后,應用程序會占用大量內存,從而更改了要在LongListSelector中顯示的數據。

在轉換器類中進行設置很重要

BitmapImage image = new BitmapImage(); 
image.DecodePixelType = DecodePixelType.Logical; 
image.CreateOptions = BitmapCreateOptions.BackgroundCreation; 
image.CreateOptions = BitmapCreateOptions.DelayCreation; 
image.DecodePixelWidth = 56; 
image.DecodePixelHeight = 100;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM