title: Does garbage collection run immediately after GC.Collect()?
body: <p>The question is just for research purposes. </p>
  
  <p>I've read many books about C# and this question always comes to my mind. What I understood that C# is managed code and all garbage collection occurs when CLR decides when to run garbage collection. Let's start.</p>
  
  <p>Let's imagine that I have simple class <code>Student</code>:</p>
  
  <pre><code>public class Student
  {
      public int IdStudent { get; set; }
      public string Name { get; set; }
      public string Surname { get; set; }
  }
  class Program
  {
        static void Main(string[] args)
        {
  This is row1:    Person person = new Person() {IdPerson=1, Name="Bill", SurName="Collins"};
  This is row2:    System.GC.Collect();
  This is row3:    string str="Hello World!";
      }        
  }
  </code></pre>
  
  <p>Please, approve or reject my suppositions:</p>
  
  <ol>
  <li>Am I right that garbage collection is not run immediately at <strong>row2</strong>?</li>
  <li><p><code>GC.Collect()</code> is just a <strong>request</strong> to make a garbage collection which DOES NOT RUN IMMEDIATELY at row2. This row <strong>maybe executed</strong> in x milliseconds/seconds. 
  In my view, method <code>System.GC.Collect();</code> just says to garbage collector that garbage collector should run garbage collection, but <strong>real garbage collection may occur in x milliseconds/seconds</strong></p></li>
  <li><p>Only garbage collector knows when garbage collection will be run. And If there is free space in Generation 0, then garbage collection will not occur in the <strong>row2:</strong> <code>row2: System.GC.Collect();</code></p></li>
  <li><p><strong>It is not possible to run garbage collection immediately as we are programming in managed environment and only CLR decides when to run garbage collection.</strong> Garbage collection can be run in x milliseconds/seconds or garbage collection <strong>maybe not run</strong> cause there is enough space in generation 0 to create new objects after calling method <code>GC.Collect()</code>. What programmer can do is just ask CLR to run garbage collection by method <code>GC.Collect()</code>.</p></li>
  </ol>
  
  <p><strong>Update:</strong></p>
  
  <p>I've read <a href="https://msdn.microsoft.com/en-us/library/xe0c2357%28v=vs.110%29.aspx?f=255&amp;MSPPError=-2147217396" rel="nofollow">this msdn article about <code>GC.Collect Method ()</code>.</a>. However, it is unclear to me when real clearing of unreferenced objets is started. MSDN says:</p>
  
  <blockquote>
    <p>GC.Collect Method () forces an immediate garbage collection of all 
    generations.</p>
  </blockquote>
  
  <p>However, in <strong>Remarks</strong> I've read this one:</p>
  
  <blockquote>
    <p>Use this method to try to reclaim all memory that is inaccessible. It
    performs a blocking garbage collection of all generations.</p>
  </blockquote>
  
  <ol start="5">
  <li>I am confused by this <strong>"Use this method to TRY"</strong> and I think that garbage collection <strong>may not occur</strong> cause <strong>CLR</strong> decides that there is enough space to create new objects. Am I right?</li>
  </ol>