Press ESC to close

How to Detect Memory Leaks with Xcode?

Hello friends, in this article, we will talk about how to detect Memory Leaks in our project with Xcode. Here you can find answers to questions such as what is Memory Leaks and how it occurs.

In the example scenario we will do, there will be an application that can add, list and delete apartments and tenants. The app will have 3 screens. These will be Splash, Adding tenants and listing screens. The operation of the application is as follows.

We have 2 different models as apartment and tenant. In the apartment model, there will be an optional tenant, and in our tenant model, we will have an apartment model. Because there may be tenants in the apartment, maybe there are not, but the tenant must live in an apartment. Our models are as follows.

//
//  Apartment.swift
//  reference-types
//
//  Created by Ömer Sezer on 30.05.2022.
//

import Foundation

class Apartment {
    let apartmentName: String
    let number: Int
    var tenant: Tenant?
    
    init(apartmentName: String, number: Int) {
        self.apartmentName = apartmentName
        self.number = number
        print("Apartment \(apartmentName) initialized")
    }
    
    deinit {
        print("Apartment \(apartmentName) deinitialized")
    }
}
//
//  Tenant.swift
//  reference-types
//
//  Created by Ömer Sezer on 30.05.2022.
//

import Foundation

class Tenant {
    let tenantName: String
    var apartment: Apartment?
    
    init(tenantName: String) {
        self.tenantName = tenantName
        print("Tenant \(tenantName) initialized")
    }
    
    deinit {
        print("Tenant \(tenantName) deinitialized")
    }
}

In order to check whether there are Memory Leaks in our application, we need to select Leaks in the Instrument application.

Then we can start our application and check if Memory Leaks occur.

As you can see in the gif, when we add a new tenant and then delete it, we can see that Memory Leaks occurs and in which models it is.

The reason is that our apartment and tenant models hold each other as Strong, so even if we delete them from the list, it does not drop to 0. For this reason, the model that we have never used continues to occupy RAM. We must define our tenant variable in our apartment model as Weak . Our corrected model is as follows.

//
//  Apartment.swift
//  reference-types
//
//  Created by Ömer Sezer on 30.05.2022.
//

import Foundation

class Apartment {
    let apartmentName: String
    let number: Int
    weak var tenant: Tenant?
    
    init(apartmentName: String, number: Int) {
        self.apartmentName = apartmentName
        self.number = number
        print("Apartment \(apartmentName) initialized")
    }
    
    deinit {
        print("Apartment \(apartmentName) deinitialized")
    }
}
//
//  Tenant.swift
//  reference-types
//
//  Created by Ömer Sezer on 30.05.2022.
//

import Foundation

class Tenant {
    let tenantName: String
    var apartment: Apartment?
    
    init(tenantName: String) {
        self.tenantName = tenantName
        print("Tenant \(tenantName) initialized")
    }
    
    deinit {
        print("Tenant \(tenantName) deinitialized")
    }
}

As you can see in Gift, Memory Leaks do not occur when a model is deleted from the list.

To view the project in more detail, you can find it here. If you have any questions, you can contact me by sending an e-mail or comment.

Leave a Reply

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