簡體   English   中英

WebClient DownloadData 的高內存使用率

[英]High memory usage with WebClient DownloadData

在 webclient 下載數據后,我有一個問題內存沒有被釋放,所以我用下面的示例代碼進行了測試,它也發生了。 GC 最多收集了 10-20%,但仍然留下太多,我真的不知道可能出了什么問題,因為似乎沒有其他人有問題。


public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        list.Add("https://media1.giphy.com/media/Uwj2CxFJs8ZyM/giphy.gif");
        list.Add("https://media0.giphy.com/media/5Ut5IWqO2kfDU1FG8T/giphy.gif");
        list.Add("https://media1.giphy.com/media/Uwj2CxFJs8ZyM/giphy.gif");
        list.Add("https://media0.giphy.com/media/5Ut5IWqO2kfDU1FG8T/giphy.gif");
        list.Add("https://media1.giphy.com/media/Uwj2CxFJs8ZyM/giphy.gif");
        list.Add("https://media0.giphy.com/media/5Ut5IWqO2kfDU1FG8T/giphy.gif");
        list.Add("https://media1.giphy.com/media/Uwj2CxFJs8ZyM/giphy.gif");
        list.Add("https://media0.giphy.com/media/5Ut5IWqO2kfDU1FG8T/giphy.gif");
        list.Add("https://media1.giphy.com/media/Uwj2CxFJs8ZyM/giphy.gif");
        list.Add("https://media0.giphy.com/media/5Ut5IWqO2kfDU1FG8T/giphy.gif");
    }

    private List<string> list = new List<string>();
    public void DownloadThumb(string Url)
    {
        WebClient ThumbClient = new WebClient();
        ThumbClient.DownloadDataCompleted += DownloadFinished;
        ThumbClient.DownloadDataAsync(new Uri(Url));
    }

    private int donecount = 0;
    public void DownloadFinished(object sender, DownloadDataCompletedEventArgs e)
    {
        Image DownloadedImage;
        using (MemoryStream MemoryStreamTemp = new MemoryStream(e.Result))
        {
            DownloadedImage = Image.FromStream(MemoryStreamTemp);
        }
        DownloadedImage.Dispose();
        ((WebClient)sender).Dispose();

        donecount++;
        if (donecount == list.Count)
        {
            donecount = 0;
            button1.Enabled = true;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        foreach (string TempString in list)
        {
            DownloadThumb(TempString);
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        GC.Collect();
    }
}

在此處輸入圖像描述

答案是更改為 HttpClient,因為 WebClient 不應該那樣使用,而且它也較舊並且基本上已經過時。

使用以下代碼,內存使用量在開始時為 60MB > 下載后為 600 > GC 后為 200 > 在大約 90 秒后 GC 時為 70。


public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        list.Add("https://media1.giphy.com/media/Uwj2CxFJs8ZyM/giphy.gif");
        list.Add("https://media0.giphy.com/media/5Ut5IWqO2kfDU1FG8T/giphy.gif");
        list.Add("https://media1.giphy.com/media/Uwj2CxFJs8ZyM/giphy.gif");
        list.Add("https://media0.giphy.com/media/5Ut5IWqO2kfDU1FG8T/giphy.gif");
        list.Add("https://media1.giphy.com/media/Uwj2CxFJs8ZyM/giphy.gif");
        list.Add("https://media0.giphy.com/media/5Ut5IWqO2kfDU1FG8T/giphy.gif");
        list.Add("https://media1.giphy.com/media/Uwj2CxFJs8ZyM/giphy.gif");
        list.Add("https://media0.giphy.com/media/5Ut5IWqO2kfDU1FG8T/giphy.gif");
        list.Add("https://media1.giphy.com/media/Uwj2CxFJs8ZyM/giphy.gif");
        list.Add("https://media0.giphy.com/media/5Ut5IWqO2kfDU1FG8T/giphy.gif");
    }

    //Becase https://qa.ostack.cn/qa/?qa=619128
    static HttpClientHandler handler = new HttpClientHandler();

    private List<string> list = new List<string>();
    static readonly HttpClient ThumbClient = new HttpClient(handler);
    public async Task<Image> DownloadThumb(string Url)
    {
        byte[] bytes = await ThumbClient.GetByteArrayAsync(new Uri(Url));
        Image DownloadedImage = (Image)new ImageConverter().ConvertFrom(bytes);
        return DownloadedImage;
    }

    private async void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        List<Task<Image>> tasks = new List<Task<Image>>();
        list.ForEach(s => tasks.Add(DownloadThumb(s)));
        await Task.WhenAll(tasks);
        foreach (Task<Image> task in tasks)
        {
            task.Dispose();
        }
        button1.Enabled = true;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }
}

暫無
暫無

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

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