1 使用masory布局不当引起的内存暴增,然后闪退
问题代码:
- (UILabel *)titleLabel {
if (_titleLabel == nil) {
_titleLabel = [UILabel new];
_titleLabel.font = UIFontMake(12);
_titleLabel.textColor = UIColorWhite;
_titleLabel.text = @"--";
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.numberOfLines = 2;
_titleLabel.minimumScaleFactor = 0.5;
_titleLabel.adjustsFontSizeToFitWidth = YES;
//[_titleLabel sizeToFit];//使用自动布局时避免混用 sizeToFit
[self addSubview:_titleLabel];
}
return _titleLabel;
}
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//make.centerX.equalTo(self);
make.left.right.equalTo(self);
make.top.equalTo(self.iconImageView.mas_bottom).offset(5);
}];
- (void)layoutSubviews {
[super layoutSubviews];
DELog(@"无限调用");
}
调试发现
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2025-07-07 17:16:18.451306+0800 IM[27035:8734297] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<MASLayoutConstraint:0x13bdea160 LiveStreamingCenterLivingSettingFilterViewItem:0x13d270700.left == UIView:0x1281ad200.left + 20>",
"<MASLayoutConstraint:0x13bdeae80 LiveStreamingCenterLivingSettingFilterViewItem:0x13d271a40.left == LiveStreamingCenterLivingSettingFilterViewItem:0x13d270700.right + 31.25>",
"<MASLayoutConstraint:0x13bdebba0 LiveStreamingCenterLivingSettingFilterViewItem:0x13d272a00.left == LiveStreamingCenterLivingSettingFilterViewItem:0x13d271a40.right + 31.25>",
"<MASLayoutConstraint:0x13bdfc900 LiveStreamingCenterLivingSettingFilterViewItem:0x13d271c00.left == LiveStreamingCenterLivingSettingFilterViewItem:0x13d272a00.right + 31.25>",
"<MASLayoutConstraint:0x13bdfd680 LiveStreamingCenterLivingSettingFilterViewItem:0x13d272680.left == LiveStreamingCenterLivingSettingFilterViewItem:0x13d271c00.right + 31.25>",
"<MASLayoutConstraint:0x13bdfd6e0 LiveStreamingCenterLivingSettingFilterViewItem:0x13d272680.right == UIView:0x1281ad200.right - 20>",
"<MASLayoutConstraint:0x13bde89c0 UIView:0x1281ad200.left == UIView:0x1283f5980.left>",
"<MASLayoutConstraint:0x13bde8a20 UIView:0x1281ad200.right == UIView:0x1283f5980.right>",
"<MASLayoutConstraint:0x13d21b9c0 UIView:0x1283f5980.left == UIView:0x1283f5b00.left>",
"<MASLayoutConstraint:0x13d21b900 UIView:0x1283f5980.right == UIView:0x1283f5b00.right>",
"<MASLayoutConstraint:0x13d21bea0 UIView:0x1283f5b00.left == LiveStreamingCenterLivingSettingFilterView:0x13d22b200.left>",
"<MASLayoutConstraint:0x13d219e60 UIView:0x1283f5b00.right == LiveStreamingCenterLivingSettingFilterView:0x13d22b200.right>",
"<NSAutoresizingMaskLayoutConstraint:0x13bdb2c10 LiveStreamingCenterLivingSettingFilterView:0x13d22b200.width == 0>"
)
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x13bdfd680 LiveStreamingCenterLivingSettingFilterViewItem:0x13d272680.left == LiveStreamingCenterLivingSettingFilterViewItem:0x13d271c00.right + 31.25>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2025-07-07 17:16:18.451646+0800 IM[27035:8734297] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<MASLayoutConstraint:0x13bcf3a80 UIView:0x1283f7000.left == UIView:0x1283f5b00.left + 15>",
"<MASLayoutConstraint:0x13d017000 UIView:0x1283f7000.right == UIView:0x1283f5b00.right - 15>",
"<MASLayoutConstraint:0x13d21bea0 UIView:0x1283f5b00.left == LiveStreamingCenterLivingSettingFilterView:0x13d22b200.left>",
"<MASLayoutConstraint:0x13d219e60 UIView:0x1283f5b00.right == LiveStreamingCenterLivingSettingFilterView:0x13d22b200.right>",
"<NSAutoresizingMaskLayoutConstraint:0x13bdb2c10 LiveStreamingCenterLivingSettingFilterView:0x13d22b200.width == 0>"
)
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x13d017000 UIView:0x1283f7000.right == UIView:0x1283f5b00.right - 15>
截屏2025-07-07 17.18.07.png
为啥会出现这种情况???
当 Masonry 中的约束设置不当,导致布局死循环时,应用程序可能遭遇以下严重问题:
1 layoutSubviews无限次调用。
UILabel 的 intrinsicContentSize 会根据文本内容自动计算 ,如果 l约束不足
l或 l冲突
l,布局引擎可能无法正确计算尺寸,进而引发多次 layoutSubviews
的调用,最终可能导致死循环或性能问题
2 内存急增
由于应用在不断调用 layoutSubviews
方法的过程中,不仅系统不断重新计算视图尺寸,而且一些临时对象(如计算过程中产生的置信度数组、约束集合等)也被不断分配而没有及时释放,造成内存使用飙升。特别是在大量文本重绘或复杂视图层级中,这种内存激增问题更为显著,甚至可能会导致 iOS 系统因内存警告而强制杀死应用程序4。
3 CPU 占用率急剧上升
布局死循环会导致 CPU 不断执行相应的约束求解算法,导致占用率飙升。长时间的高负荷运行不仅影响设备的电池寿命,还可能引发其它系统资源调度冲突,造成用户体验极差,甚至引发系统崩溃。
4 调试时的挑战
布局死循环通常不易被直接捕捉到,调试时需要借助 Instruments 或 Xcode 自带的 Debug Memory Graph 工具,才能逐步定位问题所在。此外,通过设置断点(比如在 layoutSubviews
中)观察频繁调用现象,也可证实死循环的情况。由于问题往往仅在特定文本(如设置 numberOfLines 为 2 时)出现,这增加了重现问题的难度。
截屏2025-07-07 17.30.58.png