0

Why does the code below cause a memory leak when executed? The error only happens when I use Microsoft Visual Studio 2005 or 2008 in vb.net language. If I use C # is, there is no problem.

Dim strCon As String = "data source=SRV-10G;user id=Test;password=1234"

dim factory as DbProviderFactory = DbProviderFactories.GetFactory("Oracle.DataAccess.Client");

Dim conexao As IDbConnection = factory.CreateConnection

conexao.ConnectionString = strCon

conexao.Open()


For cont As Integer = 1 To 100000

  Dim comando As IDbCommand = conexao.CreateCommand()

  comando.CommandText = "Select * from tabela where campo = " & cont

  Dim leitor As IDataReader = comando.ExecuteReader

  While leitor.Read

    Dim v As String = leitor.GetValue(1).ToString

  End While

  leitor.Close()
  leitor.Dispose()

  comando.Dispose()
Next

conexao.Close()
conexao.Dispose()
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • Can you post the working c# code as well so we can compare the two, please. And can you post the reason why you think there's a memory leak (process memory keeps increasing, database connections not closed, etc). – ligos Nov 09 '11 at 22:09

2 Answers2

0

How do you know there's a memory leak? How can you have a memory leak with a language that's garbage collected? If you're getting an error message that specifically tells you there's a memory leak, there's probably a problem in the database driver itself, which could have been written in any language. Writing any sort of .NET code against that driver shouldn't cause any problems by virtue of what .NET language you're using though.

  • When you run the above code and goes into "task namager" the amount of memory goes up to a die process. This example does not give the same error when run with C #. The memory it stays constant. Use this example and see for yourself. – Marco Aurelio Nov 11 '11 at 13:54
  • "How can you have a memory leak with a language that's garbage collected?" If you think you cannot have memory leaks in .NET (or to be precise memory retention), then you need to read a little more about how the garbage collector works. One example is to forget to unsubscribe from event handlers. – Stephen Drew Feb 10 '14 at 12:13
0

The code you have posted looks OK. Although VB.NET has a Using statement which may help (ad will at least make your code easier to read!). You'll need to post more info before any more suggestions can be given.

You may want to check this old answer: Any decent C# profilers out there? Although specific to c#, all the .NET memory profilers should work for VB.NET as well.

Community
  • 1
  • 1
ligos
  • 4,256
  • 2
  • 25
  • 34