问题
I'm using lots of audio, video, and images in one of my apps and seem to be having minor memory issues and was wondering what the best way to free up memory is.
I use lots of optional variables like this:
var myImageView: UIImageView?
I was wondering if it is considered best practice to set these to nil as soon as you know you won't need it anymore to free up memory like this:
myImageView = nil
It seems like setting it to nil would remove the last strong reference and cause it to be released, but I would also prefer not to litter my code with XXXX = nil all over the place if possible.
I also thought about creating a deinit method for the class that uses this variable and do it in there like this:
deinit {
myImageView = nil
}
The only thing is the instance I'm using doesn't actually get destroyed before it's used again. But normally when an instance is destroyed, all of it's optionals should be released as well, right?
回答1:
From Apple's documentation on Automatic Reference Counting (ARC), they say:
Swift uses Automatic Reference Counting (ARC) to track and manage your app’s memory usage. In most cases, this means that memory management “just works” in Swift, and you do not need to think about memory management yourself. ARC automatically frees up the memory used by class instances when those instances are no longer needed.
The following part seems interesting to you
However, in a few cases ARC requires more information about the relationships between parts of your code in order to manage memory for you.
You haven't posted any code, so I can't know whether you have weak references, unowned references, strong reference cycles for closures etc.
For example, if you have a strong reference cycle for closure as described in the documentation link above:
Swift provides two ways to resolve strong reference cycles when you work with properties of class type: weak references and unowned references.
I think it would be beneficial for you to read the documentation as it will give you a clear idea of how ARC works in Swift.
回答2:
Optionals are a way to use datatypes meaningfully.
For example, you have a variable called age. Just because the user doesn't specify a value to that variable, you cant take its value as zero which is absurd.
So, optional simply means that it accepts a nil value, which is why we unwrap optional variable before using it, to make sure if it has a value.
And memory management is handled by the iOS through ARC. The memory issues that you face could be due to retain cycles.
回答3:
Apple encourages you to use as little memory as possible. By doing this, the system may keep more apps in memory and dedicate memory to apps that really need it. To do this, you could use the following strategies:
Reduce Memory Footprint
If your memory footprint is too high, your application may be terminated, so it's a good idea to free up memory whenever is possible if you use a lot of resources.
If you don't need a resource anymore, you could get ride of its strong references by setting all references to nil, thus, deallocating it. But, depending on your code design, consider using one strong reference on a primary object and a bunch of weak references on others if you use the same resources throughout your code. It may avoid strong references cycles, abandoned memory, and you don't need setting your references to nil every time you want to free up memory. Just set to nil your strong reference.
Memory Warnings
You could also observe low-memory warnings in your app and remove unnecessary resources and resources that can be recreated. You have access to these warnings through:
applicationDidReceiveMemoryWarningmethod in the app delegate.didReceiveMemoryWarningmethod in your view controllers.UIApplicationDidReceiveMemoryWarningNotificationnotification.
Note: You're right. When a instance is destroyed, all their properties are released but not deallocated if it has a strong reference in another place. So there's no need to set a property to nil in the deinit method.
For more information, see:
- Automatic Reference Counting
- Use Memory Efficiently section at Performance Tips
来源:https://stackoverflow.com/questions/35463400/in-swift-should-i-set-my-optional-instance-variables-to-nil-when-done