Reduce the amount of time to release 1 million + objects in VC output

The current iOS application that we have is supposed to download more than a million objects from the server, and we save it in an array for specific purposes. When the user performs this function, and the application takes some time to return to the previous screen (~ 15 seconds). This is because 1 million objects have been released. From the Tools, we can see that the number of samples ranges from 1 million to 0 during this time (15 seconds). Is there a way to speed up the allocation of these 1 mn objects that are in the array?

+5
source share
3 answers

Instead of deleting these objects faster, I would recommend that you disable them more slowly .

  • Keep all these objects in a different place. Create one singleton [ObjectsManager sharedInstance] , which will be responsible for loading and storing all of these objects in some NSMutableArray .
  • Use these objects whenever you want in other VCs.
  • When you're done, say <<22> remove all of them NSMutableArray .
  • Instead of writing [myArray removeAllObjects] do it piecemeal - delete 20,000 objects every second using NSOperation or GCD .
+3
source

I released 1 million copies of ~ 600 MB (I chose the expensive UIView ) for about a second and a half on iPhone 6s. When you tried with a modest class (~ 50 MB), it took ~ 0.2 sec. However, if you can store your data in a structure, you are approaching zero. I believe you are wasting time elsewhere ...

 import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? class C: UIView { static var c = 0 deinit { Cc -= 1 } init() { Cc += 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(Cc) // 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(Cc) // ARC works! return true } } 
+2
source

The heap fragmentation problem. Releasing a contiguous block of memory in a language such as ObjC / C / C ++ is fast. Redistributing fragmented memory in comparison is slow. It is difficult to verify, because the distribution and removal pattern often affects the amount of fragmentation that you see. An example may help.

Many distributors use adjacent lists of mixed-sized linked lists. When you allocate a million of them in a loop, you are likely to have many contiguous pieces of memory that take advantage of caches, etc. when freed up in a loop, etc. That is, they look fast. When you test it during the production process, it is not so fast.

The only way to avoid this is to avoid heap fragmentation, which means ...

  • Select the blocks in which N objects are stored and free each block at a time.
  • It is ideal to allocate one contiguous memory area for the entire set of N objects and free it in one call.
+1
source

Source: https://habr.com/ru/post/1246752/


All Articles