简体   繁体   English

如何限制iOS应用程序的内存使用量

[英]How to limit iOS app memory usage

Ok so this is a pretty vague question but let me give you some background: 好的,这是一个非常模糊的问题,但是让我给您一些背景知识:

I have a straight forward app so far - about 5 screens each of which will hit the database and display some information on the resultant view. 到目前为止,我有一个简单的应用程序-大约有5个屏幕,每个屏幕都会进入数据库并在结果视图上显示一些信息。 The objects aren't huge , about 15 attributes some of which would be NSSets with 3-4 objects contained. 对象不是很大 ,大约有15个属性,其中一些是包含3-4个对象的NSSet。

I've noticed that by the time I'm on my 5th screen, memory has jumped to nearly 6mb which seems huge to me (though I could be wrong). 我注意到,当我进入第5个屏幕时,内存已跃升至近6mb,这对我来说似乎是巨大的(尽管我可能错了)。

I do notice a 1mb jump every time I do a DB call like NSFetchRequest which uses the usual NSManagedObjectContext, NSPersistentStoreCoordinator, etc as you'd expect. 我确实注意到,每次执行NSFetchRequest之类的数据库调用时,都会出现1mb的跳跃,该调用使用了您所期望的常规NSManagedObjectContext,NSPersistentStoreCoordinator等。

Do I need to close the context when I'm done with the DB or something like that? 完成数据库之类的工作后,是否需要关闭上下文? I remember such Persistence Contexts causing huge memory problems back in my Java days and since there's nothing else going that (I believe) can really be taxing the app thats where I'd guess the issue is. 我记得这样的持久性上下文在我的Java时代曾导致巨大的内存问题,并且由于没有其他事情(我相信)真的可以使我认为问题出在哪里的应用程序负担更多。

I'm using ARC and CoreData. 我正在使用ARC和CoreData。

Any insight into this would be really appreciated. 任何对此的见解将不胜感激。

Update 更新

So I used Instruments as recommended (great tool btw) and it looks as though the problem is caused by one screen with about 30-40 UILabels (resulting in a use of about 4MB). 因此,我按照推荐的方式使用了Instruments(最好的工具btw),看起来问题似乎是由一个带有30-40个UILabel的屏幕引起的(导致使用了大约4MB)。 Seems like a lot of memory for UILabels or am I completely off? 似乎UILabel的内存很大,还是我完全关闭了?

Every UIView has a CALayer . 每个UIView都有一个CALayer So every view ends up caching its contents. 因此,每个视图最终都会缓存其内容。 Supposing your 30–40 labels ended up filling the screen and you were on an iPhone 4 then you'd expect them to have a memory footprint of at least 960*640*4 = 2.4mb. 假设您的30–40个标签最终填满了屏幕,并且您使用的是iPhone 4,那么您希望它们的内存占用量至少为960 * 640 * 4 = 2.4mb。 I'm willing to bet they occupy more room than that. 我敢打赌他们会占据更多的空间。

If you're suffering from it, ignore your instinct to think of that as a huge waste, because: 如果您正遭受痛苦,请忽略您的直觉,将其视为巨大的浪费,因为:

  • under iOS 5 and earlier, if a memory warning occurs then any off-screen view controllers will release their views; 在iOS 5及更低版本中,如果发生内存警告,则所有屏幕外视图控制器都会释放其视图; and
  • under iOS 6 (and presumably onward), if a memory warning occurs then the layers attached to all off-screen views release their contents. 在iOS 6(可能是向前)上,如果发生内存警告,则附加到所有屏幕外视图的图层将释放其内容。

So it's usually the case that the memory is spent only while it's available — it's a simple matter of not ignoring a resource just because it may not always be available. 因此,通常情况下,仅在内存可用时才使用内存–这是一个简单的问题,不要仅仅因为它可能并不总是可用就忽略资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM