autoreleasepool.. ์–ด๋”˜๊ฐ€์—์„œ ๋“ค์–ด๋ดค์ง€๋งŒ ์–ธ์ œ์“ฐ๋Š”์ง€๋Š” ์ „ํ˜€ ๋ชฐ๋ž๋‹ค. ํ•œ๋ฒˆ ์•Œ์•„๋ณผ๊นŒ?

autoreleasepool

@IBAction func touchExecute() {
    for _ in 0...1_000_000_000 {
        let image = UIImage(named: "test")
        print("์ด๋ฏธ์ง€ ์ƒ์„ฑ์ค‘... \(image.debugDescription)")
    }
}

์ด๋Ÿฐ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด 1์–ต๊ฐœ์˜ ์ด๋ฏธ์ง€๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. ์ง€๊ธˆ ์ด ์ƒํ™ฉ์—์„œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? ํ™”๋ฉด์ด ๋ฉˆ์ถ˜๋‹ค. ๊ธฐ๋ณธ๋™์ž‘์ด main thread์—์„œ ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฉ”๋ชจ๋ฆฌ๋„ ์—„์ฒญ๋‚˜๊ฒŒ ์ฆ๊ฐ€ํ•  ๊ฒƒ์ด๋‹ค. for ๋ฌธ์„ ๋ฒ—์–ด๋‚˜๊ธฐ์ „์—๋Š” image๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์œ„์— ์˜ฌ๋ผ๊ฐ„ ๋’ค ํ• ๋‹น ํ•ด์ œ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

@IBAction func touchExecute() {
    DispatchQueue.global().async {
        for _ in 0...1_000_000_000 {
          let image = UIImage(named: "test")
          print("์ด๋ฏธ์ง€ ์ƒ์„ฑ์ค‘... \(image.debugDescription)")
        }
    }
}

๋จธ๋ฆฌ๋ฅผ ๊ตด๋ ค๋ณธ๋‹ค๊ณ , ์ผ๋‹จ main thread๋ฅผ ๋ฒ—์–ด๋‚˜์„œ ์Šค์ฝ”๋“œ๋ฅผ ๋™์ž‘์‹œ์ผฐ๋‹ค. (์‚ฌ์‹ค ์ด๊ฒƒ๋ณด๋‹ค ์ข‹์€ ๊ฒƒ์€ operation์„ ๋งŒ๋“ค์–ด์„œ ์•„์˜ˆ concurrentํ•˜๊ฒŒ ๋™์ž‘์‹œํ‚ค๋Š”๊ฒŒ ๋” ๋น ๋ฅด๊ธด ํ•˜๋‹ค) ๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์—์„œ ํ•ด๋‹น ๋™์ž‘์„ ํ•˜๋„๋ก ํ•˜์—ฌ ํ™”๋ฉด์ด ๋ฉˆ์ถ”๋Š” ๊ฒƒ์€ ๋ฐฉ์–ดํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ ๋ณ€ํ•จ์—†๋‹ค. ์ด ์ƒํ™ฉ์—์„œ for ๋ฌธ ์Šค์ฝ”ํ”„ ์•ˆ์—์„œ ๋™์ž‘ํ•œ ํ›„ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•ด์ œ๋˜๋„๋ก ํ•  ์ˆ˜๋Š” ์—†์„๊นŒ?

@IBAction func touchExecute() {
    DispatchQueue.global().async {
        for _ in 0...1_000_000_000 {
            autoreleasepool {
                let image = UIImage(named: "test")
                print("์ด๋ฏธ์ง€ ์ƒ์„ฑ์ค‘... \(image.debugDescription)")
            }
        }
    }
}

๊ทธ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด autoreleasepool์ด๋‹ค. ํ•ด๋‹น ์Šค์ฝ”ํ”„๋ฅผ ๋ฒ—์–ด๋‚  ๋•Œ๋งˆ๋‹ค ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น์‹œํ‚ฌ ์ˆ˜ ์žˆ์–ด, ๋ฉ”๋ชจ๋ฆฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.