Swift gdc使用了大量内存

时间:2016-02-21 15:08:44

标签: swift

在MacOs上(swift 2.1) 这段代码使用了超过7Gb的内存,我无法清理它 我怎样才能清理记忆?
如果我在usleep(10)之前放dispatch_async(queue!),那么内存只使用1Gb

import Foundation
import Darwin

class Thread: NSObject
{
    private var thread: NSThread?
    private var callback: (Void -> Void)?

    init(_ callback : Void -> Void) {
        super.init()
        self.callback = callback

        self.thread = NSThread(target: self, selector: "invoke", object: nil);
        thread?.start();
    }

    func invoke() {
        self.callback?()

        // Discard callback and timer.
        self.callback = nil
        self.thread = nil
    }
}

var queue:dispatch_queue_t? = dispatch_queue_create("com.test", nil);

for i in 0...20 {
    Thread(){
        for j in 0...100000 {
            dispatch_async(queue!) {

            }
        }
    }
}

sleep(20)

0 个答案:

没有答案