Memory leak on ASP.NET web site

J

Jon Davis

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 is accessed because the data
that's being stored is time relevant and the web app will be thrown out in a
few months. Since I try to separate the OleDb stuff from the business logic,
I just create new database connections and trust that those connections will
be closed and expunged when the response ends.

But I'm finding that between SQL Server and IIS the machine is quickly
running out of RAM, even when the session (where I'm caching data in a
custom user object) terminates. After 20 or so page hits (containing several
database hits per page hit) the server loses like 600MB or RAM. I wouldn't
care since the access load is trivial, except that this RAM is NEVER
re-gained. Resetting SQL Server returns about 200MB and then resetting IIS
returns about 400MB. Again, this is long after the sessions have terminated.
And I host nothing in the Application collection.

Am I wrong in my understanding that database connections automatically close
and are destroyed when the containing object destroys itself (when the web
page's response ends)?

I have tried forcing all new database connections to be kept in an ArrayList
in Global and then go through the ArrayList and close and remove all of the
connections when Global's EndRequest method executes, but this doesn't seem
to do anything for me memory-wise.

What is going on?!

Jon
 
J

Jon Davis

I have tried forcing all new database connections to be kept in an
ArrayList
in Global and then go through the ArrayList and close and remove all of the
connections when Global's EndRequest method executes, but this doesn't seem
to do anything for me memory-wise.

... Oh and it doesn't do anything because the Session object isn't exposed in
this method.

Jon
 
J

Jon Davis

I have tried forcing all new database connections to be kept in an
ArrayList
in Global

Oh, and I meant to say I keep them in the Session object, with the intent to
close them (and then remove them from the ArrayList) when the page ends.

How can I do this? The Session object isn't exposed in the Request_End
method in Global.

Jon
 
J

Jon Davis

Jon Davis said:
Oh, and I meant to say I keep them in the Session object, with the intent to
close them (and then remove them from the ArrayList) when the page ends.

How can I do this? The Session object isn't exposed in the Request_End
method in Global.

Nevermind, I already have all pages' code-behind inheret from a base class,
and I simply set up a deconstructor.

Will report here whether this (closing all open connections) clears the
memory leak.

Jon
 
J

Jon Davis

Will report here whether this (closing all open connections) clears the
memory leak.

No it does not. Anyone know what would cause such a drastic memory leak?
Again, lots of data is cached in the Session object, but even when the
Session has expired, no change is made to the memory and IIS and SQL Server
are taking up massive amounts of RAM.

Jon
 
J

Jon Davis

Actually, eventually I do now get some of the RAM back. But I still lose
about 25MB per session between SQL Server and IIS combined that is never
regained after the Session times out.

Jon
 
C

Chris Darnell

Jon, you should automatically assume that anything connecting to a database
is resource intensive. By that, I mean that it is going to take a fair
amount of processing and memory to perform and *maintain* that connectivity.
Therefore, it is common practice to only open any sort of database
connection at the point that it is needed and then release that connection
immediately after you are finished with it. Keeping the connection object
open during the entire session is definitely going to adversely affect the
memory usage of your application.

Additionally, you need to make sure that you are closing all DataReaders and
disposing of all DataSets, DataAdapters, Commands, etc.

Finally, are there other resources that you are using that are not being
closed or disposed?

Unfortunately, your statement that "time is to short to redeisgn" may be
irrelevant. If the site *has* to work, you may have to bite the bullet and
redesign how you are handling the connection... One thing you could do is
take a few pages a create a test site based on those few pages. First, make
sure that those pages continue to exhibit the same resource problems. Then,
alter just those few pages (and for goodness sake get that connection out of
the session) and see if that makes a difference in the resource usage.

Chris Darnell
 
J

Jon Davis

I only put the connections in the section because that is the shortest term
collection that I can keep around across all pages. I suppose I could put it
as a static class somewhere. Same difference, though, I am, storing every
connection in an arraylist, and at the termination of every page (through a
parent class) I go through the list and close and delete everything,
basically clearing out the arraylist.

It doesn't seem to change much.

Again I ask, don't Connections and DataReaders close automatically when
destroyed?

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top