iPhone应用程序内存不断增长

发布于 2024-12-19 00:18:45 字数 3803 浏览 1 评论 0原文

我已准备好向应用商店提交我的第一个应用程序。检查是否有泄漏,在设备上进行测试,工作正常。我想确保我的内存受到控制,所以我运行了分配,但是,唉,没有任何东西受到控制。

我已经测试了应用程序的多个区域,但我重点关注了几个大区域,用户持续使用这些区域可能会导致内存很容易失控。对我来说,来自分配/堆图的信息很难阅读,所以我希望有人可以为我罗塞塔石碑提供这个输出。

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

应用程序从菜单启动。单击按钮会通过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

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

谢谢。

I was all ready to submit my first app to the app store. Checked for leaks, tested on devices, the works. I wanted to make sure my memory was under control so I ran allocations, and, alas, nothing was under control.

I've test multiple areas of my app, but I focused on a couple big areas where continued use by the user could make the memory get out of control very easily. To me the information from Allocations/Heapshots is hard to read, so I'm hoping someone out there can Rosetta Stone this output for me.

I'll try to give as much detail as possible, and if it's not enough, just yell, and I'll write more.

App starts at a Menu. Clicking a button brings up a view, via presentModalViewController. The view comes up and in the background a database is opened and a random row is selected and stored. So the cycle of: Click on Main Menu->Open View->Close View causes my memory to grow at a rate of 10KB-25KB every time.

Some thoughts:

  • Some parts of my view are created via IB, with some of the buttons
    being custom .png files. I read that an earlier build had trouble
    releasing and reallocating these resources which caused memory leaks.
  • I've been using button.layer.borderWidth/borderColor/cornerRadius/backgroundColor with some of my buttons that were originally created in IB. Is this a no-no? (Removing them does help a bit, but the problem still shows up).

For those curious, this is the call stack for the biggest of the heap growth:

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

If you think bits of code will help, please let me know. I felt like I was moving forward and this has been pretty disheartening.

Thanks.

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

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

发布评论

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

评论(1

揽月 2024-12-26 00:18:45

使用Heapshot查找内存creap,参见:bbum blog

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

要了解结果,请查看单独的分配情况。

如果您需要查看对象在何处发生保留、释放和自动释放,请使用仪器:

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

我已经使用过很多次了,它确实很有帮助,祝你好运。

Use Heapshot to find memory creap, see: bbum blog

Basically there method is to run Instruments allocate tool, take a heapshot, run an intuition of your code and another heapshot repeating 3 or 4 times. This will indicate memory that is allocated and not released during the iterations.

To figure out the results disclose to see the individual allocations.

If you need to see where retains, releases and autoreleases occur for an object use instruments:

Run in instruments, in Allocations set "Record reference counts" on on (you have to stop recording to set the option). Cause the picker to run, stop recording, search for there ivar (datePickerView), drill down and you will be able to see where all retains, releases and autoreleases occurred.

I have used this many times and it really helped, good luck.

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