1

i am calling setABC(id) on click multiple times.

MyClass _globalObject;

setABC(id)
{
   _globalObject = new MyClass(id);
}

Does the garbage collector destroys old object from memory and assign new object to _globalObject or it just create a new object and assign to _globalObject, consuming memory every time i call setABC(id);

(edit) maybe i didn't phrased it well, i don't concern about when GC occurs, rather will the following code destroy old object in memory and assign new object to _globalObject by not consuming memory more then once for on multiple calls.

_globalObject = new MyClass(id);
ADi
  • 219
  • 1
  • 5
  • 17
  • 1
    Both statements are true. Stop worrying. – H H Jan 21 '14 at 18:27
  • possible duplicate of [When Is The Object Eligible For Garbage Collection?](http://stackoverflow.com/questions/13144899/when-is-the-object-eligible-for-garbage-collection) – H H Jan 21 '14 at 18:29
  • Your question is slightly confusing. Are you trying to ask if garbage collection has to happen before each assignment to `_globalObject` occurs? – Damien_The_Unbeliever Jan 21 '14 at 18:29
  • Actually, it doesn't work the way you ask. You assign new reference to new object every time. GC runs on its own, not when you assign new object. Your old class will be marked for collection and when GC runs it will collect it – T.S. Jan 21 '14 at 18:37
  • @HenkHolterman Suggested duplicate is not helpful as it is for java and here is .Net. though I agree concept is same. – Sriram Sakthivel Jan 21 '14 at 18:39
  • Your code doesn't destroy anything unless you specifically talk to GC. GC will take care of it when it finds it appropriate – T.S. Jan 21 '14 at 18:39
  • Then i guess i must search when GC could occur for reference. Thanks All – ADi Jan 21 '14 at 18:41
  • @SriramSakthivel - I just picked one that looked closest. There are hundreds of duplicates of this on the site, and it's also off-topic and too-broad. – H H Jan 21 '14 at 18:52
  • @HenkHolterman duplicate, i can understand, but off-topic and too-broad?, thanks anyway. – ADi Jan 22 '14 at 10:43
  • @ADi - This is a Q&A site for specific questions. You need a C# tutorial. How many people do you think are learning C# what if every beginner question was dumped here, over and over again? – H H Jan 22 '14 at 10:50

2 Answers2

2

It will not "destroy" the old object referenced by _globalObject before assigning a new reference to it.

Basically you will just overwrite the contents of the variable with a new reference.

The old reference is replaced, and thus the old object is now somewhere in memory, with no active reference to it.

At some point the Garbage Collector will run, and rearrange memory and thus reuse the memory occupied by the first object.

So temporarily you will have 2 or more of that object in memory if you keep executing that code.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • ok so should i do _globalObject = null and then _globalObject = new MyClass() to delete old reference? – ADi Jan 21 '14 at 18:38
  • No, you can't "destroy" that object at all, GC will take care of it. Just leave the code as-is. – Lasse V. Karlsen Jan 21 '14 at 18:40
  • @ADi Simple. If you can access your object from your code that will not be GC'd because you can access it. If you cant then it is eligible for GC(when there are no more hidden references anywhere) – Sriram Sakthivel Jan 21 '14 at 18:41
1

It will clear the prior assignments to _globalObject when the Garbage Collector runs, you are consuming space for every assignment on the heap until the GC runs and sweeps the unreferenced objects (in this case, the instances of MyClass that _globalObject previously pointed to).

If you want to see this in action, download a trial copy of RedGates ANTS memory profiler and attach it to your process.

Allan Elder
  • 4,052
  • 17
  • 19