我有一个使用MonoTouch开发的应用程序,它通过HTTP POST上传一个3.5MB的文件,在我测试的主要平台上(iPhone 3G和OS 3.1.2的iPhone 4和OS 4.2.1)非常不稳定。我会在这里描述我的做法,希望有人能告诉我是否有做错的地方。
为了排除我的应用程序中的其他因素,我将其简化为一个小型样例应用程序。该应用程序是一个iPhone OpenGL项目,仅执行以下操作:
1. 在启动时,以30k块分配6MB内存。这模拟了我的应用程序的内存使用情况。 2. 将一个3.5MB的文件读入内存。 3. 创建一个线程来发布数据。(创建一个WebRequest对象,使用GetRequestStream()方法,并将3.5MB的数据写入其中)。 4. 当主线程检测到发布线程完成后,转到步骤2并重复。
此外,每帧,我分配0-100k的内存来模拟应用程序执行某些操作。我不保留对此数据的任何引用,所以应该被垃圾回收。
iPhone 3G结果:该应用程序可以上传6到8次,然后操作系统将其关闭。没有崩溃日志,但有一个LowMemory日志显示该应用程序已被强制退出。
iPhone 4结果:第11次上传时出现Mprotect错误。
一些数据点:
1. Instruments不显示应用程序继续上传时内存的增加。 2. Instruments没有显示任何显着的泄漏(可能总共有1千字节)。 3. 无论是以64k块还是一次使用一个Stream.Write()调用写入POST数据都没有关系。 4. 在开始下一次上传之前等待响应(HttpWebRequest.HaveResponse)或不等待响应都没有关系。 5. POST数据是否有效都没有关系。我尝试过使用有效的POST数据,也尝试过发送3MB的零。 6. 如果应用程序在每个帧中没有分配任何数据,则需要更长时间才能耗尽内存(但如前所述,我每帧分配的内存在分配它的帧之后不再引用,因此应该被GC收回)。
如果没有人有任何想法,我将向Novell报告错误,但我想先看看我是否做错了什么。
如果有人需要完整的样例应用程序,我可以提供它,但我已经粘贴了我的EAGLView.cs文件的内容。
为了排除我的应用程序中的其他因素,我将其简化为一个小型样例应用程序。该应用程序是一个iPhone OpenGL项目,仅执行以下操作:
1. 在启动时,以30k块分配6MB内存。这模拟了我的应用程序的内存使用情况。 2. 将一个3.5MB的文件读入内存。 3. 创建一个线程来发布数据。(创建一个WebRequest对象,使用GetRequestStream()方法,并将3.5MB的数据写入其中)。 4. 当主线程检测到发布线程完成后,转到步骤2并重复。
此外,每帧,我分配0-100k的内存来模拟应用程序执行某些操作。我不保留对此数据的任何引用,所以应该被垃圾回收。
iPhone 3G结果:该应用程序可以上传6到8次,然后操作系统将其关闭。没有崩溃日志,但有一个LowMemory日志显示该应用程序已被强制退出。
iPhone 4结果:第11次上传时出现Mprotect错误。
一些数据点:
1. Instruments不显示应用程序继续上传时内存的增加。 2. Instruments没有显示任何显着的泄漏(可能总共有1千字节)。 3. 无论是以64k块还是一次使用一个Stream.Write()调用写入POST数据都没有关系。 4. 在开始下一次上传之前等待响应(HttpWebRequest.HaveResponse)或不等待响应都没有关系。 5. POST数据是否有效都没有关系。我尝试过使用有效的POST数据,也尝试过发送3MB的零。 6. 如果应用程序在每个帧中没有分配任何数据,则需要更长时间才能耗尽内存(但如前所述,我每帧分配的内存在分配它的帧之后不再引用,因此应该被GC收回)。
如果没有人有任何想法,我将向Novell报告错误,但我想先看看我是否做错了什么。
如果有人需要完整的样例应用程序,我可以提供它,但我已经粘贴了我的EAGLView.cs文件的内容。
using System;
using System.Net;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using OpenTK.Platform.iPhoneOS;
using MonoTouch.CoreAnimation;
using OpenTK;
using OpenTK.Graphics.ES11;
using MonoTouch.Foundation;
using MonoTouch.ObjCRuntime;
using MonoTouch.OpenGLES;
namespace CrashTest
{
public partial class EAGLView : iPhoneOSGameView
{
[Export("layerClass")]
static Class LayerClass ()
{
return iPhoneOSGameView.GetLayerClass ();
}
[Export("initWithCoder:")]
public EAGLView (NSCoder coder) : base(coder)
{
LayerRetainsBacking = false;
LayerColorFormat = EAGLColorFormat.RGBA8;
ContextRenderingApi = EAGLRenderingAPI.OpenGLES1;
}
protected override void ConfigureLayer (CAEAGLLayer eaglLayer)
{
eaglLayer.Opaque = true;
}
protected override void OnRenderFrame (FrameEventArgs e)
{
SimulateAppAllocations();
UpdatePost();
base.OnRenderFrame (e);
float[] squareVertices = { -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f };
byte[] squareColors = { 255, 255, 0, 255, 0, 255, 255, 255, 0, 0,
0, 0, 255, 0, 255, 255 };
MakeCurrent ();
GL.Viewport (0, 0, Size.Width, Size.Height);
GL.MatrixMode (All.Projection);
GL.LoadIdentity ();
GL.Ortho (-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
GL.MatrixMode (All.Modelview);
GL.Rotate (3.0f, 0.0f, 0.0f, 1.0f);
GL.ClearColor (0.5f, 0.5f, 0.5f, 1.0f);
GL.Clear ((uint)All.ColorBufferBit);
GL.VertexPointer (2, All.Float, 0, squareVertices);
GL.EnableClientState (All.VertexArray);
GL.ColorPointer (4, All.UnsignedByte, 0, squareColors);
GL.EnableClientState (All.ColorArray);
GL.DrawArrays (All.TriangleStrip, 0, 4);
SwapBuffers ();
}
AsyncHttpPost m_Post;
int m_nPosts = 1;
byte[] LoadPostData()
{
// Just return 3MB of zeros. It doesn't matter whether this is valid POST data or not.
return new byte[1024 * 1024 * 3];
}
void UpdatePost()
{
if ( m_Post == null || m_Post.PostStatus != AsyncHttpPostStatus.InProgress )
{
System.Console.WriteLine( string.Format( "Starting post {0}", m_nPosts++ ) );
byte [] postData = LoadPostData();
m_Post = new AsyncHttpPost(
"https://api-video.facebook.com/restserver.php",
"multipart/form-data; boundary=" + "8cdbcdf18ab6640",
postData );
}
}
Random m_Random = new Random(0);
List< byte [] > m_Allocations;
List< byte[] > m_InitialAllocations;
void SimulateAppAllocations()
{
// First time through, allocate a bunch of data that the app would allocate.
if ( m_InitialAllocations == null )
{
m_InitialAllocations = new List<byte[]>();
int nInitialBytes = 6 * 1024 * 1024;
int nBlockSize = 30000;
for ( int nCurBytes = 0; nCurBytes < nInitialBytes; nCurBytes += nBlockSize )
{
m_InitialAllocations.Add( new byte[nBlockSize] );
}
}
m_Allocations = new List<byte[]>();
for ( int i=0; i < 10; i++ )
{
int nAllocationSize = m_Random.Next( 10000 ) + 10;
m_Allocations.Add( new byte[nAllocationSize] );
}
}
}
public enum AsyncHttpPostStatus
{
InProgress,
Success,
Fail
}
public class AsyncHttpPost
{
public AsyncHttpPost( string sURL, string sContentType, byte [] postData )
{
m_PostData = postData;
m_PostStatus = AsyncHttpPostStatus.InProgress;
m_sContentType = sContentType;
m_sURL = sURL;
//UploadThread();
m_UploadThread = new Thread( new ThreadStart( UploadThread ) );
m_UploadThread.Start();
}
void UploadThread()
{
using ( MonoTouch.Foundation.NSAutoreleasePool pool = new MonoTouch.Foundation.NSAutoreleasePool() )
{
try
{
HttpWebRequest request = WebRequest.Create( m_sURL ) as HttpWebRequest;
request.Method = "POST";
request.ContentType = m_sContentType;
request.ContentLength = m_PostData.Length;
// Write the post data.
using ( Stream stream = request.GetRequestStream() )
{
stream.Write( m_PostData, 0, m_PostData.Length );
stream.Close();
}
System.Console.WriteLine( "Finished!" );
// We're done with the data now. Let it be garbage collected.
m_PostData = null;
// Finished!
m_PostStatus = AsyncHttpPostStatus.Success;
}
catch ( System.Exception e )
{
System.Console.WriteLine( "Error in AsyncHttpPost.UploadThread:\n" + e.Message );
m_PostStatus = AsyncHttpPostStatus.Fail;
}
}
}
public AsyncHttpPostStatus PostStatus
{
get
{
return m_PostStatus;
}
}
Thread m_UploadThread;
// Queued to be handled in the main thread.
byte [] m_PostData;
AsyncHttpPostStatus m_PostStatus;
string m_sContentType;
string m_sURL;
}
}