Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

System.OutOfMemoryException

Status
Not open for further replies.

s2001

Programmer
Dec 7, 2001
132
0
0
US
Hello All,

I am getting the following error:
Exception of type 'System.OutOfMemoryException' was thrown.


I have the following recursive procedure:

private void doloop()
{
//i get the error here
string mytext=GetPages();

//call to another proc to do some work
ProcessText(mytext);

//call yourself again
doloop();
}

private string GetPages()
{
string htmlcontent;
_objWebRequest = WebRequest.Create(_intraurl);
_objWebRequest.Method = "GET";
_objWebResponse = _objWebRequest.GetResponse()
//other suspect
_htmlcontent = New System.IO.StreamReader(_objWebResponse.GetResponseStream).ReadToEnd;
_objWebResponse.Close();
return htmlcontent;
}

Error occurs after looping through like 100000 times or so. I have a 2GB Ram and when this error occurred i had used up like 981 MB.

Other question i had was does the variable 'string mytext' gets created each time i call doloop(). If that is the case, then if i make 'mytext' private variable will that help?

TIA

Thanks,
MB
 
I suspect you are not disposing all of you items properly, and they are taking up memory. I would set _objWebReference to null after you close it and set mytext to null before you doloop again. Also have the garbage collector collect before you run doloop again.
 
WebRequest and StreamReader expose IDisposable: you're creating them but not disposing them.

Code:
[b]//_objWebRequest = WebRequest.Create(_intraurl);[/b]
using ( WebRequest objWebRequest = WebRequest.Create(_intraurl) )
{
...
[b]//_htmlcontent = New System.IO.StreamReader(_objWebResponse.GetResponseStream).ReadToEnd;
[/b]
using ( StreamReader reader = new System.IO.StreamReader(_objWebResponse.GetResponseStream) )
{
 _htmlcontent = reader.ReadToEnd;
}
...
}


mr s. <;)

 
Thanks guys. I will implement the advice and get back to you.

Thanks,
MB
 
Thanks a log guys. That works. I went through all the object that expose IDisposable and disposed them.

Thanks,
MB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top