HttpWebRequest Memory release

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using Visual Studio 2003 with .NET Framework 1.x on XP SP2.
I am executing the following code (see also
http://dturini.blogspot.com/2004/06/on-past-few-days-im-dealing-with-som
e.html)

HttpWebRequest wr =
(HttpWebRequest)WebRequest.Create("http://www.microsoft.com/");
using (HttpWebResponse wrp = (HttpWebResponse)wr.GetResponse())
{
using (Stream s = wrp.GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
string st = sr.ReadToEnd();
sr.Close();
s.Close();
}
}
}
wr = null;

This call to that list of instructions brings an increase of memory of 4
Mo
(this memory is seen from the task manager concerning this application)
Is-it fixed in the .Net Framework Version 2.0? It seems impossible to
release the memory taken by this list of instructions.
 
Marc said:
I am using Visual Studio 2003 with .NET Framework 1.x on XP SP2.
I am executing the following code (see also
http://dturini.blogspot.com/2004/06/on-past-few-days-im-dealing-with-som
e.html)

HttpWebRequest wr =
(HttpWebRequest)WebRequest.Create("http://www.microsoft.com/");
using (HttpWebResponse wrp = (HttpWebResponse)wr.GetResponse())
{
using (Stream s = wrp.GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
string st = sr.ReadToEnd();
sr.Close();
s.Close();
}
}
}

There's no need to call sr.Close() or s.Close() when you're using a
"using" block.
wr = null;

You don't need to do that - it'll only make a difference in debug mode.
This call to that list of instructions brings an increase of memory of 4
Mo
(this memory is seen from the task manager concerning this application)
Is-it fixed in the .Net Framework Version 2.0? It seems impossible to
release the memory taken by this list of instructions.

Note that a lot of the handles you talk about in your blog are
*deliberately* not cleaned up - one web request to MS may well be
followed by another, so the connection may be left open, for instance.

Two things about the way you're thinking about memory:
1) The CLR doesn't release the memory back to the operating system
after using it - it reuses it itself instead.
2) Task manager is a notoriously bad way of looking at memory - it just
reports the working set
 
Concerning my previous post (thank you for the answer to my previous question),
I have another questions:

What will happen if I have over 20 objects httpwebrequest running in
parallelle?
The memory used will be huge? How can I close the connection (or close
definitively the httpwebrequest?) How can I see the real memory used by the
application?

I trace also the release of objects with the rational software Purify.
If I use objects webproxy and network credential objects, these objects are
not released even it is written webproxy = null and networkcredential= null.
Do you know how to suppress from the memory also?
 
Marc said:
Concerning my previous post (thank you for the answer to my previous question),
I have another questions:

What will happen if I have over 20 objects httpwebrequest running in
parallelle?

If they're going to the same host, they probably won't actually run in
parallel - there's a limit (in machine.config) of the number of web
requests going to the same host.
The memory used will be huge?

Not necessarily. Bear in mind that not all the memory you're seeing
being allocated is necessarily *used*. The CLR may choose to request
4MB from the OS, even if it doesn't need all of it yet.
How can I close the connection (or close
definitively the httpwebrequest?)

I don't know any way of doing that, although it may be possible.
How can I see the real memory used by the application?

Use a profiler.
I trace also the release of objects with the rational software Purify.
If I use objects webproxy and network credential objects, these objects are
not released even it is written webproxy = null and networkcredential= null.
Do you know how to suppress from the memory also?

They're probably cached somewhere too - presumably to identify the
connection in the cache.
 
Back
Top