Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce the amount of time to release 1 million + objects while popping out VC

The current iOS app that we have has to download more than a million objects from the server and we keep it in an array for certain purposes. When the user is done with this functionality and the app takes a while to go back to the previous screen (~15 sec) Its because the 1 million objects are being released. We can see from Instruments that the count going from 1 million to 0 during this time (15 sec). Is there any way to speed up the deallocation of these 1 mn objects that are there in the array?

like image 414
Vinny Avatar asked Apr 09 '16 01:04

Vinny


Video Answer


2 Answers

Instead of trying to dealloc these objects faster, I would recommend that you dealloc them slower.

  1. Keep all these objects somewhere else. Create some singleton [ObjectsManager sharedInstance], which will be responsible for downloading and holding all these objects in some NSMutableArray.
  2. Use these objects whenever you want in other VCs.
  3. When you are finished, tell your [[ObjectsManager sharedInstance] removeAllDownloadedObjects], which would slowly remove all of them from your NSMutableArray.
  4. Instead of writing [myArray removeAllObjects], do it part by part -- delete 20000 objects every second, using NSOperation or GCD.
like image 187
OlDor Avatar answered Nov 15 '22 05:11

OlDor


I freed 1 milion instances of ~600 MB (picked expensive UIView) in somewhere about one and half of sec on iPhone 6s. When tried with modest class (~50 MB), it took ~0.2 sec. Still, if you can hold your data in struct, you are going close to zero. I believe, you are losing your time somewhere else...

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    class C: UIView {
        static var c = 0
        deinit { C.c -= 1 }
        init() { C.c += 1; super.init(frame: CGRect.zero) }
        required init?(coder aDecoder: NSCoder) { fatalError() }
    }

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        var a: [C]? = []
        for i in 0..<1_000_000 {
            a!.insert(C(), atIndex: i)
        }

        print(C.c) // We have 1_000_000 allocations.

        let t1 = CFAbsoluteTimeGetCurrent()
        a = nil
        let t2 = CFAbsoluteTimeGetCurrent()

        print(t2 - t1) // 1 milion instances of ~600 MB freed in ~1.5 seconds.
        print(C.c) // ARC works!

        return true
    }
}
like image 32
Stanislav Smida Avatar answered Nov 15 '22 05:11

Stanislav Smida