在长时间运行的后台线程上定期消耗自动释放池的最佳方法是? [英] Best way to periodically drain the autorelease pool on a long-running background thread?

查看:94
本文介绍了在长时间运行的后台线程上定期消耗自动释放池的最佳方法是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发人员文档中,内容为:

In the developer documentation, it says:

如果您的应用程序或线程是长期存在的,并可能生成许多自动释放的对象,则应定期排空并创建自动释放池(就像Application Kit在主线程上所做的那样);否则,自动释放的对象会堆积,并且您的内存占用量也会增加.但是,如果分离的线程未进行Cocoa调用,则无需创建自动释放池.

我想知道这样做的最好方法是什么.我有几种我认为可行的方法,但不知道哪个是最佳"方法.我目前有一种方法可以启动线程并使它等待操作执行:

I was wondering what the best way to do this is. I have several methods I think would work, but don't know which is the "best". I currently have a method that start the thread and keeps it waiting for operations to perform:

- (void)startThread
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    accessoryRunLoop = [NSRunLoop currentRunLoop];

    //Add a dummy port to avoid exiting the thread due to no ports being found
    [accessoryRunLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];

    while(accessoryThreadIsRunning)
    {
        //Keep the thread running until accessoryTheadIsRunning == NO
        [accessoryRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }

    [pool release];
}

我能想到的选择是:

1)在while(accessoryThreadIsRunning)中添加一个计数器,这样每50或100次它将耗尽自动释放池并创建一个新计数器.

1) Add a counter in the while(accessoryThreadIsRunning) so that every 50 or 100 times it will drain the autorelease pool and create a new one.

2)每次我在该线程中执行一个方法时(使用performSelector:onThread :),我都可以创建一个自动释放池,然后在方法结束时释放它.

2) Every time I perform a method in that thread (using performSelector: onThread:), I can create an autorelease pool and then release it at the end of the method.

3)设置一个计时器,以清空池,然后定期创建.

3) Make a timer so that a pool is drained and then created periodically.

我认为选项1是最好的,但是我想知道是否有其他方法可以执行此操作.谢谢!

I think that option 1 is the best, but would like to know if there is a different way I should be doing this. Thanks!

推荐答案

我将以简单的方式开始,并在每次循环中创建/清空池.

I'd start with dead simple and just create/drain the pool on every pass through the loop.

如果它在性能分析过程中显示为瓶颈,请修复它.

If it shows up during performance analysis as a bottle neck, fix it.

保持简单,直到分析表明需要复杂性为止.

Keep it simple until analysis indicates complexity is required.

我刚刚重新阅读了您的问题,并在我的答案中完全意识到了一些疑问.如果您正在运行运行循环,则应该为您自动管理自动释放池;应该在循环的顶部创建一个池,并在每次循环结束时将其耗尽.

I just re-read your question and realized something entirely boneheaded in my answer. If you are running a run-loop it should be managing an autorelease pool automatically for you; it should create a pool at the top of the loop and drain it at the end of each pass through the loop.

如果您有其他事情要在运行循环之外进行,则只需要自己循环一次即可.是这样吗?

You only need to cycle one yourself if you have other stuff going on outside of the runloop. Is that the case?

无论如何,是的,模式是:

In any case, yes, the pattern is:

 while(...) {
    ... create pool ...
    ... do stuff ...
    ... drain pool ...
 }

这篇关于在长时间运行的后台线程上定期消耗自动释放池的最佳方法是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