xcode UILabel分配持久化,占用8Mb内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19568083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UILabel allocation persistent and taking up 8Mb of memory
提问by user2916031
I have a set of labels that are placed within a set of UIViews
inside a single UIScrollView
.
They have not been assigned an outlet. I am using Avenir Next Ultralight font for all of them, and they have varying sizes.
我有一组标签,它们放置在UIViews
单个UIScrollView
. 他们没有被分配一个出口。我为所有这些字体都使用了 Avenir Next Ultralight 字体,它们的大小各不相同。
The main view has a single view controller which is nested in a navigation controller.
主视图有一个单独的视图控制器,它嵌套在导航控制器中。
I was just running through a check of allocations and noticed that with this single view upon loading consumes about 10MB of memory of which 8MB are these labels, and when I go to the next view, that allocation remains live.
我刚刚检查了分配并注意到加载时这个单一视图消耗了大约 10MB 的内存,其中 8MB 是这些标签,当我转到下一个视图时,该分配仍然有效。
What could be causing this?
什么可能导致这种情况?
This is a snapshot from recording allocations.
这是记录分配的快照。
Snapshot Timestamp Growth # Persistent
VM: UILabel (CALayer) 7.90 MB 10
0x5417000 00:02.950.428 1.23 MB
0x5553000 00:02.967.675 1.23 MB
0x568f000 00:02.978.283 1.23 MB
0x591d000 00:03.013.095 832.00 KB
0x57cb000 00:03.004.334 832.00 KB
0x50a7000 00:02.919.040 832.00 KB
0x5177000 00:02.937.076 832.00 KB
0x5347000 00:02.940.969 832.00 KB
0x52d3000 00:02.998.823 120.00 KB
0x52f1000 00:03.023.068 16.00 KB
This is the stack trace for the first one, and the rest are the same:
这是第一个的堆栈跟踪,其余相同:
0 libsystem_kernel.dylib mach_vm_allocate
1 libsystem_kernel.dylib vm_allocate
2 QuartzCore CA::Render::Shmem::new_shmem(unsigned long)
3 QuartzCore CA::Render::Shmem::new_bitmap(unsigned int, unsigned int, unsigned int, unsigned int)
4 QuartzCore CABackingStoreUpdate_
5 QuartzCore ___ZN2CA5Layer8display_Ev_block_invoke
6 QuartzCore x_blame_allocations
7 QuartzCore CA::Layer::display_()
8 QuartzCore CA::Layer::display_if_needed(CA::Transaction*)
9 QuartzCore CA::Layer::layout_and_display_if_needed(CA::Transaction*)
10 QuartzCore CA::Context::commit_transaction(CA::Transaction*)
11 QuartzCore CA::Transaction::commit()
12 UIKit -[UIApplication _reportAppLaunchFinished]
13 UIKit -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:]
14 UIKit -[UIApplication handleEvent:withNewEvent:]
15 UIKit -[UIApplication sendEvent:]
16 UIKit _UIApplicationHandleEvent
17 GraphicsServices _PurpleEventCallback
18 GraphicsServices PurpleEventCallback
19 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
20 CoreFoundation __CFRunLoopDoSource1
21 CoreFoundation __CFRunLoopRun
22 CoreFoundation CFRunLoopRunSpecific
23 CoreFoundation CFRunLoopRunInMode
24 UIKit -[UIApplication _run]
25 UIKit UIApplicationMain
26 main
27 libdyld.dylib start
SOLUTION
解决方案
Thought I would share how I managed to reduce this. It turns out that if you have labels which word wrap, and have a significant amount of empty space around the sides, and top and bottom, this adds to the memory required to display them. So, the solution is to reduce that space to an absolute minimum.
我想我会分享我如何设法减少这种情况。事实证明,如果您有自动换行的标签,并且在两侧、顶部和底部周围有大量空白空间,这会增加显示它们所需的内存。因此,解决方案是将该空间减少到绝对最小值。
Here is the resulting snapshot:
这是生成的快照:
Snapshot Timestamp Growth # Persistent
VM: UILabel (CALayer) 2.37 MB 9
0x5321000 00:02.503.255 208.00 KB
0x5248000 00:02.474.045 392.00 KB
0x5087000 00:02.432.317 208.00 KB
0x50bb000 00:02.445.927 64.00 KB
0x5303000 00:02.498.137 120.00 KB
0x52aa000 00:02.481.354 292.00 KB
0x50cb000 00:02.449.625 180.00 KB
0x5355000 00:02.505.638 112.00 KB
0x50f9000 00:02.455.350 848.00 KB
回答by Tim Walsh
I am having the same issue. I will try what you suggested with the size. But, maybe changing the background color from clearColor could correct the issue as well.
我有相同的问题。我会试试你建议的尺寸。但是,也许从 clearColor 更改背景颜色也可以解决问题。