【问题标题】:IPhone App Memory Keeps GrowingiPhone 应用程序内存不断增长
【发布时间】:2012-01-11 01:11:07
【问题描述】:

我已经准备好将我的第一个应用提交到应用商店。检查泄漏,在设备上测试,工作。我想确保我的内存处于可控状态,所以我运行了分配,但是,唉,没有什么是可控的。

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

我会尽量提供尽可能详细的信息,如果还不够,请大喊大叫,我会写更多。

应用从菜单开始。单击一个按钮会通过 presentModalViewController 调出一个视图。视图出现并在后台打开数据库并选择并存储随机行。所以循环:点击主菜单->打开视图->关闭视图导致我的内存每次以10KB-25KB的速度增长。

一些想法:

  • 我的视图的某些部分是通过 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

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

谢谢。

【问题讨论】:

    标签: iphone ios memory-management xcode-instruments allocation


    【解决方案1】:

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

    基本上有方法是运行 Instruments allocate 工具,获取一个 heapshot,运行你的代码的直觉和另一个 heapshot 重复 3 或 4 次。这将指示在迭代期间已分配但未释放的内存。

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

    如果您需要查看对象的保留、释放和自动释放发生的位置,请使用工具:

    在仪器中运行,在分配中将“记录引用计数”设置为开启(您必须停止记录才能设置选项)。使选择器运行,停止记录,在那里搜索 ivar (datePickerView),向下钻取,您将能够看到所有保留、释放和自动释放发生的位置。

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

    【讨论】:

    • 对,这就是我所做的(这就是我想出上面提到的数字的方法)。我无法破译它,一切似乎都是内部的,而不是直接指向我的任何方法。
    猜你喜欢
    • 1970-01-01
    • 2015-11-11
    • 2013-01-09
    • 2013-10-14
    • 2018-09-17
    • 2023-03-19
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多