iPhone 应用程序内存不断增长

标签 iphone ios memory-management xcode-instruments allocation

我已准备好将我的第一个应用程序提交到应用程序商店。检查泄漏,在设备上测试,工作。我想确保我的内存受到控制,所以我运行了分配,但是,唉,一切都不受控制。

我已经测试了应用的多个区域,但我主要关注了几个大区域,用户继续使用这些区域很容易导致内存失控。对我来说,来自 Allocations/Heapshots 的信息很难阅读,所以我希望有人可以为我提供 Rosetta Stone 的输出。

我会尽力提供尽可能多的细节,如果还不够,请大喊大叫,我会写更多。

应用程序从菜单开始。单击一个按钮会通过 presentModalViewController 调出一个 View 。出现该 View ,并在后台打开一个数据库,并选择并存储一个随机行。所以循环:点击Main Menu->Open View->Close View导致我的内存每次都以10KB-25KB的速度增长。

一些想法:

  • 我的 View 的某些部分是通过 IB 创建的,其中包含一些按钮 是自定义 .png 文件。我读到早期版本有问题 释放并重新分配这些导致内存泄漏的资源。
  • 我一直在使用 button.layer.borderWidth/borderColor/cornerRadius/backgroundColor 和我最初在 IB 中创建的一些按钮。这是禁忌吗? (删除它们确实有点帮助,但问题仍然存在)。

对于那些好奇的人,这是最大堆增长的调用堆栈:

0 libSystem.B.dylib calloc
1 CoreGraphics CGGlyphBitmapCreate
2 CoreGraphics CGFontCreateGlyphBitmap8
3 CoreGraphics CGFontCreateGlyphBitmap
4 CoreGraphics CGGlyphLockLockGlyphBitmaps
5 libRIP.A.dylib ripc_DrawGlyphs
6 CoreGraphics draw_glyphs
7 CoreGraphics CGContextShowGlyphsWithAdvances
8 WebCore WebCore::showGlyphsWithAdvances(WebCore::FloatPoint const&, WebCore::SimpleFontData const*, CGContext*, unsigned short const*, CGSize const*, unsigned long)
9 WebCore WebCore::Font::drawGlyphs(WebCore::GraphicsContext*, WebCore::SimpleFontData const*, WebCore::GlyphBuffer const&, int, int, WebCore::FloatPoint const&, bool) const
10 WebCore WebCore::Font::drawSimpleText(WebCore::GraphicsContext*, WebCore::TextRun const&, WebCore::FloatPoint const&, int, int) const
11 WebCore WebCore::Font::drawText(WebCore::GraphicsContext*, WebCore::TextRun const&, WebCore::FloatPoint const&, int, int) const
12 WebKit drawAtPoint(unsigned short const*, int, WebCore::FloatPoint const&, WebCore::Font const&, WebCore::GraphicsContext*, bool, WebCore::BidiStatus*, int)
13 WebKit -[NSString(WebStringDrawing) __web_drawAtPoint:forWidth:withFont:ellipsis:letterSpacing:includeEmoji:measureOnly:renderedStringOut:drawUnderline:]
14 WebKit -[NSString(WebStringDrawing) __web_drawAtPoint:forWidth:withFont:ellipsis:letterSpacing:includeEmoji:measureOnly:renderedStringOut:]
15 WebKit -[NSString(WebStringDrawing) __web_drawAtPoint:forWidth:withFont:ellipsis:letterSpacing:includeEmoji:measureOnly:]
16 WebKit -[NSString(WebStringDrawing) _web_drawAtPoint:forWidth:withFont:ellipsis:letterSpacing:includeEmoji:]
17 UIKit -[NSString(UIStringDrawing) drawAtPoint:forWidth:withFont:lineBreakMode:letterSpacing:includeEmoji:]
18 UIKit -[NSString(UIStringDrawing) drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:includeEmoji:]
19 UIKit -[NSString(UIStringDrawing) drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:]
20 UIKit -[UILabel _drawTextInRect:baselineCalculationOnly:]
21 UIKit -[UILabel drawTextInRect:]
22 UIKit -[UILabel drawRect:]
23 UIKit -[UIView(CALayerDelegate) drawLayer:inContext:]
24 QuartzCore -[CALayer drawInContext:]
25 QuartzCore backing_callback(CGContext*, void*)
26 QuartzCore CABackingStoreUpdate_
27 QuartzCore CA::Layer::display_()
28 QuartzCore -[CALayer _display]
29 QuartzCore CA::Layer::display()
30 QuartzCore -[CALayer display]
31 QuartzCore CA::Layer::display_if_needed(CA::Transaction*)
32 QuartzCore CA::Context::commit_transaction(CA::Transaction*)
33 QuartzCore CA::Transaction::commit()
34 QuartzCore CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*)
35 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
36 CoreFoundation __CFRunLoopDoObservers
37 CoreFoundation __CFRunLoopRun
38 CoreFoundation CFRunLoopRunSpecific
39 CoreFoundation CFRunLoopRunInMode
40 GraphicsServices GSEventRunModal
41 GraphicsServices GSEventRun
42 UIKit UIApplicationMain
43 GRE Words main /Users/admin/Dropbox/GRE Words/main.m:14
44 GRE Words start

如果您认为一些代码会有所帮助,请告诉我。我觉得我正在向前迈进,这非常令人沮丧。

谢谢。

最佳答案

使用 Heapshot 查找内存泄漏,请参阅:bbum blog

基本上,方法是运行 Instruments 分配工具,进行堆快照,运行代码的直觉和另一个堆快照,重复 3 或 4 次。这将指示在迭代期间分配和未释放的内存。

要弄清楚结果,请查看个人分配。

如果您需要查看某个对象的保留、释放和自动释放发生在何处,请使用工具:

在 instruments 中运行,在 Allocations 中将“Record reference counts”设置为 on(您必须停止记录才能设置该选项)。使选择器运行,停止记录,搜索 ivar (datePickerView),向下钻取,您将能够看到所有保留、释放和自动释放发生的位置。

我已经用过很多次了,它真的很有帮助,祝你好运。

关于iPhone 应用程序内存不断增长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8348992/

相关文章:

iphone - 将 NSManagedObject 存储在字典中 (NSDictionary)

ios - Google 身份验证 - 用户代理在 WebView (Nylas api) 上给出错误

ios - 当只有一个单元格时,单元格位于 Collection View 的中心

delphi - 如何查找 "fat"程序内存使用情况?

ios - 观看视频时录制视频并带有音频

iphone - 在 iPhone 顶级异常处理程序中显示警报

iphone - Objective C 中的多个字符串替换

objective-c - 将对象添加到 NSMutableArray 时出现问题

取消设置后 PHP 释放内存

c++ - 临时对象没有被正确销毁?