UILabel(CALayer)正在使用大量虚拟内存 [英] UILabel (CALayer) is using large amounts of virtual memory
问题描述
在Xcode and Instruments
中,我看到UILabel (CALayer)
使用了大量的虚拟内存(匿名VM).我看到每个UILabel
大约235 KB的虚拟内存.
In Xcode and Instruments
I see UILabel (CALayer)
using large amounts of virtual memory (Anonymous VM). I see about 235 KB of virtual memory per UILabel
.
我认为这可能是iOS 7.1或7.1.1的新问题.
I think this perhaps is a new issue with iOS 7.1 or 7.1.1.
这是预期的吗?
我创建了一个简单的程序,该程序可以创建500个UILabels
,并且Instruments
显示已使用的115MB内存.在大约1500个标签处,应用程序被操作系统终止.
I created a simple program that creates 500 UILabels
and Instruments
shows 115MB of memory used. At about 1500 labels the application is terminated by the OS.
for (int i = 0; i < 500; i++)
{
index = (int)[self.items count];
index++;
frame = CGRectMake(10.0, 20, 300.0, 50.0);
UILabel *newLabel = [[UILabel alloc] initWithFrame:frame];
newLabel.text = [NSString stringWithFormat:@"This is text for label: %d", index];
newLabel.backgroundColor = [UIColor whiteColor];
[self.view addSubview:newLabel];
[self.items setObject:newLabel forKey:[NSNumber numberWithInteger:index]];
}
有想法吗?
推荐答案
UILabel,并且任何使用drawRect的视图(至少在iOS 7+上)都由纹理支持,因此每个UILabel都会使用大量内存,标签越大,使用的内存越多.
UILabel, and any view that uses drawRect (at least on iOS 7+) is backed by a texture, so every UILabel will use a lot of memory, the larger the label, the more memory used.
我发现,在我的照片编辑扩展程序"You Doodle"中,这尤其令人痛苦,该扩展程序允许在照片"应用中添加文本.不幸的是,与常规应用程序崩溃之前,照片扩展名比常规应用程序更受内存使用限制,因此我不得不极大地限制文本的缩放范围.
I've found this especially painful in my photo editing extension for You Doodle which allows adding text in the Photos app. Unfortunately I have to greatly restrict the scale range of the text because photo extensions are much more limited to memory usage than regular apps before they crash.
可以通过运行工具并分配一堆大型UILabel并设置其文本并确保它们均可见来轻松验证这一点,即:
This can easily be verified by running instruments and allocating a bunch of large UILabel's and setting their text and ensuring they are all visible, i.e.:
CGRect f = CGRectMake(0.0.0f, 0.0f, 300.0f, 300.0f);
for (NSInteger i = 0; i < 100; i++)
{
UILabel* l = [[UILabel alloc] initWithFrame:f];
l.backgroundColor = UIColor.blueColor;
l.textColor = UIColor.whiteColor;
l.text = @"UILabel text for the memory test";
l.numberOfLines = 0;
[self.view addSubview:l];
}
这篇关于UILabel(CALayer)正在使用大量虚拟内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!