10
我有一組標籤放在一組UIScrollView
內的一組UIViews
中。 他們還沒有被分配出口。我使用的是Avenir Next Ultralight字體,它們大小各不相同。UILabel分配持久化並佔用8Mb的內存
主視圖有一個嵌套在導航控制器中的單個視圖控制器。
我剛剛通過分配檢查,發現加載這個單一視圖消耗大約10MB的內存,其中8MB是這些標籤,當我轉到下一個視圖時,該分配仍然存在。
這可能是什麼原因造成的?
這是錄製分配的快照。
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
這是第一個堆棧跟蹤,其餘的都是一樣的:
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
以爲我會分享我如何設法減少這一點。事實證明,如果您的標籤使用單詞換行,並且在邊上和頂部和底部都有大量的空白空間,則會增加顯示它們所需的內存。因此,解決方案是將該空間減少到絕對最小。
這裏是產生的快照:
Snapshot Timestamp Growth # Persistent
VM: UILabel (CALayer) 2.37 MB 9
0x00 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
這個開銷不會讓我感到過度。你可以做一個沒有標籤的測試嗎? - 另外,標籤是通過故事板實例化的嗎? – Mundi
我做了一個測試,內存使用量減少了8Mb。是的,通過標籤通過故事板實例化。我做了另一個測試,通過減少每個標籤的大小來去除標籤爲空的區域。這樣做只有兩個標籤設法減少1.5Mb。 – user2916031
所以事實證明,這是問題。標籤中的空白空間顯着增加了內存需求。我已經設法將標籤降低到2.4Mb。 – user2916031