473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to use Using

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?
Jun 22 '07 #1
13 1358
You can nest using
You can use Using on connections, files and streams - on anything
implementing IDispose.

HTH
Alex

"cj" <cj@nospam.nospamwrote in message
news:ej**************@TK2MSFTNGP02.phx.gbl...
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?

Jun 22 '07 #2
cj
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 wrote:
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?
Jun 22 '07 #3
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" <cj@nospam.nospamschreef in bericht
news:%2****************@TK2MSFTNGP06.phx.gbl...
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 wrote:
>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?

Jun 22 '07 #4
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?

--
Rory

Jun 22 '07 #5
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

"cj" <cj@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
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 wrote:
>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?

Jun 22 '07 #6
"cj" <cj@nospam.nospamschrieb:
Can I nest using?
Yes. You can even write 'Using a, b, c, d ... End Using' instead of more
than one nested 'Using' block.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jun 23 '07 #7
On Jun 22, 2:14 pm, cj <c...@nospam.nospamwrote:
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...

--
Tom Shelton

Jun 23 '07 #8
Rory,

I have the idea that Willy has written about this subject the most in dept.

http://groups.google.com/group/micro...kmanager+willy

Have a look at it,

Cor

"Rory Becker" <Ro********@newsgroup.nospamschreef in bericht
news:b0**************************@msnews.microsoft .com...
>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?

--
Rory

Jun 23 '07 #9
cj
Thanks! One little followup. When you say I don't have to call
closing, is that because end using calls the cleanup method?

Tom Shelton wrote:
On Jun 22, 2:14 pm, cj <c...@nospam.nospamwrote:
>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...

--
Tom Shelton
Jun 25 '07 #10
cj
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.
Rory Becker wrote:
>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?

--
Rory
Jun 25 '07 #11
On Jun 25, 6:46 am, cj <c...@nospam.nospamwrote:
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.

--
Tom Shelton

Jun 25 '07 #12
cj
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?

Tom Shelton wrote:
On Jun 25, 6:46 am, cj <c...@nospam.nospamwrote:
>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.

--
Tom Shelton
Jun 25 '07 #13
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 26 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
3
by: Mike L | last post by:
Should the command call "using" be before or after my namespace? **AFTER** namespace DataGridBrowser { using System; using System.Drawing; using System.Drawing.Drawing2D; using...
3
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
8
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL...
0
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase...
10
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
0
by: Eugene Anthony | last post by:
The problem with my coding is that despite removing the records stored in the array list, the rptPages repeater control is still visible. The rptPages repeater control displayes the navigation...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.