iPhone应用程序内存持续增长

4
我已准备好将我的第一个应用程序提交到应用商店。进行了泄漏检查、在设备上测试等等。我想确保我的内存受到控制,所以我运行了分配,但是,不幸的是,什么也没有受到控制。
我已经测试了我的应用程序的多个部分,但我专注于一些可以使用户持续使用时内存非常容易失控的重要区域。对我来说,从分配/堆栈中获得的信息很难阅读,因此我希望有人能够为我解释这个输出。
我会尽可能提供详细信息,如果不足,请告诉我,我会写更多。
应用程序从菜单开始。通过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

如果您认为代码的某些部分会有所帮助,请告诉我。我感觉自己一直在前进,但这让我感到相当沮丧。
谢谢。
1个回答

0

使用Heapshot查找内存泄漏,参见:bbum博客

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

要找出结果,请披露以查看各个分配。

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

在Instruments中运行,在分配中打开“记录引用计数”(您必须停止记录才能设置选项)。导致选择器运行,停止记录,搜索那里的ivar(datePickerView),深入挖掘,您将能够看到所有保留、释放和自动释放发生的位置。

我已经多次使用过这个,确实很有帮助,祝好运。


4
没错,那就是我所做的(这就是我提到的数字背后的来源)。然而我无法解读其中任何内容,似乎所有东西都是内部的,没有直接指向我的任何方法。 - ballofpopculture

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接