Memory Problems swift hurricane

Swift language development in the use of time,Many of my friends will be baffling problem encountered memory full,Obviously there ARC ,Obviously the release of memory,But still let the program memory footprint with circulation soared。

There is not really a memory leak occurs,This is actually ARC A mechanism:In each of the main Runloop At the end of the clean-up。That is,It has a caching mechanism necessary - after all,,Who can guarantee real-time release, then you do not want to stay local variables to subsequent code to use it?

But apparently,The necessary mechanisms at this time has become our nightmare,For example,:

Here we assume that loadBigData() A slightly larger file is loaded and cumbersome operation,Well, according to idea,This document will be released in each cycle - after all, is a local variable within the loop,But in fact the caching mechanism because the ARC,Memory would have been larger。

Memory usage increases until the software has been stuck System
Memory usage increases until the software has been stuck System

at this time,We can say that ARC is not very smart,So once this happens,Then the need for manual intervention ARC,To achieve better memory management effectiveness。

In the Swift,We use this global function public func autoreleasepool<Result>(invoking body: () throws -> Result) rethrows -> Result

can be seen,It is a generic statement,Return type according to your incoming closures may be。We have to do is to put the contents need to calculate this function is calculated,This is equivalent to a temporary memory for the open-content labeled as not to cache,So do not wait until the end of the ARC was recovered Runloop,But recovered immediately after use memory resources,For example, to change this:

Such,Calculation is complete contents will immediately be cleaned up,Behavior of the program truly in line with our intention。

Memory stable and controllable range
Memory stable and controllable range

Finally, still have to make a,Here we loadBigData() It refers to the process of loading large files then you want to get the data back again,Not initialize a class,If the class is initialized for example by Dafa(URL:url) Such initialization method,ARC is able to automatically manage。Here we are talking about is a very special case - after all, so long programming,I do not necessarily run into a few times。

Original article written by Gerber drop-off:R0uter's Blog » Memory Problems swift hurricane

Reproduced Please keep the source and description link:

By R0uter's Blog

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Leave a Reply

Your email address will not be published. Required fields are marked *