C# WebClient Memory Usage

后端 未结 2 698
说谎
说谎 2021-01-20 10:26

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

2条回答
  •  佛祖请我去吃肉
    2021-01-20 10:45

    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);
    

    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.

提交回复
热议问题