0

为什么下面的代码在执行时会导致内存泄漏?该错误仅在我以 vb.net 语言使用 Microsoft Visual Studio 2005 或 2008 时发生。如果我用C#的话,是没有问题的。

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()
4

2 回答 2

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.

于 2011-11-09T22:13:53.517 回答
0

你怎么知道有内存泄漏?使用垃圾收集的语言怎么会有内存泄漏?如果您收到一条错误消息,专门告诉您存在内存泄漏,则可能是数据库驱动程序本身存在问题,该驱动程序可能是用任何语言编写的。但是,根据您使用的 .NET 语言,针对该驱动程序编写任何类型的 .NET 代码都不会导致任何问题。

于 2011-11-09T21:57:59.917 回答