VB.Net 中的 OracleConnection 与 Oracle Data Access (ODP) 发生内存泄漏,但 C# 中没有
为什么下面的代码执行时会导致内存泄漏?仅当我以 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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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