我正在调用下面的方法,它使iOS内存从40MB上升到160MB,它不会再次下降。再次执行它会杀死应用程序,因为它是OOM。 我已经尝试了一切,但我看不出有问题。它正在调用一个返回大量数据(5MB到10MB)字符串的Web服务。我只想阅读它并将其存储在一个文件中。
public void GetDataStreamed ( int iID, string sFilename )
{
string sUrl = "somewebservice.com/somemethod" ;
WebRequest request = WebRequest.Create ( sUrl );
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "sSessionID=" + this.SessionID + "&iID=" + iID.ToString ( ) + "&iDepth=-1&iRootID=-1&aObjectTypes=2&aObjectTypes=3&aObjectTypes=4&aObjectTypes=6";
byte[] byteArray = Encoding.UTF8.GetBytes ( postData );
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
request.Timeout = 60000;
// Get the request stream.
using(Stream dataStream = request.GetRequestStream ( ))
{
// Write the data to the request stream.
dataStream.Write ( byteArray, 0, byteArray.Length );
// Close the Stream object.
dataStream.Close ( );
}
// Read response and stream into a file.
HttpWebResponse oResponse = ( HttpWebResponse ) request.GetResponse ( );
Stream oResponseStream = oResponse.GetResponseStream ( );
using ( StreamWriter oFileWriter = new StreamWriter ( sFilename ) )
{
using ( StreamReader oReader = new StreamReader ( oResponseStream ) )
{
while ( !oReader.EndOfStream )
{
string s = oReader.ReadLine ( );
oFileWriter.WriteLine ( System.Web.HttpUtility.HtmlDecode ( s ) );
oFileWriter.Flush();
}
oReader.Close ( );
}
oFileWriter.Close();
}
}
答案 0 :(得分:1)
我只是偶然发现了这篇文章,你可能已经想到了这一点。在您提供的代码片段中,您不会处置应使用块的以下对象:
一旦你离开了这个方法,它们使用的内存仍然被保留,不会被垃圾收集。
作为旁注,Dispose()会关闭连接,这是using语句的语法糖的原因之一,因此您不必记得调用close。如果除了另一个using语句之外没有其他正文,你也可以使用没有大括号的语句进行堆叠。这是一个快速而肮脏的重构示例,您提供了缺少的using语句。这应该可以纠正您遇到的内存泄漏。
public void GetDataStreamed(int iID, string sFilename)
{
string sUrl = "somewebservice.com/somemethod" ;
using (WebRequest request = WebRequest.Create (sUrl))
{
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "sSessionID=" + this.SessionID + "&iID=" + iID.ToString() + "&iDepth=-1&iRootID=-1&aObjectTypes=2&aObjectTypes=3&aObjectTypes=4&aObjectTypes=6";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
request.Timeout = 60000;
// Get the request stream.
using(Stream dataStream = request.GetRequestStream())
{
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
}
// Read response and stream into a file.
using(HttpWebResponse oResponse = (HttpWebResponse) request.GetResponse ( ))
using(Stream oResponseStream = oResponse.GetResponseStream ())
using(StreamWriter oFileWriter = new StreamWriter (sFilename))
using(StreamReader oReader = new StreamReader (oResponseStream))
{
while(!oReader.EndOfStream)
{
string s = oReader.ReadLine();
oFileWriter.WriteLine (System.Web.HttpUtility.HtmlDecode(s));
oFileWriter.Flush();
}
}
}
}
答案 1 :(得分:0)
如果只需要将其下载到文件中,请使用WebClient()。DownloadFile()代替。
答案 2 :(得分:-1)
您是否使用显式.Close()来确保关闭读者和作者?我认为Using {}为你做了那个?