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?
Instead of trying to dealloc these objects faster, I would recommend that you dealloc them slower.
[ObjectsManager sharedInstance]
, which will be responsible for downloading and holding all these objects in some NSMutableArray
.[[ObjectsManager sharedInstance] removeAllDownloadedObjects]
, which would slowly remove all of them from your NSMutableArray
.[myArray removeAllObjects]
, do it part by part -- delete 20000 objects every second, using NSOperation
or GCD
.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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With