-2

By reading this article i got a bit confused:

https://www.dotnetperls.com/using

They demonstrated that calling using for 2 disposable objects, ended with more memory usage, while the method without usings, doing the same instructions, used lower memory.

Can you explain to me why using increases memory consumption in that example?

I would like to understand if I should avoid using in some cases.

I thought that dispose was a good idea to free memory, but it looks like I was wrong.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
  • Think about what happens in second snippet in case of an exception being thrown before `ms.Dispose()` ... – Fildor Sep 20 '17 at 09:17
  • You should always make sure you explicitly call `.Dispose()` (either directly or with `using` statements). Don't be concerned with memory usage - it'll sort itself out over time. – Enigmativity Sep 20 '17 at 09:20
  • Downvotes don't really make sense to me either... It's a reasonable question in regards to the article you linked. – Matthew Watson Sep 20 '17 at 10:09
  • I'd ditch that website and get a copy of CLR Via C# –  Sep 20 '17 at 15:32

1 Answers1

6

You seem to be conflating several issues here:

  1. What's the difference between using and Dispose()?
  2. When should you use using or Dispose()?
  3. But what about that article that says using uses more memory?

The answers to the duplicate question tell you when to use using rather than Dispose(), so I won't repeat that here.

But when should you use either? Read on.

Dispose() is used to free up unmanaged resources, not managed resources such as memory.

As such, you should always call Dispose() either directly or via using for any type that implements it, unless the type explicitly documents that you don't need to. Do not avoid calling it just because someone tells you that the memory usage might be less.

Incidentally, I'm very dubious about that article on DotNetPearls. It looks like bad advice, and I'm not even sure that the way they measured the memory use is valid.

In answer to your question:

Can you explain me why using increases memory consumption in that example?

Not without trying to reproduce it - but I suspect that the way they measured it is not correct, and the vagaries of garbage collection have yielded misleading results.

Note how the article doesn't state how they measured the memory use, and certainly doesn't give a compilable reproduction. Unless such evidence is provided, I think their reported results should be ignored.

The article's conclusion about what might cause the "increased" memory use is nonsense:

The unneeded using statements ended up wasting memory. This could be explained by the method size bloat and the increased complexity.

If you look at the memory use, it varies from run to run. But "method size bloat and the increased complexity" (tiny though it would be) would cause a constant change in memory usage.

It's apparent that this part of the article should be approached with extreme caution.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • @MarcoSalerno I think the dupes answer it. The point is, "using" does more than just call "Dispose". It creates a new scope and makes sure Dispose is called also in case of an exception. That's why it will have slightly different impact on memory / performance but will improve readability and stability. – Fildor Sep 20 '17 at 09:30
  • But i asked it related to the memory usage noticed on the article's example, it's a specific thing, not the same – Marco Salerno Sep 20 '17 at 09:39
  • 4
    @MarcoSalerno You should always call it unless the type's documentations says otherwise.. Ignore the nonsense in that article about the memory use. It's bad advice. – Matthew Watson Sep 20 '17 at 09:43