Memory leak when using Response.Write()


P

Piotrek

Hi all.

I have a web app, in which I use frames. My main frameset consists of
three inner frames.

When some button is pressed in frame A, then content of frame B is
reloaded. I am using such code to achieve this:
string strRedirect;
strRedirect = "<script language='Javascript'>";
strRedirect += "parent.body.location.href='WFSearchResult.aspx';";
strRedirect += "</script>";
Response.Write(strRedirect);

I noticed that there are memory leaks in my app. I used .NET Memory
Profiler to trace them and I discovered that usage of memory increases
when content of some frame is loaded - I had a breakpoint on PageLoad
method in all frames, but usage of memory increasead after
Response.Write code and before PageLoad of frame.

So I think that old content of frame is not released and that is the
cause of memory leak.

Does anybody know some method to prevent memory leaks in situation I
described? Maybe there is some way I can get to the old frame and
dispose it?

Thanks in advance,
Piotrek
 
Ad

Advertisements

?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

I don't really see how there could be a memory leak from the code that
you've shown. It uses some memory, but it should all be returned when a
garbage collection occurs.

You can reduce the number of strings created from three to one:

string strRedirect;
strRedirect = "<script language='Javascript'>" +
"parent.body.location.href='WFSearchResult.aspx';" +
"</script>";
Response.Write(strRedirect);

This reduces the amount of memory the code uses. Not that it really
should make that much of a difference, though.
 
G

Guest

Piotrek,
I think your assumption that an ASP.NET application with frames, which are
strictly a client-side browser-handled construct, is fallacious. All the
ASP.NET page knows is that it is being reqeusted, it cares not whether it is
being loaded "into a frame" or not - it still does exactly the same work.
Peter
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mark said:
Three strings...?

Yes, three strings. The strings created in the code are:

"<script language='Javascript'>"

and

"<script
language='Javascript'>parent.body.location.href='WFSearchResult.aspx';"

and

"<script
language='Javascript'>parent.body.location.href='WFSearchResult.aspx';</script>"


Actually, it's more accurate to say that the code uses five strings;
three are constant strings and two are created by the code. The code I
proposed uses only one string, and it's constant so the code doesn't
create any string at all.
 
P

Piotrek

Thanks for your answers.

So if it is not a problem with frames, then I have no idea, what it can
be.

What more I noticed is that there are datasets (datacolumns,
dataviews), which are not collected by GC. I store results of search in
Session as datasets, but after a while I set them to null, so they
should be collected.

One more thing I do not understand is why usage of memory increases
after executing the code I posted earlier and before PageLoad method is
called.

I know that it is hard for you to answer that questions, but maybe you
know something, which could help me?

Piotrek.
 
Ad

Advertisements

?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

When you remove the reference to the dataset, it won't be collected
immediately. It will be collected when the garbage collector is run, and
that only happens when the memory is needed, or when the garbage
collector thinks that it's convenient to do a collection.

It's normal for the memory usage to increase while an application runs.
If your application uses and releases a lot of objects, you would
observe a sawtooth like curve where the memory usage increases to a
certain point until a garbage collection occurs and frees up a ot of
memory at once.
 
P

Piotrek

But memory is not released even when I explicitely call GC.Collect()
method.

I know that there should be sawtooth like curve. And it is like that
but with one exception: numer of live object instances is continously
getting bigger e.g. when my app. starts then there is about 100 live
objects. During work with the app this number is getting bigger to
let's say 500 objects. Then memory collection is called, but after it
there is still about 150 objects (50 more than there should be) - .NET
Memory Profiler says that there is a lot of strings, many DataColumns
and few DataTables and DataSets, which are not collected.

As a result IIS has to be periodically reseted to free all allocated
memory (even 5 times a day).
 
M

Mark Rae

Yes, three strings. The strings created in the code are:

"<script language='Javascript'>"

and

"<script
language='Javascript'>parent.body.location.href='WFSearchResult.aspx';"

and

"<script
language='Javascript'>parent.body.location.href='WFSearchResult.aspx';</script>"

Er, are you sure...?
Actually, it's more accurate to say that the code uses five strings; three
are constant strings and two are created by the code. The code I proposed
uses only one string, and it's constant so the code doesn't create any
string at all.

Er, OK...
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

There are two types of garbage collections, a complete collection, and a
generation zero collection that only collects objects that was recently
created and released.

Older objects aren't collected during a genaration zero collection, and
objects that have a finalizer will have to go through two collections
before before being collected, so it's not uncommon that some objects
that are collectable survives a collection.

The number of uncollected objects shouldn't grow substantially from
collection to collection, though. You should watch what kind of objects
it is that survives the collections, that should give you a hint of
where there might be a leak.
 
Ad

Advertisements

P

Piotrek

Objects, which survive collection are DataSets, DataTables, DataRows
and many Strings.
 
J

JT

I don't know if this would have a dramatic effect, but if you're using
DataTables, and reusing them instead of creating new ones, then in
addition to doing a [DataTable].Clear();, I would also do a
[DataTable].Columns.Clear();. I've found instances where row data was
removed, but there was still column data remaining. However, I think
you would have noticed that in the data you used and displayed. Good
luck.

JT
 
P

Piotrek

Hi JT

I am using both DataTable.Columns.Clear() and DataTable.Clear. With no
effects.

Piotrek
 
Ad

Advertisements

P

Piotrek

Hi.

I still haven't solved that problem.

I noticed that new dataset is created after every Response.Write, which
I use to load new page into some frame. Maybe these datasets are
created to store old pages? How can I release memory being held by
these pages before they are replaced by new ones?

I tried to use OnUnload, but with no effects.

Piotrek.
 

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

Top