1

Noob here. Let's say I made a function like this:

public void something()
{
   List<string> list = new List<string>();
}

Then I call something() 10x. Will it creates 10 objects of the same name or will it replaces with the new empty one so the object is still 1 in total? Does doing this would cause memory leak? Sorry for bad english.

Alfian
  • 43
  • 7

2 Answers2

5

Yes, it will create 10 new objects if called 10 times. No, this doesn't cause a memory leak1. One of the main points of working in a managed language with a garbage collector is that most of the time you don't have to think about how memory is being used2.

It's also good that each call creates a new instance of the List - because more and more these days, you'll have multiple threads running your program and you wouldn't want two calls that happen at the same time to interfere with each other's use of list.


1To properly be a memory leak in .NET, you need something long-lived to retain references to what should be short-lived objects, when it will never actually use those references. Local variables, such as here, have relatively short lives.

2And when you do care, you're much better off learning to use a memory profiler to see what memory is being used where.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • I'm still new about this. Therefore I experimented by spamming the function (example above) in visual studio and the process memory didn't increase. So what happened to the other 9 objects I created before? Did they get disposed by GC or stay forever unused? – Alfian Jul 15 '20 at 15:21
  • 1
    @Alfian - bear in mind that each empty list is of the order of about 40 bytes in size. But the CLR doesn't go to the OS for each individual allocation it makes - it grabs large chunks of memory and then doles out sections of it itself. You're unlikely to see it need to reallocate from the OS for less than 1K of total memory usage. The other lists are likely garbage, but when and whether they *have* been collected is a point most often not worth dwelling on. The GC is meant to be invisible to the programmer and mean you *don't* have to think about memory usage. – Damien_The_Unbeliever Jul 15 '20 at 15:36
  • In addition, let's say that modenrs Operating System that runs on moderns CPU do the memory management well, thnaks to 32-bits and 64-bits as well as virtual memory and/or swapping. That said, the only critical point for data-intensive applications like servers or image and video processing software is the amount of physical RAM before having performances down. –  Jul 15 '20 at 16:29
2

No it does not cause memory leaks.

C# works on .Net Framework. Compiled .Net applications run in special environment called CLR (Common Language Runtime).

There is garbage collector in CLR which releases the memory for objects that are no longer being used by the application. Garbage collector works in background, so you do not have to worry about it.

If you want to you can call garbage collector manually: System.GC.Collect(), but it is not recommended. It is expensive to run. You must be abolutely sure that code is well written and you know what you are doing. Here is some examples: stack-clr-gc

zolty13
  • 1,943
  • 3
  • 17
  • 34