2

I was wondering if using curly brackets to define scopes within a method call would 'force' or give a information to the C# Garbage collector to release the memory allocated within that block, so, let's take the piece of code below as an example:

void MyMethod() {
//here some important code, like reading some information from disk, api, whatever.
//Open brackets to define scope...
  {
   var myClassObject = new MyClass();
   myClassObject.DoSomething();
   var mySecondClassObject = new MySecondClass();
   mySecondClassObject.DoSomething();
  }
   //I expect that at this moment, GC would release myClassObject and MySecondClassObject from the Heap... 
   //is that correct?

  //here do something else
  /...
}
Bruno
  • 924
  • 9
  • 20
  • You can´t force the GC to do anything - unless you call `GC.Collect`. However there´s usually no reason to do so at all. Instead this is allmost ever an indication that you´re doing something whrong before - in particular that your members do too much and thus your variables don´t go out f scope. – MakePeaceGreatAgain Mar 17 '19 at 14:00
  • No, I am not doing this, but I believed that was true, since my variables are not inside of the scope anymore, why GC will not clean them ?? – Bruno Mar 17 '19 at 14:03
  • 5
    No. GC can collect variables [even from within currently active scope](https://devblogs.microsoft.com/oldnewthing/20100810-00/?p=13193). You are confusing *eligibility for garbage collection* with *actually collecting the garbage*. Your variables will become eligible as soon as it is possible, but that [does not mean](https://stackoverflow.com/a/28461701/11683) the collection will start. – GSerg Mar 17 '19 at 14:03
  • 2
    GC in isn´t deterministic in when it does cleaning. Usually it does so when there´s **memory-pressure**, this is when you´re running out of memory for your process, for example. But again: **why do you care**? Do you have any specific problem? – MakePeaceGreatAgain Mar 17 '19 at 14:05
  • Well, I always care if my app can consume the least amount of resources as it could, so if the statement of my post was true, I would do that, however, as many of you have proven otherwise, it doesn't matter :(. But thank your for sharing your thoughts! – Bruno Mar 17 '19 at 14:09
  • 1
    Maybe this helps: https://stackoverflow.com/questions/28461652/when-does-garbage-collection-get-triggered-in-c?noredirect=1&lq=1 – MakePeaceGreatAgain Mar 17 '19 at 14:09
  • @Bruno your Approach is certainly a good one, but with .net you have only so much control of the memory Management. If you are in need of having "absolute" control of memory you _have_ to switch to another language like c++ that has no GC at all where you have to take care of memory management by yourself. – Venson Mar 17 '19 at 14:12
  • 1
    This is C++-thinking, it doesn't apply to C# at all. .NET uses an aggressive collector, objects can be destroyed while a method is still executing that uses that object. Important, what you allocate in Main() doesn't necessarily live for the life of the process. One of the primary, but highly invisible jobs of the just-in-time compiler is to make that safe. It builds a table that reports the exact machine code addresses at which the object is in use. Neither braces nor scope play a role at all. – Hans Passant Mar 17 '19 at 14:38
  • 1
    @Bruno If you want your app to consume the least amount of resources that it can, then C# is not the best language for your app, and you could alternatively look into C, C++, Rust, etc. But if it's just a pet peeve and not really critical to your app, then you can live with the fact that the C# GC will eventually recycle the memory (unless you are leaking handles, of course). – Theodoros Chatzigiannakis Mar 17 '19 at 14:42
  • I started this discussion, because I have seen the use of this 'brackets' approach once, it was written by a 'C# specialist', so I thought that it would worth to bring this to a discussion, I passed this information forward, hehehehe it turns out that I was wrong :) living and learning guys!! – Bruno Mar 17 '19 at 14:42
  • 1
    @Bruno C# compiles to IL which doesn't have the notion of these local brackets. So to the runtime (the JIT-compiler, the GC, etc) it doesn't matter if you include these brackets in the C# code or not. – Theodoros Chatzigiannakis Mar 17 '19 at 14:43

1 Answers1

2

In Short: No. you are not able to Force the Garbage collector to dispose this allocated !classes! to be disposed right now.

In Long: You might be. You can simply set the variables to Null and call GC.Collect(). This will invoke the normal GC collection and Can include the previous unreferenced classes in its collection but there is no Garantie that this will happen. But this will invoke the GarbageCollector for your Whole application not just this thread. This is by design not possible.

Venson
  • 1,772
  • 17
  • 37
  • 3
    Please do not recommend setting object references to null. [Look here](https://stackoverflow.com/questions/17130382/understanding-garbage-collection-in-net) – Hans Passant Mar 17 '19 at 14:41