Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Swift should I set my optional instance variables to nil when done?

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?

like image 716
TenaciousJay Avatar asked Feb 17 '16 17:02

TenaciousJay


People also ask

Are optional variables weak in Swift?

No, weak and optional are not the same, but there is some interplay between the two. Optional just means that a variable can be nil , either by assigning nil yourself, or becoming nil through some other means. The weak keyword has to do with memory management.

Is nil a value in Swift?

In Swift, nil means the absence of a value. Sending a message to nil results in a fatal error. An optional encapsulates this concept. An optional either has a value or it doesn't.

When should I use optionals Swift?

Optional types or Optionals in Swift You use optionals in situations where a value may be absent. An optional represents two possibilities: Either there is a value, and you can unwrap the optional to access that value, or there isn't a value at all. That's a pretty straightforward definition.

How check Optional is null in Swift?

In Swift, you can also use nil-coalescing operator to check whether a optional contains a value or not. It is defined as (a ?? b) . It unwraps an optional a and returns it if it contains a value, or returns a default value b if a is nil.


1 Answers

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.

like image 182
Guy Daher Avatar answered Oct 13 '22 09:10

Guy Daher