看到 malloc 分配大块内存 - 试图找出原因 (iPhone)
我看到我的应用程序被 iOS 杀死并显示内存不足消息,但是,在分配工具中跟踪应用程序的进度时,我看到许多 malloc 似乎发生在我编写的代码之外。
我没有看到任何泄漏,所以我认为这些分配应该存在。问题是,因为我不确定为什么分配它们,所以我不确定我可以做些什么来优化应用程序并防止操作系统抛弃我的应用程序。
有谁知道为什么分配内存,或者有什么办法让我找出答案?
以下是 Instruments 中显示 malloc 的几张照片。在第二个镜头中,所有分配都具有相同的堆栈跟踪。
编辑
我'显示一个大图像作为 UIView 背景(1024x768),然后用一些自定义绘图覆盖一个较小的(600 像素正方形)UIView 和第三个 UIView (550 像素正方形)覆盖在那些顶部包含两个重叠的 550px 方形图像。
我猜这是不合适的,并且可能有更好的方法来实现应用程序工作所需的视图组合。
这在 iPad 上应该可行吗?
I'm seeing my app being killed by iOS with an out of memory message, however, while tracing the progress of the app in the Allocations Instrument, I see lots of mallocs that seem to be occurring outside of the code I've written.
I'm not seeing any leaks being caught, so I assume these allocations are supposed to be there. Thing is, because I'm not sure about why they have been allocated, I'm not sure what I can do to optimize the app and prevent the OS from jettisoning my app.
Does anyone know why the memory is being allocated, or is there any way for me to find out?
Here are a couple of shots from Instruments showing the mallocs. In the second shot, all of the allocations have the same stack trace.
EDIT
I' displaying a single large image as the UIView background (1024x768), then overlaying a smaller (600px square) UIView with some custom drawing and a third UIView (550px square) over the top of those that contains two 550px square images overlayed.
I'm guessing that this is not appropriate, and there is probably a better way of achieving the composition of views I need for the app to work.
Should this be possible on the iPad?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这里没有太多信息可以继续 - 如果您添加更多有关应用程序中此视图正在执行的操作的信息,您可能会得到一些更明智的建议。
从屏幕截图来看,似乎正在分配大块来显示图像。
鉴于我冒昧地猜测,要么您正在尝试显示一些非常大的图像,要么您的 UIView 很大,要么您在内存中有更多的 UIView 需要显示当前屏幕。
我想准确追踪它们的来源的最简单方法是禁用您怀疑的应用程序部分,然后再次运行并查看分配是否仍然发生。
编辑
所有图像的尺寸都与您显示的尺寸相同吗? (即,您是否尝试将 5M 照片显示为 1024x768 背景?)如果不是,您可能需要将它们缩小到显示的尺寸,或者至少更接近。
如果您不需要透明度,请确保将所有视图设置为不透明。
I think there's not really much information to go on here - if you add a bit more information about what this view in your app is doing you might get some more informed suggestions.
From the screenshot, it would appears large blocks are being allocated to display an image.
Given that I'd hazard a guess that either you're trying to display some very large images, or you UIView is large, or you have more UIViews in memory that you need to display the current screen.
I guess the easiest way to track down exactly where they're coming from would be to disable the part of the application you suspect then run again and see if the allocations still occur.
EDIT
Are all the images the same size as you're displaying them? (ie. are you trying to display a 5M photo as the 1024x768 background?) If not you probably need to scale them down to the size you are display them, or at least closer.
If you're not needing transparency, make sure to make all the views opaque.
我找出了问题的根源 - 我正在使用
加载图像。我相信很多人都知道,这会缓存图像数据。我有足够大小的图像导致我的应用程序被抛弃。
问题很明显,不是因为图像的大小,而是因为我使用的图像的大小和数量。这里的教训是 小心 [UIImage imageNamed:]。
感谢所有的帮助,伙计们!
I figured out the source of the problem - I was using
to load in my images. This, as I'm sure many people are aware, caches the image data. I had enough images of sufficient size to cause my app to be jettisoned.
The problem was apparent not because of the size of the image but because of both the size and number of images I was using. The lesson here is be careful with [UIImage imageNamed:].
Thanks for all of the help, chaps!
Malloc 可能发生在您的应用程序调用的其他 API 内部(例如加载图像、视图、播放长声音等)。您可以尝试以不同的量更改图像、视图、声音和其他对象的大小作为测试,并且查看 malloc 内存大小的变化是否跟踪您所做的更改之一。
Mallocs can occur inside of other API's that your app calls (such as loading images, views, playing long sounds, etc.) You can try changing the size of your images, views, sounds and other objects by various amounts as a test, and see if the size of the malloc'd memory changes track one of the changes that you've made.