Main Page | Report Page

 

  .NET DotNet Forum Index » VB.NET Forum (Visual Basic .NET) » how to use Using

Author Message
cj
Posted: Fri Jun 22, 2007 3:14 pm
 
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?
 
AlexS
Posted: Fri Jun 22, 2007 3:22 pm
 
You can nest using
You can use Using on connections, files and streams - on anything
implementing IDispose.

HTH
Alex

"cj" <cj@nospam.nospam> wrote in message
news:ejjKqnQtHHA.4572@TK2MSFTNGP02.phx.gbl...
Quote:
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?

 
cj
Posted: Fri Jun 22, 2007 4:01 pm
 
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:
Quote:
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?

 
Cor Ligthert [MVP]
Posted: Fri Jun 22, 2007 5:00 pm
 
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.nospam> schreef in bericht
news:%237oYxBRtHHA.4440@TK2MSFTNGP06.phx.gbl...
Quote:
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?

 
AlexS
Posted: Fri Jun 22, 2007 5:47 pm
 
Use nested Using:

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


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

HTH

"cj" <cj@nospam.nospam> wrote in message
news:%237oYxBRtHHA.4440@TK2MSFTNGP06.phx.gbl...
Quote:
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?

 
Herfried K. Wagner [MVP]
Posted: Fri Jun 22, 2007 6:57 pm
 
"cj" <cj@nospam.nospam> schrieb:
Quote:
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/>
 
Cor Ligthert [MVP]
Posted: Sat Jun 23, 2007 12:27 am
 
Rory,

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

http://groups.google.com/group/microsoft.public.dotnet.general/search?group=microsoft.public.dotnet.general&q=taskmanager+willy

Have a look at it,

Cor

"Rory Becker" <RoryBecker@newsgroup.nospam> schreef in bericht
news:b0ac48a0101c78c9833bedd66dc0@msnews.microsoft.com...
Quote:
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



 
cj
Posted: Mon Jun 25, 2007 8:06 am
 
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:
Quote:
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



 
Tom Shelton
Posted: Mon Jun 25, 2007 11:52 am
 
On Jun 25, 6:46 am, cj <c...@nospam.nospam> wrote:
Quote:
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
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Thu Feb 11, 2016 10:12 am