正确加载和处理 XML,不会浪费内存
我正处于调整我的第一个 iPhone 应用程序以供发布的最后阶段,并且正在努力尽可能地减少千字节。我有一个在应用程序启动时与我的服务器同步一些数据的进程,我注意到当我注释掉时,我的应用程序在启动完成后使用了 7MB。当我打开它时,它在启动完成后使用了 18MB。我现在正在尝试确定该过程的哪一部分正在消耗内存而不将其返还。我已经关闭了大部分同步功能,只剩下这个,它仍然使用 2MB 内存,并且在完成后不会释放它:
GDataXMLDocument *syncData = [[self getXmlWithUrl:@"http://SOMEURL"] autorelease];
这只是使用我的辅助函数来加载一个 xml 文档供我使用。我的辅助函数如下:
-(GDataXMLDocument*)getXmlWithUrl:(NSString*)url{
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
return [[GDataXMLDocument alloc]initWithData:response options:0 error:&err];
}
我在创建syncData之后发布了一个release,但当然它说它已经被解除分配。关于可能导致这种情况的原因有什么想法吗?
I am in the final stages of tweaking my first iphone app for release and am trying to shave off kilobytes where I can. I have a process that syncs some data with my server on start of the app, and I noticed when I commented that out, my app is using 7MB when it is done starting. When I turn it on, it is using 18MB when it is done starting. I am now trying to determine what part of the process is eating the memory and not giving it back. I have turned off most of my sync function and am left with this and it still uses 2MB of memory and does not release it when it is done:
GDataXMLDocument *syncData = [[self getXmlWithUrl:@"http://SOMEURL"] autorelease];
This just uses my helper function to go out and load up a xml document for me to use. My helper function is as follows:
-(GDataXMLDocument*)getXmlWithUrl:(NSString*)url{
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
return [[GDataXMLDocument alloc]initWithData:response options:0 error:&err];
}
I put a release after the syncData is created, but of course it says its already de-allocated. Any ideas on what could be causing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个想法。
GDataXMLDocument 是基于 DOM 的解析。你的反应有多大?
通常,DOM 解析需要 RAM 中原始文档大小的 2-4 倍
使用某些文本编码可能会更多。你真的需要 DOM 解析吗?我们尝试使用 SAX 完成所有工作,因此
渐进式且内存效率更高。
您正在使用自动释放,因此内存不会被释放,直到
自动释放池被清空。您可以用 auto 包装代码
释放池,然后处理它。
http://developer.apple.com/library /mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html
最后,同步 HTTP 请求通常不是一个好主意因为网络的性能变化很大,您将阻塞 UI 线程。
Two thoughts.
GDataXMLDocument is a DOM based parse. How big is your response?
Typically DOM parses need 2-4x of the original document size in RAM
can be even more with certain text encodings. Do you really need DOM parsing? We try and do all of our stuff using SAX so it's
progressive and much more memory efficient.
You're using autorelease, so memory won't be released until the
autorelease pool is emptied. You could wrap the code with an auto
release pool and then dispose of that.
http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html
Finally, synchronous HTTP requests are generally a bad idea because the performance of the network can be so variable and you'll block the UI thread.