Reputation: 566
What are best practices for using (multiple) WebClients in regards to the memory consumption? In all resources on the internet I've read, webClient object is never released manualy, therefore my question is when it will released?
What if I'm using multiple webclient objects for fetching async data from the internet - they may and may not run in parallel?
Upvotes: 1
Views: 2066
Reputation: 431
WebClient implements the IDisposable interface but it's not actually used. It's inherited from the Component class which requires a Dispose handler to be registered within an "events" field/variable (you can check with Reflector/ILSpy/dotPeek/etc) so the garbage collector will probably automatically free it whenever that is neccessary. You might want to read a little about it here: .NET: Do I need to keep a reference to WebClient while downloading asynchronously? and also here: C# WebClient Memory Usage
As for using multiple WebClient objects, I'm not sure how much memory that'd use as I've never tried profiling or paying attention to memory consumption under those circumstances.
Also, if speed is not an issue, I'd use a List or Queue so you can process requests one after another right after they finish to prevent high memory consumption.
Upvotes: 1