gpt4 book ai didi

ios - 为什么@autoreleasepool 不起作用

转载 作者:行者123 更新时间:2023-11-29 10:48:29 26 4
gpt4 key购买 nike

我尝试在 dispatch_async block 中使用 autoreleasepool,但它不会释放 str。当重复调用 timerEvent 时,会导致内存不足问题。

- (void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:0.0001 target:self selector:@selector(timerEvent) userInfo:nil repeats:YES];

}
-(void)timerEvent
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
@autoreleasepool {
NSString *str =[NSString stringWithFormat:@"%d and %d",px,py];
NSLog(str);
}
});
}

感谢您的帮助。

----- 已解决 -------------- 感谢 C_X

定时器间隔设置得太小。就我而言,我发现它至少应为 0.004。现在,它起作用了。

最佳答案

虽然调度队列确实管理自动释放池,但您正在使用调度队列,但不能保证它们被清空的时间/点。这意味着您的对象将在一段时间后释放。

我认为您的计时器太频繁了,因此您的内存会无限增长(意味着您的对象没有机会解除分配并且您收到内存警告)。

这是苹果documentation .这是一个link关于 stackoverflow 的问题得到了一些很好的答案,请阅读它们。

关于ios - 为什么@autoreleasepool 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21454112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com