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

How is memory managed for Database connections and DataReaders

Assume i have a db connection and a datareader for that connection. Now I
perform a query that retrieves 100MB of data. I know that the data reader
gets 1 row at a time. However, there is a result set out there that is
100MB is size.

If I close the dataReader but keep the DB connection open, will the 100MB be
freed up after closing the DataReader?

Can I continue reusing the same DB connection and creating and destroying
datareaders assigned to that connect and trust that the memory after the
datareader close will be freed?

Thanks
Mar 1 '07 #1
4 4676
On Mar 1, 10:14 am, "Mike" <gacracker...@hotmail.comwrote:
Assume i have a db connection and a datareader for that connection. Now I
perform a query that retrieves 100MB of data. I know that the data reader
gets 1 row at a time. However, there is a result set out there that is
100MB is size.

If I close the dataReader but keep the DB connection open, will the 100MB be
freed up after closing the DataReader?

Can I continue reusing the same DB connection and creating and destroying
datareaders assigned to that connect and trust that the memory after the
datareader close will be freed?

Thanks
The rule for me is usually is: If Displose() method exists on the
objects, use it by all means. Otherwise, trust that memory will be
managed by .Net.

Yes, you can continue to use the connection, but best practice is
don't reuse it and let the connection pool does it magic work.
Typically, do this in your code:

private void f()
{
using (SqlConnection conn = new SqlConnection())
{
// Use conn in here
}
}

* note: code typed up from memory and didn't verify for syntax

Quoc Linh

Mar 1 '07 #2

If you use a datareader then it theoretically only reads one row at a
time. So the entire 100mb will not be in memory in your .NET
application in the first place.

The data reader may do some prefetching depending on the
implementation, the socket connection may do some buffering, and the
database most likely will do caching, so more than one record will be
in memory, but it's generally safe to think of a datareader as a
one-record-in-memory data access method.

When you close the data reader the one record it cached will be
released and available for collection. The cursor in the db will also
be released so the db can release whatever memory it was using.

Yes you can continue using that connection and not worry about memory
management in .NET. Just make sure you use the using() {} pattern
every time so you are sure to close the readers even on error. Don't
hold open the connection longer than needed though--connection pooling
makes resuing connections less important than would otherwise be the
case.

Note that a DataSet / DataTable would hold the entire result set in
memory and keep it in memory until all references are removed (DataSet
does have Dispose(), but it's only there due to it's object hierarchy
and it's Dispose method actually doesn't do anything).

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 1 Mar 2007 13:14:40 -0500, "Mike" <ga**********@hotmail.com>
wrote:
>Assume i have a db connection and a datareader for that connection. Now I
perform a query that retrieves 100MB of data. I know that the data reader
gets 1 row at a time. However, there is a result set out there that is
100MB is size.

If I close the dataReader but keep the DB connection open, will the 100MB be
freed up after closing the DataReader?

Can I continue reusing the same DB connection and creating and destroying
datareaders assigned to that connect and trust that the memory after the
datareader close will be freed?

Thanks
Mar 1 '07 #3
I am currently using a .NET OLEDB provider which does not support connection
pooling. If i were using an ADO.NET data provider which supports connection
pooling would this be a better solution than managing connections myself? I
need to speed things up as much as possible.

Thanks
"Samuel R. Neff" <sa********@nomail.comwrote in message
news:c6********************************@4ax.com...
>
If you use a datareader then it theoretically only reads one row at a
time. So the entire 100mb will not be in memory in your .NET
application in the first place.

The data reader may do some prefetching depending on the
implementation, the socket connection may do some buffering, and the
database most likely will do caching, so more than one record will be
in memory, but it's generally safe to think of a datareader as a
one-record-in-memory data access method.

When you close the data reader the one record it cached will be
released and available for collection. The cursor in the db will also
be released so the db can release whatever memory it was using.

Yes you can continue using that connection and not worry about memory
management in .NET. Just make sure you use the using() {} pattern
every time so you are sure to close the readers even on error. Don't
hold open the connection longer than needed though--connection pooling
makes resuing connections less important than would otherwise be the
case.

Note that a DataSet / DataTable would hold the entire result set in
memory and keep it in memory until all references are removed (DataSet
does have Dispose(), but it's only there due to it's object hierarchy
and it's Dispose method actually doesn't do anything).

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 1 Mar 2007 13:14:40 -0500, "Mike" <ga**********@hotmail.com>
wrote:
>>Assume i have a db connection and a datareader for that connection. Now I
perform a query that retrieves 100MB of data. I know that the data reader
gets 1 row at a time. However, there is a result set out there that is
100MB is size.

If I close the dataReader but keep the DB connection open, will the 100MB
be
freed up after closing the DataReader?

Can I continue reusing the same DB connection and creating and destroying
datareaders assigned to that connect and trust that the memory after the
datareader close will be freed?

Thanks

Mar 2 '07 #4

Before switching to improve performance always check to see if the
thing you think is causing an issue really is.

I'm working with a ADO.NETfor a different database (SQLite) that also
does not support connection pooling and there was some discussion as
to whether connection pooling would be beneficial for this database.
It turned out that the answer depended a lot on the schema. For a
small size database the cost of opening a connection was under a
millisecond which made pooling useless. However, for my more medium
sized schema the cost is 17ms which made pooling essential.

So I can't give you any specific advice, especially without knowing
what db you're using and what other provider options you have, other
than to say don't change anything without being sure it's a change for
the better through testing and don't assume anything is a performance
problem (or isn't) until it's tested.

Good luck,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Fri, 2 Mar 2007 11:24:34 -0500, "Mike" <ga**********@hotmail.com>
wrote:
>I am currently using a .NET OLEDB provider which does not support connection
pooling. If i were using an ADO.NET data provider which supports connection
pooling would this be a better solution than managing connections myself? I
need to speed things up as much as possible.

Thanks
Mar 2 '07 #5

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

Similar topics

1
by: Vladimir | last post by:
I created a windows service that performs a MSSQL2000 database analysis in a separate threads with a specified interval. And when running this service uses a lot of system memeory and it does...
1
by: Gaël | last post by:
Hi everybody! I have a really big problem with ASP.NET application. I noticed that the w3wp.exe memory size, increase with the time and the use of my website. When it raise a certain value, w3wp...
4
by: Chris | last post by:
Hi, I have an asp.net site that receives a medium amount of traffic (500-1k users per day). I'm having massive trouble at the moment with a memory leak that I just cannot isolate. The server is...
7
by: Jon Davis | last post by:
OK I have a web app that I built that makes MANY calls to the DB in each request. The app wasn't tuned for scalability so this wasn't a problem, but time is too short to redesign how the database...
19
by: Jon Davis | last post by:
I'm reposting this because I really need some advice. I have a web app that makes many queries to the database on every page. In order to save development effort, I've consolidated all database...
4
by: DevinG | last post by:
I would like to first start off saying that this is pretty much my first week of heavily getting into ASP.net. I previously have programmed in ASP Classic, and now understanding the benefits of...
6
by: Tina | last post by:
My asp.net app that ran fine on my dev boxes is having problems at my web hoster who is running IIS 6. I'm getting Out of Memory exceptoions. When my web hoster bounces my app, the problem goes...
4
by: Sean | last post by:
I am a little bit confused about memory clean up in VB.NET. (Damn I never thought I'd miss News and Deletes from VC++, but at least I was sure there). If I have some code like: Public...
3
by: Adam Clauss | last post by:
We have an application which is fairly network intensive. During some stress testing, we had it setup to open approximately 300-400 TCP connections (outbound connections, we are a TCP client). ...
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: 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.