473,385 Members | 1,919 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,385 software developers and data experts.

garbage collection

Just wondering if anyone has experienced any issues with garbage collection
in .net. We developed an application using VB .NET; many of the pages made
db calls.

One of the developers forgot to close the connection(s) and DataReader
objects. We found that once the site recieved a large number of hits, the
server died on us and memory usage was ridiculously high. Since that, we've
closed all the connections and datareaders and the app works great..

Should the .NET GC not have managed all of this for us?

BTW: I know its good coding practice to close everything you open.. but just
wondering IF GC does this or not..

Bijoy
Nov 19 '05 #1
8 1178

First of all, with datareaders, if you do not close the reader after the
usage, you can not reuse the connection on which it opened. This is not a
garbage collection issue, its a logical error. Garbage collection cleans the
resources for you, by doing automatic memory deallocation, but it doesnt
close the datareader for you. Thats your job. Also, If your application
doesnt allow database connection pooling, you need to dispose your database
connection. But, your application seems to be using the connection pooling,
so you do not have to worry. By the way, the reason you need to call dispose
on database connection is, it is unmanaged object. GC can not clean the
unmanaged resources for you
--
Kumar Reddi
http://kumarreddi.blogspot.com

"Bijoy Naick" <b_*****@yahoo.ca> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
Just wondering if anyone has experienced any issues with garbage collection in .net. We developed an application using VB .NET; many of the pages made
db calls.

One of the developers forgot to close the connection(s) and DataReader
objects. We found that once the site recieved a large number of hits, the
server died on us and memory usage was ridiculously high. Since that, we've closed all the connections and datareaders and the app works great..

Should the .NET GC not have managed all of this for us?

BTW: I know its good coding practice to close everything you open.. but just wondering IF GC does this or not..

Bijoy

Nov 19 '05 #2
Understood.. so what happens in the following case..

Assume that I have a custom class - call it Events. One of the methods of
this class is GetEvents defined/implemented as follows:

public function GetEvents(startDate, endDate) as sqlDataReader
Dim myReader As SqlDataReader
Dim oConn As SqlConnection

oConn.Open()
sql = "select * from events in date range"
myReader = oConn.ExecuteReader(sql)
oConn.Close()
return myReader
end function

If an aspx page creates an instance of the Events class and calls the
GetEvents method, it will get a reader back.. How can I close the reader
defined in the GetEvents method?

BTW: I took a look at the code in the Application Block provided by Msft
called SqlHelper. Their code doesnt close the reader either..

Bijoy

"Kumar Reddi" <Ku********@REMOVETHIS.gmail.com> wrote in message
news:OR**************@TK2MSFTNGP15.phx.gbl...

First of all, with datareaders, if you do not close the reader after the
usage, you can not reuse the connection on which it opened. This is not a
garbage collection issue, its a logical error. Garbage collection cleans the resources for you, by doing automatic memory deallocation, but it doesnt
close the datareader for you. Thats your job. Also, If your application
doesnt allow database connection pooling, you need to dispose your database connection. But, your application seems to be using the connection pooling, so you do not have to worry. By the way, the reason you need to call dispose on database connection is, it is unmanaged object. GC can not clean the
unmanaged resources for you
--
Kumar Reddi
http://kumarreddi.blogspot.com

"Bijoy Naick" <b_*****@yahoo.ca> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
Just wondering if anyone has experienced any issues with garbage

collection
in .net. We developed an application using VB .NET; many of the pages made db calls.

One of the developers forgot to close the connection(s) and DataReader
objects. We found that once the site recieved a large number of hits, the server died on us and memory usage was ridiculously high. Since that,

we've
closed all the connections and datareaders and the app works great..

Should the .NET GC not have managed all of this for us?

BTW: I know its good coding practice to close everything you open.. but

just
wondering IF GC does this or not..

Bijoy


Nov 19 '05 #3

You can not return reader after the connection is closed. Yes, Close()
method does close all the readers associated with the connections. Though
microsoft documentation does not say anything about it.

But to return a dataReader you need to keep the connection open. So, you
might be wondering how you are going to close the connection in the another
method. For this, you should create the dataReader using

ExecuteReader(CommandBehavior.CloseConnection);

