简体   繁体   中英

C# WebClient Memory Usage

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.

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?

Thanks

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.

"Memory usage" as displayed by tools like Taskmgr.exe or ProcExp.exe tells you squat about the actual memory in use by a program. When virtual memory is released by the garbage collector, the free space is almost never returned to the operating system. It gets added to a list of free blocks, ready for re-use by the next allocation. The odds that the free blocks coalesce back into a range of pages that can be freed are quite small.

This is never a real problem, this is virtual memory. Another way to make you feel good quickly is to minimize the main window of the program. That trims the working set, the amount of RAM in use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM