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.

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

PixL
  • 31
  • 1
  • 2
  • No, don't worry. The way garbage collection works (for .NET and elsewhere) is that memory is not *immediately* freed. What causes a clean-up is memory pressure. – Steven Sudit Aug 01 '10 at 18:13
  • So long as you're not holding onto references to dead objects (and you're properly disposing of the ones that are `IDisposable`), you can count on the GC to eventually clean things up. But if you want to see for yourself, you can test it by inserting a call to `GC.Collect` (http://msdn.microsoft.com/en-us/library/system.gc.collect.aspx). That's just to see: you almost never want to do that in real code. – Steven Sudit Aug 01 '10 at 18:15

2 Answers2

9

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.

Steven Sudit
  • 19,391
  • 1
  • 51
  • 53
dtb
  • 213,145
  • 36
  • 401
  • 431
  • 1
    Well, in this case I doubt it will change anything, as the returned string from `DownloadString` isn't exactly an unmanaged resource that will be freed on `Dispose()`. Good general advice to always wrap `IDisposable` in `using`, though. – Joey Aug 01 '10 at 18:40
  • 1
    @Johannes Rössel: It's not the return value that is disposed by WebClient, but the internal objects that are used by it, e.g., HttpWebRequest/HttpWebResponse objects. There is a reason why WebClient implements IDisposable, because if there wasn't, it wouldn't implement it. – dtb Aug 01 '10 at 18:49
  • As I am new to C#, can you explain this IDisposable thing? – PixL Aug 01 '10 at 23:47
  • 1
    WebClient doesn't override Dispose() metodh from Component class. The using-pattern is right, however doesn't solve the memory-leak issue. – TcKs May 20 '14 at 14:17
3

"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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536