C#WebClient的内存使用情况 [英] C# WebClient Memory Usage

查看:554
本文介绍了C#WebClient的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WebClient的,DownloadString(http://example.com/string.txt);
当我把它叫做内存跳起来,但从来没有一次停机,因为我需要从网上下载2-3个不同的字符串内存颇多跳起来。

I am using WebClient,DownloadString("http://example.com/string.txt"); When I call it the memory jumps up, but never goes down again, and since I need 2-3 different strings downloaded from the web the memory jumps up quite much.

我是新的C#和还在学习,但反正是有清除内存我已经下载从网络后的字符串?
如果不是,你知道的任何其他方法,我可以用它来阅读网络至极使用较少的内存?

I am new to C# and still learning, but is there anyway to clear the memory after I have downloaded the string from the web? If not, do you know any other methods I can use to read from the web wich uses less memory?

感谢

推荐答案

WebClient的工具 IDisposable的的,所以你的代码应该是这样的:

WebClient implements IDisposable, so your code should look like this:

string result;
using (WebClient client = new WebClient())
{
    result = client.DownloadString("http://example.com/string.txt");
}
Console.WriteLine(result);

这将确保通过WebClient的实例所使用的大部分资源被释放。

This will make sure that most resources used by the WebClient instance are released.

其余部分将最终被垃圾收集清理。你不需要这个担心。

The rest will eventually be cleaned up by the Garbage Collector. You don't need worry about this.

这篇关于C#WebClient的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