how to use Using

  • Thread starter Thread starter cj
  • Start date Start date
C

cj

I'm looking at using Using to try an plug a suspected memory leak. I've
already started using dispose for everything that implements it but no
luck. I have several questions about Using

Can I nest using?

I take it using only applies to resources that it was given as
parameters and will not help with those dimmed inside a using block. Right?

I guess I'm still supposed to close and dispose sql connections and
close files etc before end using?
 
You can nest using
You can use Using on connections, files and streams - on anything
implementing IDispose.

HTH
Alex
 
So in this code below I should be good with the sql adapter but what
about the streamwriter? Since I dimmed it inside the using block is it
guaranteed to be cleaned up too?

Using Sql2000DesktopDataAdapter As New
System.Data.SqlClient.SqlDataAdapter("select * from " & sourceTable,
TextBox2.Text)

Try
Sql2000DesktopDataAdapter.Fill(myds, 0, 10, sourceTable)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.DataSource = myds.Tables(sourceTable)

Dim sw As New System.IO.StreamWriter("C:\test.txt", True)
sw.WriteLine("TESTING")
sw.Close()
End Using

Or do I have to do this?

Using Sql2000DesktopDataAdapter As New
System.Data.SqlClient.SqlDataAdapter("select * from " & sourceTable,
TextBox2.Text), sw As New System.IO.StreamWriter("C:\test.txt", True)

Try
Sql2000DesktopDataAdapter.Fill(myds, 0, 10, sourceTable)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.DataSource = myds.Tables(sourceTable)

sw.WriteLine("TESTING")
sw.Close()
End Using
 
cj,

How do you know that you have a memory leak. And please don't tell me that
you have seen that in the taskmanager, one look at Google gives you
thousends of responses that that says nothing.

Cor
 
cj,
How do you know that you have a memory leak. And please don't tell me
that you have seen that in the taskmanager, one look at Google gives
you thousends of responses that that says nothing.

Cor

On a very slightly related point....

Cor... I'm curious... I have observed processes in Taskmanager (in this case
not .Net but Ruby) which appeared to eternally use greater and greater quantities
of memory (presumably)as time goes on.

I can understand that due to garbage collection issues (in either Ruby or
..Net) that taskmanager's absolute concept of memory usage for a given process
can be way off.

However, when memory keeps going up and up and up, it becomes fairly clear
that something is up.

Now granted, I have no Idea if this is what the OP is experiencing, but doesn't
this mean that TaskManager isn't entirely useless in detecting at least the
existence of a problem?
 
Use nested Using:

Using sw As New System.IO.StreamWriter("C:\test.txt", True)
sw.WriteLine("TESTING")
sw.Close()
end using

I don't remember if sw is IDisposable, but compiler will tell you

HTH
 
I'm looking at using Using to try an plug a suspected memory leak. I've
already started using dispose for everything that implements it but no
luck. I have several questions about Using

Can I nest using?

Sure. Not a problem. You can also put all your IDisposable objects
in one using.
I take it using only applies to resources that it was given as
parameters and will not help with those dimmed inside a using block. Right?

Yep.

I guess I'm still supposed to close and dispose sql connections and
close files etc before end using?

Nope. Close is simply a call to there internal clean up methods. In
other words, there is no difference between calling close and
dispose. Close is just more natural symantically...
 
Thanks! One little followup. When you say I don't have to call
closing, is that because end using calls the cleanup method?
 
Rory and Cor, yes Taskmanager does show the same thing Rory is seeing
and after over 2 weeks of use the operating system issues a warning
about page file or virtual memory almost full (I wasn't the one who saw
the message so I can't say specifically what it said).

I am currently using perfmon to track the problem an I see:

# Bytes in All Heaps fluctuates but has shown no signs of increase
Large Object Heap Size hasn't changed since it started
% Time in GC has fluctuated but shown no upward trend and is very small
Avg .004

Private bytes continues to climb whenever the system is active
Working Set also continues to climb at the same rate as private bytes
Virtual Bytes continues to climb in a stair step fashion--jumps up every
few hours.
Thread Count fluctuates but shows no upward trend Avg 15

Cor, I have a post titled "multithreaded tcp/ip server large page file"
that has quite a history if you want to know more. I've been working
with Charles Wang to try to solve the memory problems. If you wish to
offer suggestions on memory leaks I'd be glad to hear them but I think
it'd be best to put them in respose to my other post. This post was
intended only to determine how to use using.
 
Thanks! One little followup. When you say I don't have to call
closing, is that because end using calls the cleanup method?

That's correct. Using is really syntactic sugar for:

Dim disposableObject As DisposableResource
Try
disposableObject.UseResource
Finally
If Not disposableObject Is Nothing Then
disposableObject.Dispose
End Try

Using is simply a cleaner way to guarentee that your resource will be
cleaned up - no matter how the block is exited.
 
Let me see if I understand this fully. Actually dimming an sql
connection uses no resources. The resources are acquired when .open or
..fill etc are used. .Close and .dispose (apparently are the same thing)
relinquish the resources but don't undim it therefore things like
mysqlconnection.connectionstring = ... still remain in memory and if
another open or fill is encountered new resources are acquired. Correct?
 
Hi Cj,

Yes, normally, the resource in your description means unmanaged resource
encapsulated by the managed object. The unmanaged resource includes file
handle, bitmap resource, network/sql connections, window handle etc... .Net
GC can use the metadata to know how to collect the managed object space on
the heap, however, it knows nothing about the unmanaged resource, so the
designer of the class needs to implement the IDispose interface to tell the
developer to release the unmanaged resource.

Regarding SqlConnection, its unmanaged resource is the connection to the
SQL Server and this connection will normally be started by Open method. So
SqlConnection.Open method will allocate the unmanaged resource, and the
calling to Dispose and Close method is required to release it. However,
SqlConnection.ConnectionString is a managed string object, which is known
to GC. So it has nothing to do with Dispose/Close method. It will be freed
by .Net GC when there is not references from root to point to the
SqlConnection.

Hope this is clear.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top