473,406 Members | 2,633 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,406 software developers and data experts.

Naive memory management questions.

Hi
I've an asp.net, c#, sql server website and the volumes are starting to
rise quickly. I've been asked to look at improving the memory usage of
the application. I've not looked at this before so these are probably
naive questions:

1. If I close the sql connection every time do I need to close or
dispose of the dataset, command or any other objects?

2. Should I use data adaptor instaed of dataset?

3. Are there any other objects that I should close or dispose, or just
reply on the garbage collector?

4. How can I check for memory leaks and what are the most likely
culprits?

Any suggestions to get me up to speed in this would be greatly
appreciated.

John South
Pangbourne
www.WhereCanWeGo.com

Nov 17 '05 #1
9 1556
Some guidelines that I 'm trying to follow are these:

1. Use connection pooling. This way you can have a control on the
number of connections open without sacrificing much from your
performance

2. Fetch only the data you need.

3. Use a reader instead of a dataset whenever this is possible, ike
drop downs.

4. Try not to put objects in session or viewstate as this consumes
memory

5. In general you should dispose only objects that use unmanaged
resources (like connections). DataSets do not use unmanaged resources
and in fact they were designed to be used in a disconnected manner.

6. Use some kind of caching for data that is requested often and does
not change. This may sound stupid in situations like yours, but
consider that you can have only one copy of your data stored, without
having to fetch them from the server for every instance.

The garbage collector in general will not permit memory leaks ( only in
case of unmanaged resources that are not deallocated properly ) but it
can suffer from memory fragmentation. .NET garbage collector has
provisioning for that but a restart of the worker service will always
do good. There is a nice article about the garbage collector in MSDN

ms-help://MS.MSDNQTR.2005APR.1033/dndotnet/html/dotnetgcbasics.htm#dotnetgcbasics_topic3

Hope these will help.

Nov 17 '05 #2

"Tasos Vogiatzoglou" <tv*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Some guidelines that I 'm trying to follow are these:

1. Use connection pooling. This way you can have a control on the
number of connections open without sacrificing much from your
performance
Connection pooling is the default. No? If you're using SqlConnection I think
connection pooling happens automagically.
The garbage collector in general will not permit memory leaks ( only in
case of unmanaged resources that are not deallocated properly )


However, the garbage collector cannot prevent you from inadvertently
"hanging on" to objects you no longer need. Sounds silly, but can be a
problem in complex systems.
Nov 17 '05 #3

<Jo**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
1. If I close the sql connection every time do I need to close or
dispose of the dataset, command or any other objects?
As Tasos recommended, use SqlDataReader instead of DataSet. And yes, you
should close it when finished.
2. Should I use data adaptor instaed of dataset?
SqlDataReader is fastest for forward-only read-only access.
4. How can I check for memory leaks and what are the most likely
culprits?


Most likely culprits, stuff you put into Session and forgot about. Also,
objects where you manually wired up some events.

http://www.red-gate.com/products/ANT...iler/index.htm

http://www.red-gate.com/products/ants_load/index.htm

Nov 17 '05 #4
Connection pooling depends on the connection string you use. If you
want to setup connection pool you have to use the minPool, maxPool at
your connection string.

If there are objects that you do not need you should unreference them.
As a rule of thumb, you should create late, destroy early. If you
finished working with an object, dispose it.

Try also to minimize "elderly objects". .NET garbage collection is
using the notion of generations. Try to dispose objects as early as you
can and minimize references to older objects.

In general , if you don't use unmanaged resources, memory leaks should
be rare if not none. Just try not to create complicated object graphs
and if you have to create, use an appropriate dispose strategy.

Nov 17 '05 #5
> Connection pooling depends on the connection string you use. If you
want to setup connection pool you have to use the minPool, maxPool at
your connection string.
Can I have a reference for that?

--
Jonathan Allen
"Tasos Vogiatzoglou" <tv*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com... Connection pooling depends on the connection string you use. If you
want to setup connection pool you have to use the minPool, maxPool at
your connection string.

If there are objects that you do not need you should unreference them.
As a rule of thumb, you should create late, destroy early. If you
finished working with an object, dispose it.

Try also to minimize "elderly objects". .NET garbage collection is
using the notion of generations. Try to dispose objects as early as you
can and minimize references to older objects.

In general , if you don't use unmanaged resources, memory leaks should
be rare if not none. Just try not to create complicated object graphs
and if you have to create, use an appropriate dispose strategy.

Nov 17 '05 #6
MSDN Article for Connection pooling for SQL Server :

ms-help://MS.MSDNQTR.2005APR.1033/cpguide/html/cpconConnectionPoolingForSQLServerNETDataProvider. htm

Nov 17 '05 #7

"Tasos Vogiatzoglou" <tv*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Connection pooling depends on the connection string you use. If you
want to setup connection pool you have to use the minPool, maxPool at
your connection string.
The help topic for SqlConnection says:

"Note To deploy high-performance applications, you need to use connection
pooling. When you use the .NET Framework Data Provider for SQL Server, you
do not need to enable connection pooling because the provider manages this
automatically, although you can modify some settings. For more information
about using connection pooling with the .NET Framework Data Provider for SQL
Server, see Connection Pooling for the .NET Framework Data Provider for SQL
Server."

If you click the "Connection Pooling" link you get more text saying that it
happens automatically. The MinPool default is listed as "0" and the MaxPool
default is listed as "100".
If there are objects that you do not need you should unreference them.
As a rule of thumb, you should create late, destroy early. If you
finished working with an object, dispose it.


Right, but the non-intuitive (to me) referencing created as a result of
registering event handles sometimes prevents this.
Nov 17 '05 #8

"Tasos Vogiatzoglou" <tv*****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
MSDN Article for Connection pooling for SQL Server :

ms-help://MS.MSDNQTR.2005APR.1033/cpguide/html/cpconConnectionPoolingForSQLS
erverNETDataProvider.htm


I'm unable to get there. Could you post the pertinent text?
Nov 17 '05 #9

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

Similar topics

7
by: Dan Nilsen | last post by:
Hi! I'm writing a small piece of software that basically runs on an embedded system with a Power-PC cpu. This runs on a stripped down version of Linux - Busybox. As I'm writing a piece of...
1
by: trialproduct2004 | last post by:
Hi all, I am having slight confusion regarding memory management in .net. Say suppose i have two application one is in C# and other is in MFC(VC++). Both of this application are using lots...
5
by: RobbGMelenyk | last post by:
I've got a Windows Service written in C# that is having some unfortunate memory issues. I've been working with .NET MemProfiler and AllocationProfiler. But you don't have to use those programs to...
0
by: djpaul | last post by:
Hello! I am getting this error when i open a new form into antoher form. System.AccesViolationException - Attempted to read or write protected memory. What i do is : Dim PrintView as new...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.