so your reader should be created as
sql.ExecuteReader(CommandBehavior.CloseConnection) ;
Creating reader this way, would close the database connection associated
with the reader, when the reader itself is closed.

So, in your calling function, simply close the reader after you are done
with it. That would close the connection as well
--
Kumar Reddi
http://kumarreddi.blogspot.com

"Bijoy Naick" <b_*****@yahoo.ca> wrote in message
news:uw**************@TK2MSFTNGP14.phx.gbl...
Understood.. so what happens in the following case..

Assume that I have a custom class - call it Events. One of the methods of
this class is GetEvents defined/implemented as follows:

public function GetEvents(startDate, endDate) as sqlDataReader
Dim myReader As SqlDataReader
Dim oConn As SqlConnection

oConn.Open()
sql = "select * from events in date range"
myReader = oConn.ExecuteReader(sql)
oConn.Close()
return myReader
end function

If an aspx page creates an instance of the Events class and calls the
GetEvents method, it will get a reader back.. How can I close the reader
defined in the GetEvents method?

BTW: I took a look at the code in the Application Block provided by Msft
called SqlHelper. Their code doesnt close the reader either..

Bijoy

"Kumar Reddi" <Ku********@REMOVETHIS.gmail.com> wrote in message
news:OR**************@TK2MSFTNGP15.phx.gbl...

First of all, with datareaders, if you do not close the reader after the
usage, you can not reuse the connection on which it opened. This is not a
garbage collection issue, its a logical error. Garbage collection cleans

the
resources for you, by doing automatic memory deallocation, but it doesnt
close the datareader for you. Thats your job. Also, If your application
doesnt allow database connection pooling, you need to dispose your

database
connection. But, your application seems to be using the connection

pooling,
so you do not have to worry. By the way, the reason you need to call

dispose
on database connection is, it is unmanaged object. GC can not clean the
unmanaged resources for you
--
Kumar Reddi
http://kumarreddi.blogspot.com

"Bijoy Naick" <b_*****@yahoo.ca> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
Just wondering if anyone has experienced any issues with garbage

collection
in .net. We developed an application using VB .NET; many of the pages made db calls.

One of the developers forgot to close the connection(s) and DataReader
objects. We found that once the site recieved a large number of hits, the server died on us and memory usage was ridiculously high. Since that,

we've
closed all the connections and datareaders and the app works great..

Should the .NET GC not have managed all of this for us?

BTW: I know its good coding practice to close everything you open..

but just
wondering IF GC does this or not..

Bijoy



Nov 19 '05 #4
In this case you are handing ownership of the SqlDataReader over to
the caller of the method. After all, only the caller knows when they
are done with the reader and are ready to close it.

There is no ExecuteReader method on SqlConnection in 1.1. Is this 2.0
or a custom library?

The best practice when returning a SqlDataReader from a method is to
use:

= command.ExecuteReader(CommandBehavior.CloseConnect ion)

The above ensures that the underlying database connection is closed
when the SqlDataReader is closed.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 14 Dec 2004 15:15:23 -0500, "Bijoy Naick" <b_*****@yahoo.ca>
wrote:
Understood.. so what happens in the following case..

Assume that I have a custom class - call it Events. One of the methods of
this class is GetEvents defined/implemented as follows:

public function GetEvents(startDate, endDate) as sqlDataReader
Dim myReader As SqlDataReader
Dim oConn As SqlConnection

oConn.Open()
sql = "select * from events in date range"
myReader = oConn.ExecuteReader(sql)
oConn.Close()
return myReader
end function

If an aspx page creates an instance of the Events class and calls the
GetEvents method, it will get a reader back.. How can I close the reader
defined in the GetEvents method?

BTW: I took a look at the code in the Application Block provided by Msft
called SqlHelper. Their code doesnt close the reader either..

Bijoy

"Kumar Reddi" <Ku********@REMOVETHIS.gmail.com> wrote in message
news:OR**************@TK2MSFTNGP15.phx.gbl...

First of all, with datareaders, if you do not close the reader after the
usage, you can not reuse the connection on which it opened. This is not a
garbage collection issue, its a logical error. Garbage collection cleans

the
resources for you, by doing automatic memory deallocation, but it doesnt
close the datareader for you. Thats your job. Also, If your application
doesnt allow database connection pooling, you need to dispose your

