我正在使用 WebClient,DownloadString("http://example.com/string.txt "); 当我调用它时,内存会跳起来,但再也不会下降,因为我需要从网上下载 2-3 个不同的字符串,所以内存会跳起来很多。
我是 C# 的新手,还在学习,但是在我从网上下载字符串后,有没有清除内存的方法? 如果没有,您是否知道我可以使用占用更少内存的其他方法从 Web 读取数据?
谢谢
最佳答案
WebClient工具 IDisposable ,因此您的代码应如下所示:
string result;
using (WebClient client = new WebClient())
{
result = client.DownloadString("http://example.com/string.txt");
}
Console.WriteLine(result);
这将确保释放 WebClient 实例使用的大部分资源。
剩下的最终会被垃圾收集器清理掉。您无需为此担心。
关于C# WebClient 内存使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3383167/