-1

I got an "Out of memory exception" and my 32 GB RAM isn't even full, it's downloading pictures, I want it to keep downloading and expanding RAM usage at night and even use the SSD as alternative "RAM" after the RAM is filled (I have 2 TB SSD and it has 390 GB empty).

And why is it expanding RAM, I'm doing a loop that contains a lot of this:

using (WebClient wc = new WebClient())
{
  wc.DownloadFileAsync(new Uri(url), @"g:\Folder1\" + i.ToString() + ".jpg");
}

So why isn't the RAM being released after each time the file (image) has completed downloading.

John1990
  • 29
  • 1
  • 7
  • Programming questions are out of scope here at Super User – Security Hound May 09 '21 at 23:53
  • Where to post it? – John1990 May 09 '21 at 23:54
  • You should read the tag descriptions, especially the one for C#, it indicates exactly which SE community this question would be within scope. However, it has other problems, which is one reason I am not voting to migrate it – Security Hound May 09 '21 at 23:55
  • @John1990, you can refer to the [Example](https://stackoverflow.com/a/32391580/11507778) to know how to use WebClient.DownloadFileTaskAsync and Task.WhenAll to mark the wc is completed. After that you can use wc.Dispose(). – Jack J Jun May 10 '21 at 08:02

1 Answers1

0

Set a pointer to the object to null to encourage garbage collection.

That said, languages such as Java an C# that rely on garbage collection, rather than having true destructors such as C++ and Delphi (Object Pascal) that can be called explicitly, may be slow to put out the trash.

  • I thought that by having: using (WebClient wc = new WebClient()) { } The webclient is disposed alone, so should I add in the brackets in the end: "wc.Dispose();" and/or "wc=null;"? And I have an asyn file download operation for the WebClient and the WebClient.DownloadFileCompleted Event isn't firing, so how do I know when to dispose the WebClient wc? – John1990 May 10 '21 at 01:50
  • Dispose of an object when done with it. https://stackoverflow.com/questions/574019/setting-an-object-to-null-vs-dispose – DrMoishe Pippik May 10 '21 at 21:20