Norway


There is no . This comes from the fact that you are abusing the main thread: thousands of expensive NSDateComponents objects are created per second, each taking 176 Bytes, which amounts in a few seconds to tens of megabytes. Calendar..component(_:from:) is the one for all of these allocations.

The solution is to use an autoreleasepool. With ARC, Apple says that developers do not need to deallocate objects manually from the memory. There are some cases, however, where creating an autoreleasepool could help reduce the peak memory footprint of the . Autoreleasepools are helpful because instead of waiting for the system to release whatever objects you have made, you are telling the system to release those objects at the end of a closure. In an app, the main function is running in an autoreleasepool. A drain operation of that pool will happen at the end of every main run loop. the problem arises when there are more objects created than drained.

So, you should use an autoreleasepool for the parts of your code that use up a lot of memory:

autoreleasepool(invoking: {
    while Calendar.current.component(.year, from: Date()) > 0 {
        print("Hello world")
    }
})

This would slow down the rate at which the memory is being populated.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here