database
connection. But, your application seems to be using the connection

pooling,
so you do not have to worry. By the way, the reason you need to call

dispose
on database connection is, it is unmanaged object. GC can not clean the
unmanaged resources for you
--
Kumar Reddi
http://kumarreddi.blogspot.com

"Bijoy Naick" <b_*****@yahoo.ca> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
> Just wondering if anyone has experienced any issues with garbage

collection
> in .net. We developed an application using VB .NET; many of the pagesmade > db calls.
>
> One of the developers forgot to close the connection(s) and DataReader
> objects. We found that once the site recieved a large number of hits,the > server died on us and memory usage was ridiculously high. Since that,

we've
> closed all the connections and datareaders and the app works great..
>
> Should the .NET GC not have managed all of this for us?
>
> BTW: I know its good coding practice to close everything you open.. but

just
> wondering IF GC does this or not..
>
> Bijoy
>
>



Nov 19 '05 #5
hmm.. interesting..

so if i close the reader in the aspx page, the reader in the class will also
get closed?
"Kumar Reddi" <Ku********@REMOVETHIS.gmail.com> wrote in message
news:Ov*************@TK2MSFTNGP12.phx.gbl...

You can not return reader after the connection is closed. Yes, Close()
method does close all the readers associated with the connections. Though
microsoft documentation does not say anything about it.

But to return a dataReader you need to keep the connection open. So, you
might be wondering how you are going to close the connection in the another method. For this, you should create the dataReader using

ExecuteReader(CommandBehavior.CloseConnection);

so your reader should be created as
sql.ExecuteReader(CommandBehavior.CloseConnection) ;
Creating reader this way, would close the database connection associated
with the reader, when the reader itself is closed.

So, in your calling function, simply close the reader after you are done
with it. That would close the connection as well
--
Kumar Reddi
http://kumarreddi.blogspot.com

"Bijoy Naick" <b_*****@yahoo.ca> wrote in message
news:uw**************@TK2MSFTNGP14.phx.gbl...
Understood.. so what happens in the following case..

Assume that I have a custom class - call it Events. One of the methods of
this class is GetEvents defined/implemented as follows:

public function GetEvents(startDate, endDate) as sqlDataReader
Dim myReader As SqlDataReader
Dim oConn As SqlConnection

oConn.Open()
sql = "select * from events in date range"
myReader = oConn.ExecuteReader(sql)
oConn.Close()
return myReader
end function

If an aspx page creates an instance of the Events class and calls the
GetEvents method, it will get a reader back.. How can I close the reader
defined in the GetEvents method?

BTW: I took a look at the code in the Application Block provided by Msft
called SqlHelper. Their code doesnt close the reader either..

Bijoy

"Kumar Reddi" <Ku********@REMOVETHIS.gmail.com> wrote in message
news:OR**************@TK2MSFTNGP15.phx.gbl...

First of all, with datareaders, if you do not close the reader after the usage, you can not reuse the connection on which it opened. This is not
a garbage collection issue, its a logical error. Garbage collection
cleans
the
resources for you, by doing automatic memory deallocation, but it
doesnt close the datareader for you. Thats your job. Also, If your application doesnt allow database connection pooling, you need to dispose your

database
connection. But, your application seems to be using the connection

pooling,
so you do not have to worry. By the way, the reason you need to call

dispose
on database connection is, it is unmanaged object. GC can not clean the unmanaged resources for you
--
Kumar Reddi
http://kumarreddi.blogspot.com

"Bijoy Naick" <b_*****@yahoo.ca> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
> Just wondering if anyone has experienced any issues with garbage
collection
> in .net. We developed an application using VB .NET; many of the pages made
> db calls.
>
> One of the developers forgot to close the connection(s) and
DataReader > objects. We found that once the site recieved a large number of hits, the
> server died on us and memory usage was ridiculously high. Since

that, we've
> closed all the connections and datareaders and the app works great..
>
> Should the .NET GC not have managed all of this for us?
>
> BTW: I know its good coding practice to close everything you open..

but just
> wondering IF GC does this or not..
>
> Bijoy
>
>



Nov 19 '05 #6
Yeah I noticed that Bijoy Naick was creating a datareader on the connection
object. I thought its a mistake while typing.

