{"id":12907,"date":"2015-11-24T20:00:19","date_gmt":"2015-11-25T01:00:19","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12907"},"modified":"2015-11-25T09:37:35","modified_gmt":"2015-11-25T14:37:35","slug":"how-swift-implements-unowned-and-weak-references","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/11\/24\/how-swift-implements-unowned-and-weak-references\/","title":{"rendered":"How Swift Implements Unowned and Weak References"},"content":{"rendered":"<p><a href=\"https:\/\/twitter.com\/jckarter\/status\/654819932962598913\">Joe<\/a> <a href=\"https:\/\/twitter.com\/jckarter\/status\/667361480371630081\">Groff<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/654819932962598913\"><p>Unowned is faster and allows for immutability and nonoptionality. If you don&rsquo;t need weak, don&rsquo;t use it.<\/p><\/blockquote>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/667361480371630081\"><p>unowned uses a second refcount in the object. weak refs are tracked in a global table.<\/p><\/blockquote>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/667363293309177856\"><p>It&rsquo;s a space\/time tradeoff. unowned can&rsquo;t free memory until unowned refs die, but weak frees immediately when strong refs die.<\/p><\/blockquote>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/667364165057515521\"><p>Yeah, so we can check whether the object is still alive before strong-retaining it again.<\/p><\/blockquote>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/667367466465529856\"><p>The object is destroyed and gives up all its resources when the last strong reference is released.<\/p><\/blockquote>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/667367659198001152\"><p>The memory for the instance is still allocated but left in a zombie state.<\/p><\/blockquote>\n<p>This is so that Swift can guarantee that if you try to access it you get an error rather than a crash or the wrong data. If you <a href=\"https:\/\/twitter.com\/jckarter\/status\/667370460292579329\">don&rsquo;t want<\/a> that overhead:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/667370460292579329\"><p>There&rsquo;s unowned(unsafe), which is completely unmanaged.<\/p><\/blockquote>\n<p>With weak references, the memory can be freed immediately. This is safe because the references are zeroed, but it&rsquo;s more cumbersome because you have to deal with optionals.<\/p>","protected":false},"excerpt":{"rendered":"<p>Joe Groff: Unowned is faster and allows for immutability and nonoptionality. If you don&rsquo;t need weak, don&rsquo;t use it. unowned uses a second refcount in the object. weak refs are tracked in a global table. It&rsquo;s a space\/time tradeoff. unowned can&rsquo;t free memory until unowned refs die, but weak frees immediately when strong refs die. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[55,46,571,138,71,901,943],"class_list":["post-12907","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-languagedesign","tag-memory-management","tag-optimization","tag-programming","tag-swift-programming-language","tag-swift-runtime"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=12907"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12907\/revisions"}],"predecessor-version":[{"id":12908,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12907\/revisions\/12908"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}