title: VB.net Garbage collector not releasing objects
body: <p>First of all, thanks in advance for your help. </p>
  
  <p>I've decided to ask for help in forums like this one because after several months of hard working, I couldn't find a solution for my problem. </p>
  
  <p>This can be described as '<i>Why an object created in VB.net isn't released by the GC when it is disposed even when the GC was forced to be launched?</i>"</p>
  
  <p>Please consider the following piece of code. Obviously my project is much more complex, but I was able to isolate the problem: </p>
  
  <pre><code>Imports System.Data.Odbc
  Imports System.Threading
  Module Module1
      Sub Main()
          'Declarations-------------------------------------------------      
              Dim connex As OdbcConnection 'Connection to the DB
              Dim db_Str As String         'ODBC connection String      
          'Sentences----------------------------------------------------
              db_Str = "My ODBC connection String to my MySQL database"
              While True
                  'Condition: Infinite loop.
                  connex = New OdbcConnection(db_Str)
                  connex.Open()
                  connex.Close()
  
                  'Release created objects
                  connex.Dispose()
  
                  'Force the GC to be launched
                  GC.Collect()
  
                  'Send the application to sleep half a second
                  System.Threading.Thread.Sleep(500)
              End While
      End Sub
  End Module
  </code></pre>
  
  <p>This simulates a multithreaded application making connections to a MySQL database. As you can see, the connection is created as a new object, then released. Finally, the GC was forced to be launched. I've seen this algorithm in several forums but also in the MSDN online help, so as far as I am concerned, I am not doing anything wrong. </p>
  
  <p>The problem begins when the application is launched. The object created is disposed within the code, but after a while, the availiable memory is exhausted and the application crashes. </p>
  
  <p>Of course, this problem is hard to see in this little version, but on the real project, the application runs out of memory very quickly (due to the amount of connections made over the time) and as result, the uptime is only two days. Then I need to restart the application again. </p>
  
  <p>I installed a memory profiler on my machine (Scitech .Net Memory profiler 4.5, downloadable trial version  <a href="http://memprofiler.com/" rel="nofollow noreferrer">here</a>). There is a section called 'Investigate memory leaks'. I was absolutely astonished when I saw this on the 'Real Time' tab. If I am correct, this graphic is telling me that none of the objects created on the code have been actually released:  </p>
  
  <p><img src="https://i.stack.imgur.com/Orql7.jpg" alt="http://www.zuzsso.com/images/screenshot3.jpg"></p>
  
  <p>The surprise was even bigger when I saw this other screen. According to this, all undisposed objects are <b>System.Transactions</b> type, which I assume are internally managed within the .Net libraries as I am not creating any object of this type on my code. Does it mean there is a bug on the VB.net Standard libraries???: </p>
  
  <p><img src="https://i.stack.imgur.com/qtlUo.jpg" alt="http://www.zuzsso.com/images/screenshot4.jpg"></p>
  
  <p>Please notice that in my code, I am not executing any query. If I do, the <b>ODBCDataReader</b> object won't be released either, even if I call the <b>.Close()</b> method (surprisingly enough, the number of unreleased objects of this type is exactly the same as the unreleased objects of type <b>System.Transactions</b>)</p>
  
  <p>Another important thing is the statement <b>GC.Collect()</b>.  This is used by the memory profiler to refresh the information to be displayed. If you remove it from the code, the profiler wont' update the real time diagram properly, giving you the false impression that everything is correct.</p>
  
  <p>Finally, if you ommit the <b>connex.Open()</b> statement, the screenshot #1 will render a flat line (that means all the objects created have been successfully released), but unfortunatelly, we can't make any query against the database if the connection hasn't been opened.</p>
  
  <p>Can someone find a logical explanation to this and also, a workaround for effectively releasing the objects? </p>
  
  <p>Thank you all folks.</p>
  
  <p>Nico</p>