iPhone 4.0 上的应用程序在发布后使​​用 UIImageview 使用的内存 [ Real RAM ]

发布于 2024-09-15 21:47:51 字数 814 浏览 3 评论 0原文

我有两个查询

  1. 我正在运行一个示例应用程序,其中实现了单个视图控制器。当我使用 Instrument 检查内存时,它显示 3.66 MB 。想知道为什么它会占用如此多的 RAM,因为应用程序中没有什么繁重的东西。

  2. 当我添加 UIImageview 且图像大小为 25 KB 时,内存使用量变为 4.24 MB
    [我开始知道背后的原因是“图像已解压320*480*4 = 580 KB”,但需要对此进行更多调试&它保留在缓存中]

在此期间我还观察到了两种情况

  1. 当我们使用 api [UIImage imageNamed:aName] 加载图像,然后调用 [ UIImageviewrelease]没有任何效果。

  2. 但是当我们使用

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:aName ofType:nil]];

当我调用时,一些内存被清理>[UIImageview release]

在我的应用程序中,我将使用大量图像,这将导致内存不足并使应用程序崩溃。

请为我提供一些参考或路径来分析为什么会出现这种行为。

谢谢,

萨加尔

I have two queries

  1. I was running a sample app which having single view controller in it implemented. When I check the memory using Instrument it was showing 3.66 MB
    . Wondered why it is taking so much of RAM as there is nothing much heavy in app.

  2. When I have added UIImageview with the Image having size of 25 KB,then Memory uses go to 4.24 MB
    [ I come to know the reason behind is "image is unpacked 320*480*4 = 580 KB" but need to debug more on this & it remains in cache ]

Along this I have also observed two scenarios

  1. When we uses api [UIImage imageNamed:aName] for loading image, then calling [UIImageview release] doesn't have any effect.

  2. But When we use

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:aName ofType:nil]];

Some memory is cleaned up when I call [UIImageview release]

In my app , I am going to use lot of images and that will cause a low memory and will crash the app.

Please provide me some ref or path to analyse why this behavior.

Thanks,

Sagar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习ぎ惯性依靠 2024-09-22 21:47:51

试图让你的应用程序适合内存是一场失败的游戏。它会引导你走上奇怪的道路,试图找出你正在运行的设备类型,根据该设备换入和换出资源,等等。

更好的选择是设计你的内存结构以实现可抛弃性,然后当您收到内存不足的通知时,支持相当严厉的放弃方案。继续使用内存——它就在那里——并让低内存警告成为你删除未使用资源的信号。

很多人似乎对他们的应用程序生成内存警告感到难过。那太愚蠢了。这里的设计模式是,想吃多少就吃多少,但当你被告知自己超重时,要做出适当的反应。鉴于您运行的设备种类繁多,内存配置也各不相同(例如,iPhone 3G 的 RAM 是 iPhone 4 的 1/4),最好的方法就是对内存已满的时间保持敏感。记忆。

您将遇到的主要障碍是从丢弃的数据中恢复。我发现最有效的方法是将 UIImage 对象显式设置为 nil,然后在使用它们之前测试 nil,从捆绑包或网络或其他任何必要的地方重新加载它们。

综上所述:[UIImage imageNamed:] 支持可丢弃性,但您无法控制它。当 UIViewController 子类收到内存警告时,它将丢弃您使用该方法创建的缓存 UIImage,但在此之前您无法做任何事情让它们消失。即使为有问题的 UIImage 分配一个很小的东西也无济于事,因为它是与它“命名”的“名称”相关联的缓存,而不是与它分配给的对象相关联。因此,这种方法对于您将要大量重复使用的图像很有用,但即便如此,到时候它也会被修剪,您需要做出适当的响应。

Trying to fit your app in memory is a losing game. It'll lead you down weird paths of trying to figure out which sort of device you're running on, swapping in and out resources based on that, etc, etc.

The better option is to design your memory structure for ditchability, and then support a fairly harsh ditching regimen when you're notified of low memory conditions. Go ahead and use memory--it's there for that--and let the low memory warning be your signal to prune out unused resources.

A lot of people seem to feel bad that their application generates memory warnings. That's silly. The design pattern here is, eat all you want, but respond appropriately when you're told you're overweight. Given you're running on a broad range of devices with a broad range of memory profiles (an iPhone 3G has 1/4 the RAM of an iPhone 4 for instance), the best way is just to be sensitive to when you've filled memory.

The main hurdle you'll encounter is recovering from having ditched data. I find what works best is to explicitly set UIImage objects to nil, and then test for nil before using them, reloading them from the bundle or the network or whatever if necessary.

All that said: [UIImage imageNamed:] supports ditchability, but you don't have control over it. When a UIViewController subclass gets a memory warning, it will ditch cached UIImages that you've created with that method, but nothing you can do will make them go away until then. Even assigning a tiny something to the UIImage in question won't help because it's cached associated with the "name" that it's "Named", not the object that it is assigned to. So that method is good for images you're going to reuse a lot, but even so, it will get pruned when the time comes, and you need to respond appropriately.

信仰 2024-09-22 21:47:51

使用 imageNamed 加载的图像由 UIKit 缓存在内存中,而使用 imageWithContentsOfFile 加载的图像则不会。

Images, loaded with imageNamed, are cached in memory by UIKit, and images, loaded with imageWithContentsOfFile are not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文