{"id":5480,"date":"2019-08-29T17:34:14","date_gmt":"2019-08-29T12:04:14","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=5480"},"modified":"2026-01-14T11:52:57","modified_gmt":"2026-01-14T06:22:57","slug":"automatic-reference-counting-arc-and-memory-management-in-swift","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/","title":{"rendered":"Automatic Reference Counting (ARC) and Memory Management in Swift"},"content":{"rendered":"<p class=\"p1\"><b>Introduction:<\/b><span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p class=\"p1\">Memory management is a key factor when we developing apps. If a program is using a lot of memory it can affect badly on your device making apps run slowly or even cause crashes. So for that in swift, you\u00a0can work with Automatic Reference Counting (ARC) to keep your apps memory usage minimal. This doesn\u2019t mean you can forget about the memory in your app but it does take care of most things for you.<\/p>\n<p class=\"p2\">It\u2019s Important to note that ARC only works when working with classes, structs and enums are value types and therefore are stored differently in memory than classes which are stored and passed by reference.<\/p>\n<p class=\"p1\"><b>Memory Problems<\/b><\/p>\n<p class=\"p1\"><strong>1-<\/strong> Your app uses too much of phone memory that causes your app to perform badly or can even crash or unexpected things can happen for example:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-5489\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-1-300x167.png\" alt=\"\" width=\"729\" height=\"406\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-1-300x167.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-1-768x427.png 768w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-1-1024x569.png 1024w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-1-624x347.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-1.png 1141w\" sizes=\"(max-width: 729px) 100vw, 729px\" \/><\/p>\n<p class=\"p1\">We have viewController, label or a tableView that loaded in memory and for some reason, if you forgot to use dequeuereusablecell the load all cells in the array, so use so this type of things can cause your app memory problem.<\/p>\n<p class=\"p1\"><strong>1-<\/strong> Adding something to the memory and then it never gets removed, like you have a view controller and that shows a popup and then popup closes but it never removes if you use that popup so many time then it use so much memory and never get released from memory.<\/p>\n<p class=\"p1\"><span style=\"font-size: 1rem;\"><strong>2-<\/strong> You assign something to memory but unexpected remove from memory so then you try to access it or try to get the value of this then it results in a crash because this doesn\u2019t exist in the memory anymore.<\/span><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/span><\/p>\n<p class=\"p1\">So these are three different memory problems that are not all but for now, we are going with there.<\/p>\n<p class=\"p3\"><b>How these things happen:<\/b><\/p>\n<p class=\"p1\">There are two different types in swift that you need to understand with,<\/p>\n<p class=\"p1\"><strong>1- Value types:<\/strong>\u00a0This type can be like String, Int, Stuck, Enum, Array, Dictionary etc, for value type each instance (like String or Int) hold on its value and if you use it somewhere else it makes a copy of it and passes around your application.<\/p>\n<p class=\"p1\"><strong>2- Reference Types:<\/strong> Class is a reference type, for reference type instances, share a single copy of the data. So if you use or pass around your app then only one copy exists and you just passing a pointer or reference back to your class.<\/p>\n<p class=\"p1\"><strong>Ex:<\/strong> write this in Playground then run.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-5482\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/2-300x188.png\" alt=\"\" width=\"721\" height=\"452\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/2-300x188.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/2.png 406w\" sizes=\"(max-width: 721px) 100vw, 721px\" \/><\/p>\n<p class=\"p1\">The job\u00a0has a default value of\u00a0<b>2<\/b>, and Job first instance created called\u00a0person. It will have a\u00a0job\u00a0of\u00a0<b>2<\/b>.<\/p>\n<p class=\"p1\">Now, create a new object called\u00a0new\u00a0and assign the previous object to this like as in the code above.<\/p>\n<p class=\"p1\">Now change the\u00a0job\u00a0of\u00a0new\u00a0to\u00a0<b>5<\/b>\u00a0and then print the\u00a0job of both the instance. Both will print the number\u00a0<b>5<\/b>.<\/p>\n<p class=\"p1\">This is because the class is a reference type.<\/p>\n<p class=\"p3\"><b>When will swift remove a reference type from memory?<\/b><\/p>\n<p class=\"p3\"><strong>*\u00a0<\/strong> When there are no more variables pointing to the reference type, Swift uses ARC to for this work.<\/p>\n<p class=\"p3\"><b>How ARC works :<\/b><\/p>\n<p class=\"p1\">First, create a project with swift language and then select your ViewController.Swift file.<\/p>\n<p class=\"p1\">Add the following class to the bottom of your\u00a0<b>ViewController.swift:<br \/>\n<\/b><\/p>\n<p><img decoding=\"async\" class=\"wp-image-5483 aligncenter\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/3-300x193.png\" alt=\"\" width=\"731\" height=\"470\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/3-300x193.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/3-624x401.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/3.png 671w\" sizes=\"(max-width: 731px) 100vw, 731px\" \/><\/p>\n<p class=\"p1\">This is very simple viewController and a class named MyClass, in DidLoad just initiate the class and create a couple more variables to pointing to the same class and then set them all to nil. See the MyClass code there is a definite function in there this is called right before the object remove from memory. So there is three-pointer (three reference count for MyClass), once you nil-all of them then you can see the print statements.<\/p>\n<p class=\"p1\">Now let&#8217;s see the memory problem 2 in which I mention adding something to the memory and then it never gets removed.<\/p>\n<p class=\"p1\">Add the following class to the bottom of you \u00a0<b>ViewController.swift:<\/b><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5484\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/4-300x237.png\" alt=\"\" width=\"716\" height=\"566\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/4-300x237.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/4-624x494.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/4.png 734w\" sizes=\"(max-width: 716px) 100vw, 716px\" \/><\/p>\n<p>So here in this example, We are using two-class Job and Person, Job class have a variable named the person with the type of Person Class and similarly, Person class have a variable named job with type of Job Class, and we create some properties in DidLoad()<\/p>\n<p>Variables &#8211; joe, dev that we create in DIdLoad(), they both have taken their Space and Memory and both having reference count 1.<br \/>\njoe is a Person type class variable and dev is a Job type class variable. and then we give joe a Job,<\/p>\n<p style=\"text-align: left;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <strong>\u00a0 \u00a0 \u00a0 \u00a0joe?.job = dev<\/strong><\/p>\n<p>So class var joe\u2019s job variable will now reference of dev object coz of this Job have 2 reference count means two objects pointing to it.<\/p>\n<p>Similarly with dev with Person Class,<\/p>\n<p><strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 dev?.person = joe<\/strong><\/p>\n<p>Now the person variable of dev point to Person Class so it also has 2 reference count.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5490\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-2-300x167.png\" alt=\"\" width=\"711\" height=\"396\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-2-300x167.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-2-768x427.png 768w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-2-1024x569.png 1024w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-2-624x347.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-2.png 1141w\" sizes=\"(max-width: 711px) 100vw, 711px\" \/><\/p>\n<p class=\"p1\">So what happens if we did use their variables, and we set them to nil,<\/p>\n<p class=\"p1\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/span>joe = <b>nil<\/b><\/p>\n<p class=\"p1\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span>dev = <b>nil<\/b><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5491\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-3-300x167.png\" alt=\"\" width=\"751\" height=\"418\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-3-300x167.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-3-768x427.png 768w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-3-1024x569.png 1024w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-3-624x347.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/iOS-3.png 1141w\" sizes=\"(max-width: 751px) 100vw, 751px\" \/><\/p>\n<p class=\"p1\">but still, there no deallocation print statement in console and Job and Person Class can\u2019t be removed from memory because of the job and person variable point to them.<\/p>\n<p class=\"p1\">and this is called Retain cycle.<\/p>\n<p class=\"p3\"><b>Retain Cycle : <\/b><\/p>\n<p class=\"p3\">Retain because apple holds on to these object and not let them go with the reference count &gt; 1 and Cycle because of both object reference each other.<\/p>\n<p class=\"p3\"><b>how you can detect these problems in Xcode?<\/b><\/p>\n<p class=\"p1\"><strong>*\u00a0<\/strong> By using Instruments tool.<\/p>\n<p class=\"p1\">In Xcode, Go to Product and click on profile,<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5492\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/7-300x121.png\" alt=\"\" width=\"739\" height=\"298\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/7-300x121.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/7-768x309.png 768w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/7-624x251.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/7.png 814w\" sizes=\"(max-width: 739px) 100vw, 739px\" \/><\/p>\n<p class=\"p1\">Then a new tool will open name Instruments then select Leaks and choose.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5493\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/8-300x173.png\" alt=\"\" width=\"739\" height=\"426\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/8-300x173.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/8-768x442.png 768w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/8-1024x589.png 1024w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/8-624x359.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/8.png 1278w\" sizes=\"(max-width: 739px) 100vw, 739px\" \/><\/p>\n<p class=\"p1\">Then you can see it point to our simulator and show next to our app as a target to run.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5494\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/9-300x188.png\" alt=\"\" width=\"737\" height=\"462\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/9-300x188.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/9-768x480.png 768w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/9-1024x640.png 1024w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/9-624x390.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/9.png 1280w\" sizes=\"(max-width: 737px) 100vw, 737px\" \/><\/p>\n<p class=\"p1\">Then you just need to tap on the red button and it starts recording.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5495\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/10-300x142.png\" alt=\"\" width=\"649\" height=\"307\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/10-300x142.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/10-768x363.png 768w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/10-624x295.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/10.png 788w\" sizes=\"(max-width: 649px) 100vw, 649px\" \/><\/p>\n<p class=\"p1\">You can see the red cross sign showing two leaks, just tap on it, then you can see what objects are leaking like job and Person here.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5496\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/11-300x134.png\" alt=\"\" width=\"656\" height=\"293\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/11-300x134.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/11-768x344.png 768w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/11-624x279.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/11.png 1017w\" sizes=\"(max-width: 656px) 100vw, 656px\" \/><\/p>\n<p class=\"p1\">You can look it a different way to like click on Leaks then it will show like three options (Leaks, Cycles &amp; Roots and Call Tree).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5508\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-19-at-2.27.06-PM-300x170.png\" alt=\"\" width=\"632\" height=\"358\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-19-at-2.27.06-PM-300x170.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-19-at-2.27.06-PM-768x434.png 768w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-19-at-2.27.06-PM-1024x579.png 1024w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-19-at-2.27.06-PM-624x353.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-19-at-2.27.06-PM.png 1276w\" sizes=\"(max-width: 632px) 100vw, 632px\" \/><\/p>\n<p class=\"p1\">Choose Cycles &amp; Roots, then a cycle graph shows (Retain Cycle).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5498\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-07-at-6.19.39-PM-300x99.png\" alt=\"\" width=\"658\" height=\"217\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-07-at-6.19.39-PM-300x99.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-07-at-6.19.39-PM-768x252.png 768w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-07-at-6.19.39-PM-624x205.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-07-at-6.19.39-PM.png 1022w\" sizes=\"(max-width: 658px) 100vw, 658px\" \/><\/p>\n<p class=\"p1\">This is what actual problem with memory. They both pointing to each other.<\/p>\n<p class=\"p3\"><b>How to fix a Retain Cycle:<\/b><\/p>\n<p class=\"p1\">Using Weak or Unowned reference instead of Strong reference, because weak or unowned reference does not increment the reference count.<\/p>\n<p class=\"p1\"><strong>Strong Reference :<\/strong><\/p>\n<p class=\"p1\">It is by default if you make a reference without using weak or unowned then it is a strong reference.<\/p>\n<p class=\"p1\"><strong>Weak Reference:<\/strong><\/p>\n<p class=\"p1\">For weak reference child may or may not exist if the parent removes from memory. It&#8217;s optional it can be set to nil.<\/p>\n<p class=\"p1\"><strong>Unowned Reference:<\/strong><\/p>\n<p class=\"p1\">In this case, child definitely exists all the time but it is removed when the parent is removed so it is not an optional thing and we can\u2019t set it to nil.<\/p>\n<p class=\"p1\">So let\u2019s fix the Retain Cycle in Code.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5499\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-07-at-6.29.36-PM-279x300.png\" alt=\"\" width=\"745\" height=\"801\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-07-at-6.29.36-PM-279x300.png 279w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-07-at-6.29.36-PM.png 514w\" sizes=\"(max-width: 745px) 100vw, 745px\" \/><\/p>\n<p class=\"p1\">I just make the job a weak variable then run the app, so it&#8217;s print the statement of deallocating Job and Person.<\/p>\n<p class=\"p1\">Now if we run the Instrument app then again record our app into it then we can see the green checkmark in place of the red cross. And its showing no leaks as well<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5500\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-07-at-6.33.16-PM-300x247.png\" alt=\"\" width=\"691\" height=\"569\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-07-at-6.33.16-PM-300x247.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2019-08-07-at-6.33.16-PM.png 426w\" sizes=\"(max-width: 691px) 100vw, 691px\" \/><\/p>\n<p class=\"p1\">This is how you can track and resolve the Retain Cycle and also can solve some memory leaks problems.<\/p>\n<p>InnovationM is a globally renowned mobile app development company in India that caters a robust &amp; secure Android app development, iOS app development, hybrid app development services. Our commitment &amp; engagement towards our target gives us brighter in the world of technology and has led us to establish success stories consecutively. We are a leading iOS app development company in India &amp; Android app development company in India.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction:\u00a0 Memory management is a key factor when we developing apps. If a program is using a lot of memory it can affect badly on your device making apps run slowly or even cause crashes. So for that in swift, you\u00a0can work with Automatic Reference Counting (ARC) to keep your apps memory usage minimal. This [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5515,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,380],"tags":[],"class_list":["post-5480","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift-5"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Automatic Reference Counting (ARC) and Memory Management in Swift - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Automatic Reference Counting (ARC) and Memory Management in Swift - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"Introduction:\u00a0 Memory management is a key factor when we developing apps. If a program is using a lot of memory it can affect badly on your device making apps run slowly or even cause crashes. So for that in swift, you\u00a0can work with Automatic Reference Counting (ARC) to keep your apps memory usage minimal. This [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-29T12:04:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-01-14T06:22:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/ios-4.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1285\" \/>\n\t<meta property=\"og:image:height\" content=\"805\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Automatic Reference Counting (ARC) and Memory Management in Swift\",\"datePublished\":\"2019-08-29T12:04:14+00:00\",\"dateModified\":\"2026-01-14T06:22:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/\"},\"wordCount\":1290,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/ios-4.png\",\"articleSection\":[\"iOS\",\"Swift 5\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/\",\"name\":\"Automatic Reference Counting (ARC) and Memory Management in Swift - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/ios-4.png\",\"datePublished\":\"2019-08-29T12:04:14+00:00\",\"dateModified\":\"2026-01-14T06:22:57+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/ios-4.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/ios-4.png\",\"width\":1285,\"height\":805,\"caption\":\"Automatic Reference Counting (ARC) and Memory Management in Swift\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/automatic-reference-counting-arc-and-memory-management-in-swift\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Automatic Reference Counting (ARC) and Memory Management in Swift\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Automatic Reference Counting (ARC) and Memory Management in Swift - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/","og_locale":"en_US","og_type":"article","og_title":"Automatic Reference Counting (ARC) and Memory Management in Swift - InnovationM - Blog","og_description":"Introduction:\u00a0 Memory management is a key factor when we developing apps. If a program is using a lot of memory it can affect badly on your device making apps run slowly or even cause crashes. So for that in swift, you\u00a0can work with Automatic Reference Counting (ARC) to keep your apps memory usage minimal. This [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/","og_site_name":"InnovationM - Blog","article_published_time":"2019-08-29T12:04:14+00:00","article_modified_time":"2026-01-14T06:22:57+00:00","og_image":[{"width":1285,"height":805,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/ios-4.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Automatic Reference Counting (ARC) and Memory Management in Swift","datePublished":"2019-08-29T12:04:14+00:00","dateModified":"2026-01-14T06:22:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/"},"wordCount":1290,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/ios-4.png","articleSection":["iOS","Swift 5"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/","url":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/","name":"Automatic Reference Counting (ARC) and Memory Management in Swift - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/ios-4.png","datePublished":"2019-08-29T12:04:14+00:00","dateModified":"2026-01-14T06:22:57+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/ios-4.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/08\/ios-4.png","width":1285,"height":805,"caption":"Automatic Reference Counting (ARC) and Memory Management in Swift"},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/automatic-reference-counting-arc-and-memory-management-in-swift\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Automatic Reference Counting (ARC) and Memory Management in Swift"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/5480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=5480"}],"version-history":[{"count":1,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/5480\/revisions"}],"predecessor-version":[{"id":8927,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/5480\/revisions\/8927"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/5515"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=5480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=5480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=5480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}