取消视图控制器后,是否应该清空内存? [英] When a view controller is dismissed, should it empty the memory?

查看:67
本文介绍了取消视图控制器后,是否应该清空内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解一下Objective-C中的内存管理,我发现这并不容易,因为我在Objective-C和ARC上还很陌生,而且我经常习惯于编写我没有的语言用那么多(或根本不做)内存管理.

I'd like to learn about memory management in Objective-C which I find not that easy because I'm fairly new at Objective-C and ARC and I'm mostly used to script languages for which I don't have to deal that much (or not at all) with memory management.

我正在处理的应用程序是在按下按钮后从代码中呈现一个viewController(附加了xib文件).在这个视图控制器中,我实例化了几个视图.我记录了一系列图像(来自相机的照片,保存到磁盘上),将其转换为电影,并且有一个gps跟踪器(mapKit),该跟踪器在屏幕上显示了一张小地图.完成所有操作后,我可以按完成"按钮,调用[self dismissViewControllerAnimated:YES completion:nil];

The app I'm working on is presenting a viewController (with xib file attached) from code after the press of a button. In this view controller I have several views instantiated; I record a sequence of images (photo's from the camera, saved to disk) which I convert to a movie and I have a gps tracker (mapKit) which display's a little map on screen. After all is done I can push a 'done' button which calls [self dismissViewControllerAnimated:YES completion:nil];

将viewController动画化为我的rootViewController,并且由于我在被关闭的viewController的dealloc方法内放置了NSLog消息,因此我可以确认此viewController已被释放.

The viewController is animated back to my rootViewController and because I put an NSLog message inside the dealloc method in the viewController that is being dismissed I can confirm this viewController is being deallocated.

问题是使用该应用程序后,我看到内存增加(用法包括在MapKit地图上拍照和记录gps位置以及生成电影文件)到大约80 MB,而现在下降到大约70MB当我按完成"时,viewController关闭,应用程序返回到我的rootViewController.我可以再次显示相同的viewController,使用并将其关闭,该应用程序仍将占用约70MB的内存,并且不会丢失.在我看来,这似乎并不像是内存泄漏,因为在那种情况下,我希望随着ViewController的每次实例化和释放,内存都将稳定增长.即使我的rootViewController中有不同的按钮都实例化了viewController类的新的唯一实例,情况并非如此.

The problem is that I see the memory rise after the use of the app (usage consists of taking pictures and recording gps locations on a MapKit map as well as generate a movie file) to about 80 MB and this drops to about 70MB when I press 'done' so the viewController dismisses and the app returns to my rootViewController. I can present the same viewController again, use it and dismiss it, and the app will still occupy around 70MB of memory which doesn't drop. This does not really look like a memory leak to me because in that case I would expect a steady rise of memory with every instantiation and dismissal of the viewController. This isn't the case even if I have different buttons in my rootViewController which all instantiate a new and unique instance of my viewController class.

我想知道:我应该寻找某些东西吗?或者这是预期的行为?也许该应用程序正在缓存类以供将来使用?在正确执行内存管理的情况下,我应该期望在取消显示的唯一viewController之后将应用恢复为原始"内存状态(在这种情况下,该状态约为4 MB)?

I am wondering: Is there something I should look for or is this expected behavior? Maybe the app is caching classes for future use? With memory management done right, should I be expecting an app to revert to 'virgin' memory state (in this case this would be around 4 MB) after dismissal of the only viewController that was presented?

推荐答案

如果您在Xcode中看到这种内存增加,而在使用工具时却没有看到,那么

If you are seeing this memory rise in Xcode and not when using instruments then the answer I have come up with is in this answer.

您可以阅读整个答案,它的深度很深.但总而言之,在Xcode中,您看到的是操作系统提供"给您的应用程序的内存量.每当您的应用尝试分配某些内容时,这都会增加.它的下降速度并没有那么快,因为操作系统确实会以为您的应用程序将来可能需要更多内存来猜测性能.对于操作系统而言,将内存提供"给您的应用程序要快于将其删除并稍后再提供.

You can read that whole answer, it's pretty in depth. But long story short, in Xcode you are seeing the amount of memory that the OS has "given" to your app to use. This will increase any time your app tries to allocate something. It does not decrease as fast because the OS does performance guesses thinking your app may need more memory in the future. It is faster for the OS to leave that memory "given" to your app than to take it away and give it back later.

这篇关于取消视图控制器后,是否应该清空内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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