473,666 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1567
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.2005 APR.1033/dndotnet/html/dotnetgcbasics. htm#dotnetgcbas ics_topic3

Hope these will help.

Nov 17 '05 #2

"Tasos Vogiatzoglou" <tv*****@gmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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**********@g mail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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.goo glegroups.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.2005 APR.1033/cpguide/html/cpconConnection PoolingForSQLSe rverNETDataProv ider.htm

Nov 17 '05 #7

"Tasos Vogiatzoglou" <tv*****@gmail. com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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.goo glegroups.com.. .
MSDN Article for Connection pooling for SQL Server :

ms-help://MS.MSDNQTR.2005 APR.1033/cpguide/html/cpconConnection PoolingForSQLS
erverNETDataPro vider.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
1852
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 code that basically acts as a server and that will be running for weeks or months and probably even longer, memory management is a topic that is quite crucial.
1
2794
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 of memory. Suppose i run first C# application which has occupied all memory and
5
2679
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 notice the memory leak. The memory usage in the TaskManager for the process can be seen climbing and climbing, MBs at a time. When using MemProfiler, I noticed the Win32 Heap is what seems to be growing. I've been searching on the internet...
0
963
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 PrintForm() PrintView.ReportName = "SaleView" PrintView.GetPaymentChoice = Data PrintView.GetQuery = Query PrintView.Show(Me)
0
8445
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8640
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.