Memory Allocation In VisualStudio!
I have some sample webservice like below,
<WebMethod()> _
Public Function ExecuteCMD() As Boolean
Dim cnn As New Data.SqlClient.SqlConnection
Try
cnn.ConnectionString = "ConnectionString Here"
cnn.Open()
Dim cmd As New Data.SqlClient.SqlCommand("CommandText Here", cn)
cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
Return False
End Try
End Function
Normally,we always close the connection after using it, but there has never call close method.
Some of my friends said that webservice is stateless method and it doesn't matterw开发者_StackOverflow中文版hether we have close it or not.Is it true?I also know that cnn object life time is in only that method and visualstudio will dispose it end of that method. I really want to know object lifetime,how they allocated on memory and when they are disposed after using.Best Regards,
ChongIt does matter if you close the connection or not. SQL Server is quite resilient against lingering unclosed connections, but if you for example used Access you would quickly run out of available connections and get an error message when you tried to connect.
You have a connection object and a command object which both are disposable, so you should dispose them. Letting them go out of scope is not enough, the .NET memory management doesn't work that way. The IDisposable interface is intended for objects with unmanaged resources that need to be cleared up.
Most disposable objects has a finaliser as fallback if you fail to dispose them, so they will be cleaned up eventually, but you want to aviod that. You want to dispose the objects as soon as possible, otherwise they will remain in memory until the garbage collector comes around to clean them up.
A Using
block around the code where a disposable object is used is a good way to make sure that it's disposed properly. It uses a Try...Finally
block to ensure that the object is always disposed even if an error occurs.
<WebMethod()> _
Public Function ExecuteCMD() As Boolean
Using connection As New Data.SqlClient.SqlConnection
Try
connection.ConnectionString = "ConnectionString Here"
connection.Open()
Using command As New Data.SqlClient.SqlCommand("CommandText Here", connection)
command.ExecuteNonQuery()
End Using
Return True
Catch ex As Exception
Return False
End Try
End Using
End Function
You don't need to explicitly call close as calling dispose will close it for you.
If you refactor your code slightly then you can do this
Public Function ExecuteCMD() As Boolean
Using cnn As New Data.SqlClient.SqlConnection("ConnectionString Here")
Try
cnn.Open()
Dim cmd As New Data.SqlClient.SqlCommand("CommandText Here", cnn)
cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
Return False
End Try
End Using
End Function
The using block ensures that the connection is disposed of however you leave the code block. The whole thing will be removed permanantly once garbage collection kicks in.
You might want to think about a new error handling strategy as you are giving no information back to the client for it (or where appropriate, the user) can decide what to do about that error .
精彩评论