iPhone内存管理didReceiveMemoryWarning [英] iPhone Memory Management didReceiveMemoryWarning

查看:104
本文介绍了iPhone内存管理didReceiveMemoryWarning的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧......

我正在iPhone上实现一个简单的OpenGL ES应用程序,最近我在Pinch Media Analytics中添加了它.这样做有助于发现内存管理问题,但我不确定如何处理.

在一个完美的世界中,我的应用程序-在didFinishLoading中加载PNG和.CAF文件将启动,加载所有资源并运行正常.

但是,如果我的程序崩溃了(这是在集成Pinch Media库时发生的),或者如果我运行Safari并打开了一堆页面然后启动游戏,则游戏将崩溃回到菜单,因为它内存不足.

在我对系统进行硬重置之前,该问题将一直存在.

您要上网的默认答案是实现didReceiveMemoryWarning方法,如下所示....

- (void)didReceiveMemoryWarning
{ 
  // default behavior is to release the view if it doesn't have a superview.

  // remember to clean up anything outside of this view's scope, such as
  // data cached in the class instance and other global data.
  [super didReceiveMemoryWarning];
}

但是这并没有真正的帮助,因为其他程序正在占用内存而不是我的.我不想发表自己的看法,对吗?关于如何处理这种情况和/或didReceiveMemoryWarning事件中发生了什么,有很好的描述吗?

解决方案

如果您只有一个视图,那么唯一可以做的就是释放任何您不使用的数据,并在以后延迟加载它们. /p>

如果您有多个视图,那么如果它们不可见,则可能会释放它们.如果发生这种情况,将向相应的控制器发送setView:nil.我通过立即释放所有IBOutlet变量来处理这种情况,以便在再次从其xib加载视图时正确设置它们.

这是我在普通的非OpenGL ES应用程序中采用的方法,该应用程序具有> 6个​​视图,即使在导航视图中处于4级深度并且所有以前的控制器的视图都设置为nil时,该方法也能始终如一地工作-尽管向后重新加载视图会有所延迟,但向后导航不会导致崩溃.

如果尚未找到它,则在模拟器中有一个菜单项可以模拟内存警告,这比在真实设备中强制发生情况要容易.话虽如此,它并不能代替在真实设备中测试同一场景-只是使测试更容易.

Ok......

I'm implementing a simple OpenGL ES application on the iPhone and I recently added in Pinch Media Analytics. Doing this helped uncover a memory management problem, and I'm not entirely sure how to deal with it.

In a perfect world, my application - which loads PNGs and .CAF files in didFinishLoading will launch, load all of it's resources, and run just fine.

HOWEVER, if my program gets a crash (which happened as I was integrating the Pinch Media libraries) or if I run Safari and open a bunch of pages and then launch my game, the game will crash back to the menu because it is out of memory.

This problem will persist until I do a hard reset of the system.

The default answer that you sort of get online is to implement the didReceiveMemoryWarning method as listed below....

- (void)didReceiveMemoryWarning
{ 
  // default behavior is to release the view if it doesn't have a superview.

  // remember to clean up anything outside of this view's scope, such as
  // data cached in the class instance and other global data.
  [super didReceiveMemoryWarning];
}

But this doesn't really help as it's the other programs that are holding onto memory not mine. I don't want to release my own view do I? Is there a good description of how to handle this situation and / or what happens in the didReceiveMemoryWarning event?

解决方案

If you only have a single view, then the only thing you can do really is to release any data you are not using, and lazy load them later.

If you have more than a single view, then they might be released if they are not visible. If this happens, the corresponding controller will be sent setView: with nil. I handle this happening by immediately releasing all IBOutlet variables so they are set properly when the view is loaded from its xib again.

This is the approach I take in a normal, non-OpenGL ES application which has >6 views, and works consistently even when I am 4 levels deep in a navigation view and all previous controllers have their views set to nil - there is no crash when I navigate backwards, though there is a delay as views are reloaded.

If you haven't found it already, in the simulator there is a menu item to simulate a memory warning, which is easier than forcing the condition to occur in a real device. That said, it does not replace testing the same scenario in a real device - just makes testing easier.

这篇关于iPhone内存管理didReceiveMemoryWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