Reference count in Swift memory management

Source: Internet
Author: User

In swift, each object has a life cycle, and when the end of the lifecycle calls the Deinit () function to free up memory space.

Observe this section of code:

classperson{var name:string var pet:pet?Init (name:string) {self.name=Name Print (" Person", Name,"is initialized")} init (name:string, petname:string) {self.name=name Self.pet=Pet (name:petname) print (" Person", Name,"is initialized")} deinit{print (" Person", Name,"is deinitialized!")    }}classpet{var name:string init (name:string) {self.name=name; Print ("Pet", Name,"is initialized")} deinit{print ("Pet", Name,"is deinitialized!")    }}

This code creates two classes, the person class and the pet class, each with the Init method for creating objects and the Deinit method to free up memory space, where the person class has two init methods that correspond to whether the name of the pet class is included.

When we call these two methods:

" Snow " " Wolf "  = Nil

The two-step implementation results are:

 is are deinitialized!   is deinitialized!

It will be found that the second Init method is called when creating the object of snow, and in this method a new pet object is created, so the pet wolf is initialized will be printed first and then the person snow is initialized. When the memory of the Snow object is released, the nil is assigned to this object, it will release snow this memory space, but also release wolf this memory space.

But if we call the first Init method we will find:

" Snow " var wolf:pet"Wolf") Snow? Pet == Nil

We first created a snow object, then we created a wolf object and then added wolf to the Snow object, but when we released the memory of this snow object we found that:

 is are deinitialized!

Only Snow's memory space was freed, but Wolf's memory space was not freed, which was related to the reference count in Swift memory management:

When we created the object of snow, we opened up a memory space for it, named A, when the snow object refers to this memory space, this memory space reference count is 1,

Similarly, when we created the Wolf object, we opened up a memory space for it, named B, when the Wolf object refers to this memory space, the reference count of this memory space is 1,

When we will snow? Pet = Wolf, then a property in snow also points to the creation of the Wolf Object memory space, then this memory space reference count is 2.

When we release the memory space for snow = nil, the reference count for memory space A is 0, and the reference count for memory space B is 1.

When the system discovers a memory space reference count of 0, then the system will release this memory space, then memory space A is released.

However, the reference count for memory space B is 1, and the system does not automatically release memory. Only when we proceed:

Wolf = Nil

After the operation, this memory space B will not be released.

Similarly, for this code:

Import UIKitclassperson{var name:string init (name:string) {self.name=Name Print (" Person", Name,"is initialized")} deinit{print (" Person", Name,"is being deinitialized!")}}var Person1:person? = Person (name:"Liuyubobobo") var Person2:person? =Person1var Person3:person? = Person1

Then the reference count for the Person1 memory space is 3, and if this memory space is freed, three objects will be nil

If only the person1=nil, it will not release the memory space.

Reference count in Swift memory management

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.