Hmm, so there is a ExecuteReader in .net 2.0 on connection object.
Interesting..
--
Kumar Reddi
http://kumarreddi.blogspot.com

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:o3********************************@4ax.com...
In this case you are handing ownership of the SqlDataReader over to
the caller of the method. After all, only the caller knows when they
are done with the reader and are ready to close it.

There is no ExecuteReader method on SqlConnection in 1.1. Is this 2.0
or a custom library?

The best practice when returning a SqlDataReader from a method is to
use:

= command.ExecuteReader(CommandBehavior.CloseConnect ion)

The above ensures that the underlying database connection is closed
when the SqlDataReader is closed.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 14 Dec 2004 15:15:23 -0500, "Bijoy Naick" <b_*****@yahoo.ca>
wrote:
Understood.. so what happens in the following case..

Assume that I have a custom class - call it Events. One of the methods of
this class is GetEvents defined/implemented as follows:

public function GetEvents(startDate, endDate) as sqlDataReader
Dim myReader As SqlDataReader
Dim oConn As SqlConnection

oConn.Open()
sql = "select * from events in date range"
myReader = oConn.ExecuteReader(sql)
oConn.Close()
return myReader
end function

If an aspx page creates an instance of the Events class and calls the
GetEvents method, it will get a reader back.. How can I close the reader
defined in the GetEvents method?

BTW: I took a look at the code in the Application Block provided by Msft
called SqlHelper. Their code doesnt close the reader either..

Bijoy

"Kumar Reddi" <Ku********@REMOVETHIS.gmail.com> wrote in message
news:OR**************@TK2MSFTNGP15.phx.gbl...

First of all, with datareaders, if you do not close the reader after the usage, you can not reuse the connection on which it opened. This is not a garbage collection issue, its a logical error. Garbage collection cleans
the
resources for you, by doing automatic memory deallocation, but it

doesnt close the datareader for you. Thats your job. Also, If your application
doesnt allow database connection pooling, you need to dispose your

database
connection. But, your application seems to be using the connection

pooling,
so you do not have to worry. By the way, the reason you need to call

dispose
on database connection is, it is unmanaged object. GC can not clean the
unmanaged resources for you
--
Kumar Reddi
http://kumarreddi.blogspot.com

"Bijoy Naick" <b_*****@yahoo.ca> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
> Just wondering if anyone has experienced any issues with garbage
collection
> in .net. We developed an application using VB .NET; many of the pages

made
> db calls.
>
> One of the developers forgot to close the connection(s) and DataReader > objects. We found that once the site recieved a large number of hits,

the
> server died on us and memory usage was ridiculously high. Since that,
we've
> closed all the connections and datareaders and the app works great..
>
> Should the .NET GC not have managed all of this for us?
>
> BTW: I know its good coding practice to close everything you open.. but just
> wondering IF GC does this or not..
>
> Bijoy
>
>

Nov 19 '05 #7

Well, I checked and it doesn't appear to be in 2.0 either:
http://msdn2.microsoft.com/library/492zc4xe.aspx

Perhaps the OP had a cut and paste error.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 14 Dec 2004 15:42:53 -0500, "Kumar Reddi"
<Ku********@REMOVETHIS.gmail.com> wrote:
Yeah I noticed that Bijoy Naick was creating a datareader on the connection
object. I thought its a mistake while typing.

Hmm, so there is a ExecuteReader in .net 2.0 on connection object.
Interesting..


Nov 19 '05 #8
my code was "pseudocodish".. not real code

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:9u********************************@4ax.com...

Well, I checked and it doesn't appear to be in 2.0 either:
http://msdn2.microsoft.com/library/492zc4xe.aspx

Perhaps the OP had a cut and paste error.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 14 Dec 2004 15:42:53 -0500, "Kumar Reddi"
<Ku********@REMOVETHIS.gmail.com> wrote:
Yeah I noticed that Bijoy Naick was creating a datareader on the connectionobject. I thought its a mistake while typing.

Hmm, so there is a ExecuteReader in .net 2.0 on connection object.
Interesting..

Nov 19 '05 #9

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

Similar topics

1
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and...
6
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
11
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
5
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000...
8
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by...
28
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will...
56
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application =...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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.