Memory management in Swift: memory does not open after NSOperation completes

Can someone clarify one thing about Swift memory management?

I have the following application delegate:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    private let _queue = NSOperationQueue()

    func applicationDidFinishLaunching(aNotification: NSNotification) {

        _queue.maxConcurrentOperationCount = 1

        _queue.addOperation(Operation())
        _queue.addOperation(Operation())
        _queue.addOperation(Operation())
    }
}

private class Operation: NSOperation {

    override func main() {

        autoreleasepool {

            var d = [String: AnyObject]()

            for i in 1...1000 {
                d[i.description] = Repeat(count: 10000, repeatedValue: "ABCDEF").joinWithSeparator("") // tested with repeat or just large string
            }

            d.removeAll()
        }
    }
}

After starting the application, I start three synchronous operations. Each operation simply creates a very large dictionary (struct) with very large strings (also structs).

Since all the data created is a value type and nothing was captured, I believe that memory should be released when the main function ends. In fact, I added d.RemoveAll()just in case to free up memory.

After completing all operations, Activity Monitor displays the following information: enter image description here

- 26.7mb, _queue.addOperation(Operation()), 7mb. 19 , ?

@Lou Franco

func applicationDidFinishLaunching(aNotification: NSNotification) {  
    for _ in 1...100 {
        _queue.addOperation(Operation())
    }
}

8 . 40 . 100 000 . 40 . 600... 1000 , .

+4
1

. - . malloc.

. , .

, , ( ). , 13.8, . , , , .

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    private let _queue = NSOperationQueue()

    func applicationDidFinishLaunching(aNotification: NSNotification) {

        _queue.maxConcurrentOperationCount = 1

        _queue.addOperation(Operation())
        _queue.addOperation(Operation())
        _queue.addOperation(Operation())
    }
}

private class Operation: NSOperation {

    override func main() {
        autoreleasepool {
            let count = 100000000
            let p = UnsafeMutablePointer<Int>.alloc(count)
            for i in 0..<count {
                p.advancedBy(i).initialize(i)
            }
            p.destroy(count)
            p.dealloc(count)
        }
    }
}
0

All Articles