内存泄漏/代码写得不好或两者兼而有之 - 正确处置 sqlconnection?
我正在调查 .net 1.1 Web 应用程序的一个问题,其中似乎有很多“内部连接致命错误”异常,可能是连接对象保持打开状态或未正确处理。 Web 服务器最终在重负载下崩溃。我检查了代码,实际上我们在 try catch finally 的所有地方都调用了 sqlconnection.close()
该项目实现了以下模式。有人可以告诉我这是否会导致内存泄漏?
aspx网页在Private Sub Page_Load中进行以下调用
Dim oDictionary As New dbDictionary
tagVal = oDictionary.GetTag(41)
,其中dbDictionary用于从DB获取sql表
Public Class dbDictionary
Inherits DBInteractionBase
Public Function GetTag(ByVal tagId)
'uses the _connection connection object and Executes sql which sometimes throws a sql exception, but closes the connection in finally
GetDictConnectionString()
'new sqlconnection object for another sqlcommand
Dim dictConnection As New SqlConnection
dictConnection = _connection 'At this point the _connection is closed.
'I think the dictConnection is a reference to _connection
Dim cmdToExecute As SqlCommand = New SqlCommand
' // Use base class' connection object
cmdToExecute.Connection = dictConnection
Try
' // Open connection.
dictConnection.Open()
' // Execute query.
adapter.Fill(toReturn)
Catch ex As Exception
Throw New Exception("Error occured.", ex)
Finally
' // Close connection.
dictConnection.Close()
cmdToExecute.Dispose()
adapter.Dispose()
End Try
End Function
Private Function GetDictConnectionString()
' initialize SqlCommand... Use base class _connection object
cmdToExecute.Connection = _connection
Try
_connection.Open()
adapter.Fill(toReturn)
Catch ex As Exception
Return "Error" ' Could this be the issue? The original exception isn't released?
Finally
_connection.Close()
cmdToExecute.Dispose()
adapter.Dispose()
End Try
End Class
这是它继承的DBInteractionBase
Public MustInherit Class DBInteractionBase
Inherits System.Web.UI.Page
Protected _connection As SqlConnection
Public Sub New()
_connection = New SqlConnection
_connection.ConnectionString = "some conn string"
End Sub
Public Overloads Sub Dispose()
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Overloads Sub Dispose(ByVal isDisposing As Boolean)
' // Check to see if Dispose has already been called.
If Not _isDisposed Then
If isDisposing Then
' // Dispose managed resources.
_connection.Dispose()
_connection = Nothing
End If
End If
_isDisposed = True
End Sub
现在,当代码执行时,调用网页永远不会调用Dispose。我想知道 GC 是否执行过处理代码?我可能看到的另一个问题是,如果 GetDictConnectionString 有异常,它永远不会重新抛出原始的 sql 异常。这会以某种方式将 sql 对象留在内存中吗?请记住,这是一个 .NET 1.1 应用程序,并且(.NET 1 中的 GC 效率不是很高)
另外,我想知道我可以使用 perfmon 在 Web 服务器上监视什么来指示内存泄漏。我计划修改此代码,并希望有一个指示问题已解决的指标。我看到 SqlClient 中的趋势:当前 # 连接池 - 它每天稳定增长 1000(这是与进程关联的当前池数。)所以我想知道它是否应该随着会话的减少而下降。我正在查看 (\ASP.NET Apps v1.1.4322(Total)\Sessions Active) 以查看服务器负载情况。
I am investigating an issue with a .net 1.1 web application where it seems that we had quite a few "Internal connection fatal error" exceptions possibly the connection objects staying open or not disposed of properly. The web server eventually crashes under heavy load. I checked the code and we are in fact calling sqlconnection.close() in all places in the try catch finally
The project implements the following pattern. Can someone tell me if it looks like this may cause a memory leak?
The aspx webpage makes the following call in Private Sub Page_Load
Dim oDictionary As New dbDictionary
tagVal = oDictionary.GetTag(41)
where dbDictionary is used for getting a sql table from DB
Public Class dbDictionary
Inherits DBInteractionBase
Public Function GetTag(ByVal tagId)
'uses the _connection connection object and Executes sql which sometimes throws a sql exception, but closes the connection in finally
GetDictConnectionString()
'new sqlconnection object for another sqlcommand
Dim dictConnection As New SqlConnection
dictConnection = _connection 'At this point the _connection is closed.
'I think the dictConnection is a reference to _connection
Dim cmdToExecute As SqlCommand = New SqlCommand
' // Use base class' connection object
cmdToExecute.Connection = dictConnection
Try
' // Open connection.
dictConnection.Open()
' // Execute query.
adapter.Fill(toReturn)
Catch ex As Exception
Throw New Exception("Error occured.", ex)
Finally
' // Close connection.
dictConnection.Close()
cmdToExecute.Dispose()
adapter.Dispose()
End Try
End Function
Private Function GetDictConnectionString()
' initialize SqlCommand... Use base class _connection object
cmdToExecute.Connection = _connection
Try
_connection.Open()
adapter.Fill(toReturn)
Catch ex As Exception
Return "Error" ' Could this be the issue? The original exception isn't released?
Finally
_connection.Close()
cmdToExecute.Dispose()
adapter.Dispose()
End Try
End Class
Here is the DBInteractionBase that it inherits
Public MustInherit Class DBInteractionBase
Inherits System.Web.UI.Page
Protected _connection As SqlConnection
Public Sub New()
_connection = New SqlConnection
_connection.ConnectionString = "some conn string"
End Sub
Public Overloads Sub Dispose()
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Overloads Sub Dispose(ByVal isDisposing As Boolean)
' // Check to see if Dispose has already been called.
If Not _isDisposed Then
If isDisposing Then
' // Dispose managed resources.
_connection.Dispose()
_connection = Nothing
End If
End If
_isDisposed = True
End Sub
Now when the code is executing, the Dispose never gets called by the calling web page. What I am wondering is if the dispose code ever gets executed by the GC? Another issue I see possibly is that if GetDictConnectionString has an exception, it never rethrows the original sql exception. Would that somehow leave the sql object in memory? Keep in mind this is a .NET 1.1 application and (GC in .NET 1 is not very efficient)
Also I am wondering what I can monitor on the web server using perfmon to indicate a memory leak. I am planning on modifying this code and would like an indicator that the issue was fixed. I see a trend in SqlClient: Current # connection pools - it's growing steadily 1000 each day (It's Current number of pools associated with the process.) so I am wondering if it should go down as the Sessions decrease. I am looking at (\ASP.NET Apps v1.1.4322(Total)\Sessions Active) to see what the server load looks like.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码:
正在创建两个
SqlConnection
(As New SqlConnection
正在创建一个),第一个永远不会被释放。这肯定是连接泄漏的根源。您还将处置从未创建过的连接
_connection
(看不到它是在哪里创建的)。假设它是按 1:1 创建的,这应该不是一个大问题,但如果在Try
块之前发生异常,则该连接也不会被释放。如果_connection
没有为此代码以 1:1 的方式创建,那么当其他东西试图使用它时,您最终可能会得到ObjectDisposeException
。This code:
Is creating two
SqlConnection
s (theAs New SqlConnection
is creating one), and the first would never be disposed. This could certainly be a source of leaking connections.You're also disposing of the connection
_connection
that you never created (can't see where this is being created). Assuming it's being created 1:1 this shouldn't be a huge problem, but if an exception occurs before yourTry
block, this connection will also not be disposed. If_connection
is not being created 1:1 for this code, you might end up gettingObjectDisposedException
if something else tries to use it.