简体   繁体   English

NSProgress极大地增加了内存

[英]Massive memory growth with NSProgress

I am experimenting with NSProgress, and finding a simple loop on a background thread causes memory to grow rapidly: 我正在试验NSProgress,在后台线程上找到一个简单的循环会导致内存快速增长:

class Worker {
    var progress:NSProgress?

    func doWork() {
        let numIterations:Int64 = 100000
        let delay:UInt32 = 100

        let progressObj = NSProgress(totalUnitCount: numIterations)
       // progressObj.cancellable = true
        progress = progressObj


        let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT

        dispatch_async(dispatch_get_global_queue(priority, 0)) {
            progressObj.becomeCurrentWithPendingUnitCount(numIterations)
            for i in 0...numIterations {
                progressObj.completedUnitCount = i
                usleep(delay)
            }

            progressObj.resignCurrent()
        }

    }

}

Profiling this with the Allocations instrument shows memory grow to about 20mb over 30 seconds (more if I increase the size of the loop). 使用“分配”工具对此进行分析,可以发现内存在30秒内增长到大约20mb(如果增加循环的大小,则可以增加更多)。 The allocations are all attributed to _NSProgressFraction . 分配都归因于_NSProgressFraction

Is there something obvious I'm overlooking, or is this a bug with NSProgress? 是否有明显的我忽略的东西,或者这是NSProgress的错误?

After a little more experimentation, it looks like the act of setting progressObj.completedUnitCount causes NSProgress to make allocations into the current autorelease pool. 经过更多的试验后,设置progressObj.completedUnitCount的行为似乎导致NSProgress向当前的自动释放池中进行分配。 I found I can keep the memory from growing by wrapping the loop body in an autorelease pool, like so: 我发现可以通过将循环主体包装在自动释放池中来防止内存增长,如下所示:

for i in 0...numIterations {
    autoreleasepool {
        progressObj.completedUnitCount = i
        usleep(delay)
    }
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM