{"id":2310,"date":"2022-06-02T18:50:56","date_gmt":"2022-06-02T18:50:56","guid":{"rendered":"https:\/\/sezeromer.com\/?p=2310"},"modified":"2023-03-04T14:43:44","modified_gmt":"2023-03-04T11:43:44","slug":"xcode-ile-nasil-memory-leaks-tespit-edilir","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/xcode-ile-nasil-memory-leaks-tespit-edilir\/","title":{"rendered":"How to Detect Memory Leaks with Xcode?"},"content":{"rendered":"<p>Hello friends, in this article, we will talk about how to detect <strong>Memory<\/strong> <strong>Leaks<\/strong> in our project with Xcode. <a href=\"https:\/\/sezeromer.com\/strong-unowned-weak-tipleri\/\">Here<\/a> you can find answers to questions such as what is Memory Leaks and how it occurs.<\/p>\n<p>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.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2313\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/13.gif\" alt=\"\" width=\"422\" height=\"914\" \/><\/p>\n<p>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.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/\r\n\/\/  Apartment.swift\r\n\/\/  reference-types\r\n\/\/\r\n\/\/  Created by \u00d6mer Sezer on 30.05.2022.\r\n\/\/\r\n\r\nimport Foundation\r\n\r\nclass Apartment {\r\n    let apartmentName: String\r\n    let number: Int\r\n    var tenant: Tenant?\r\n    \r\n    init(apartmentName: String, number: Int) {\r\n        self.apartmentName = apartmentName\r\n        self.number = number\r\n        print(\"Apartment \\(apartmentName) initialized\")\r\n    }\r\n    \r\n    deinit {\r\n        print(\"Apartment \\(apartmentName) deinitialized\")\r\n    }\r\n}<\/pre>\n<\/div>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/\r\n\/\/  Tenant.swift\r\n\/\/  reference-types\r\n\/\/\r\n\/\/  Created by \u00d6mer Sezer on 30.05.2022.\r\n\/\/\r\n\r\nimport Foundation\r\n\r\nclass Tenant {\r\n    let tenantName: String\r\n    var apartment: Apartment?\r\n    \r\n    init(tenantName: String) {\r\n        self.tenantName = tenantName\r\n        print(\"Tenant \\(tenantName) initialized\")\r\n    }\r\n    \r\n    deinit {\r\n        print(\"Tenant \\(tenantName) deinitialized\")\r\n    }\r\n}<\/pre>\n<\/div>\n<p>In order to check whether there are <strong>Memory<\/strong> <strong>Leaks<\/strong> in our application, we need to select <strong>Leaks<\/strong> in the <strong>Instrument<\/strong> application.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2315\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-20.53.49.png\" alt=\"\" width=\"836\" height=\"521\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-20.53.49.png 836w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-20.53.49-300x187.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-20.53.49-768x479.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-20.53.49-500x312.png 500w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-20.53.49-800x499.png 800w\" sizes=\"(max-width: 836px) 100vw, 836px\" \/><\/p>\n<p>Then we can start our application and check if Memory Leaks occur.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2316\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/14-1024x524.gif\" alt=\"\" width=\"640\" height=\"328\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/14-1024x524.gif 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/14-300x154.gif 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/14-768x393.gif 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/14-1536x786.gif 1536w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/14-500x256.gif 500w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/14-800x409.gif 800w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/14-1280x655.gif 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>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.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2317\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.32.46-1024x708.png\" alt=\"\" width=\"640\" height=\"443\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.32.46-1024x708.png 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.32.46-300x207.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.32.46-768x531.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.32.46-500x346.png 500w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.32.46-800x553.png 800w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.32.46-1280x885.png 1280w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.32.46.png 1396w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>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\u00a0<strong>Weak<\/strong>\u00a0. Our corrected model is as follows.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/\r\n\/\/  Apartment.swift\r\n\/\/  reference-types\r\n\/\/\r\n\/\/  Created by \u00d6mer Sezer on 30.05.2022.\r\n\/\/\r\n\r\nimport Foundation\r\n\r\nclass Apartment {\r\n    let apartmentName: String\r\n    let number: Int\r\n    weak var tenant: Tenant?\r\n    \r\n    init(apartmentName: String, number: Int) {\r\n        self.apartmentName = apartmentName\r\n        self.number = number\r\n        print(\"Apartment \\(apartmentName) initialized\")\r\n    }\r\n    \r\n    deinit {\r\n        print(\"Apartment \\(apartmentName) deinitialized\")\r\n    }\r\n}<\/pre>\n<\/div>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/\r\n\/\/  Tenant.swift\r\n\/\/  reference-types\r\n\/\/\r\n\/\/  Created by \u00d6mer Sezer on 30.05.2022.\r\n\/\/\r\n\r\nimport Foundation\r\n\r\nclass Tenant {\r\n    let tenantName: String\r\n    var apartment: Apartment?\r\n    \r\n    init(tenantName: String) {\r\n        self.tenantName = tenantName\r\n        print(\"Tenant \\(tenantName) initialized\")\r\n    }\r\n    \r\n    deinit {\r\n        print(\"Tenant \\(tenantName) deinitialized\")\r\n    }\r\n}<\/pre>\n<\/div>\n<p>As you can see in Gift, <strong>Memory<\/strong> <strong>Leaks<\/strong> do not occur when a model is deleted from the list.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2318\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/15-1024x524.gif\" alt=\"\" width=\"640\" height=\"328\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/15-1024x524.gif 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/15-300x154.gif 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/15-768x393.gif 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/15-1536x786.gif 1536w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/15-500x256.gif 500w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/15-800x409.gif 800w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/15-1280x655.gif 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2319\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.44.44-1024x698.png\" alt=\"\" width=\"640\" height=\"436\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.44.44-1024x698.png 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.44.44-300x204.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.44.44-768x523.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.44.44-500x341.png 500w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.44.44-800x545.png 800w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.44.44-1280x872.png 1280w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-06-02-at-21.44.44.png 1352w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>To view the project in more detail, you can find it <a href=\"https:\/\/github.com\/omersezer\/swift-projects\/tree\/main\/reference-types\">here<\/a>. If you have any questions, you can contact me by sending an e-mail or comment.<\/p>","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2315,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[327,706],"tags":[902,905,806,906,8,7,635,907,904,440,896,328,707,897],"class_list":["post-2310","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","category-swiftui","tag-cycle","tag-leaks","tag-memory","tag-memory-leaks","tag-omer","tag-omer-sezer","tag-ram","tag-retain","tag-retain-cycle","tag-sezer","tag-strong","tag-swift","tag-swiftui","tag-weak"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2310"}],"collection":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/comments?post=2310"}],"version-history":[{"count":5,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2310\/revisions"}],"predecessor-version":[{"id":2690,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2310\/revisions\/2690"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/2315"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=2310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=2310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=2310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}