{"id":1728,"date":"2020-05-08T15:17:55","date_gmt":"2020-05-08T14:17:55","guid":{"rendered":"https:\/\/gdksoftware.com\/?post_type=knowledge&#038;p=1728"},"modified":"2023-02-13T20:54:30","modified_gmt":"2023-02-13T19:54:30","slug":"smartpointers-em-delphi","status":"publish","type":"knowledge","link":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi","title":{"rendered":"Smartpointers em Delphi"},"content":{"rendered":"<p>Como voc\u00ea sabe, a Delphi n\u00e3o tem um coletor de lixo. O ARC logo ser\u00e1 <a href=\"https:\/\/blog.marcocantu.com\/blog\/2018-october-Delphi-ARC-directions.html\">removido<\/a> do compilador sem janelas. Portanto, a Delphi n\u00e3o tem estrutura para liberar objetos automaticamente. Fizemos algumas pesquisas para ver se os Smartpointers s\u00e3o adequados para a libera\u00e7\u00e3o autom\u00e1tica de objetos. O conceito de Smartpointers \u00e9 emprestado do C++ e foi introduzido pela primeira vez na Delphi por Barry Kelly. A estrutura da Spring4D tamb\u00e9m tem uma implementa\u00e7\u00e3o de Smartpointers.<\/p>\n<p>Para mostrar um exemplo do uso de Smartpointers, vamos dar uma olhada em algum c\u00f3digo:<\/p>\n<pre>var\r\n  s: TStringList;\r\nbegin\r\n  s := TStringList.Create;\r\ntry\r\n  DoSomething(s);\r\nfinally\r\n  s.Free;\r\nend;<\/pre>\n<p>Com os Smartpointers, o c\u00f3digo ser\u00e1 parecido com este:<\/p>\n<pre>var\r\n  s: IShared&lt;TStringlist&gt;;\r\nbegin\r\n  s := Shared.Make(TStringList.Create);\r\n  DoSomething(s);\r\nend;<\/pre>\n<p>Escrevemos um programa de teste para testar um cen\u00e1rio mais complexo. Aqui est\u00e1 o c\u00f3digo do programa de teste:<\/p>\n<pre>program TestSmartPointer;\r\n\r\n{$APPTYPE CONSOLE}\r\n\r\nuses\r\n  Spring,\r\n  Diagnostics,\r\n  Classes,\r\n  SysUtils,\r\n  System.Generics.Collections;\r\n\r\ntype\r\n  TTestObj = class\r\n  private\r\n    FDescription: string;\r\n  public\r\n    property Description: string read FDescription write FDescription;\r\n    destructor Destroy; override;\r\n  end;\r\n\r\n  TTestList = class(TList&lt;IShared&lt;TTestObj&gt;&gt;)\r\n  public\r\n    destructor Destroy; override;\r\n  end;\r\n\r\nprocedure Test_SmartPointer;\r\nvar\r\n  lTestList: IShared&lt;TTestList&gt;;\r\n  lTestObj: IShared&lt;TTestObj&gt;;\r\n  i: integer;\r\nbegin\r\n  writeln('SmartPointer test started');\r\n  lTestList := Shared.Make(TTestList.Create);\r\n  for i := 1 to 10 do\r\n  begin\r\n    lTestObj := Shared.Make(TTestObj.Create);\r\n    lTestObj.Description := i.ToString;\r\n    writeln(format('TestObj with description %s added to Testlist', [lTestObj.Description]));\r\n    lTestList.Add(lTestObj);\r\n  end;\r\n  \r\n  for lTestObj in lTestList do\r\n    writeln(format('Item %d of Testlist has value %s', lTestList.IndexOf(lTestObj), lTestObj.Description));\r\n\r\n  writeln('SmartPointer test finished');\r\nend;\r\n\r\n{ TTestObj }\r\n\r\ndestructor TTestObj.Destroy;\r\nbegin\r\n  writeln(format('TestObj with description %s is destroyed', [FDescription]));\r\n  inherited;\r\nend;\r\n\r\n{ TTestList }\r\n\r\ndestructor TTestList.Destroy;\r\nbegin\r\n  writeln('TTestList is destroyed');\r\n  inherited;\r\nend;\r\n\r\nbegin\r\n  Test_SmartPointer;\r\n  readln;\r\nend.<\/pre>\n<p>Neste programa, uma lista gen\u00e9rica TTestList \u00e9 preenchida com objetos TTestObj. Observe a declara\u00e7\u00e3o da Lista TTestList:<\/p>\n<p>TTestList = class(TList&lt;IShared&lt;TTestObj&gt;&gt;)<\/p>\n<p>A sa\u00edda deste programa de teste est\u00e1 na figura abaixo.<\/p>\n<p><img decoding=\"async\" class=\"img-fluid alignnone wp-image-1729 size-full\" src=\"https:\/\/gdksoftware.com\/wp-content\/uploads\/2020\/07\/1.png\" alt=\"\" width=\"479\" height=\"567\" srcset=\"https:\/\/gdksoftware.com\/wp-content\/uploads\/2020\/07\/1.png 479w, https:\/\/gdksoftware.com\/wp-content\/uploads\/2020\/07\/1-253x300.png 253w\" sizes=\"(max-width: 479px) 100vw, 479px\" \/><\/p>\n<p>Como voc\u00ea pode ver, os objetos s\u00e3o liberados ap\u00f3s a linha &#8220;Teste SmartPointer terminado&#8221;. Assim, quando o procedimento Test_SmartPointer \u00e9 finalizado, as vari\u00e1veis locais saem do escopo e s\u00e3o automaticamente liberadas. Isto funciona bem.<br \/>\nTamb\u00e9m testamos o desempenho e a mem\u00f3ria utilizada. Os resultados s\u00e3o os seguintes:<\/p>\n<p><img decoding=\"async\" class=\"img-fluid alignnone wp-image-1731 size-full\" src=\"https:\/\/gdksoftware.com\/wp-content\/uploads\/2020\/07\/2.png\" alt=\"\" width=\"556\" height=\"252\" srcset=\"https:\/\/gdksoftware.com\/wp-content\/uploads\/2020\/07\/2.png 556w, https:\/\/gdksoftware.com\/wp-content\/uploads\/2020\/07\/2-300x136.png 300w\" sizes=\"(max-width: 556px) 100vw, 556px\" \/><\/p>\n<p>No teste de velocidade foram criadas 100.000 listas e cada lista foi preenchida com 100 itens. Os smartpointers s\u00e3o 29,17% mais lentos do que quando a t\u00e9cnica cl\u00e1ssica \u00e9 utilizada.<br \/>\nNo teste de mem\u00f3ria 1 a lista foi criada com 100.000 itens. Quando a lista foi totalmente preenchida, a mem\u00f3ria utilizada pelo programa foi medida. Os smartpointers usam 26,94% mais mem\u00f3ria do que quando a t\u00e9cnica cl\u00e1ssica \u00e9 usada. Isto s\u00e3o diferen\u00e7as consider\u00e1veis.<\/p>\n<p><strong>Conclus\u00e3o<\/strong><\/p>\n<p>Smartpointers funcionam tecnicamente bem. Os objetos s\u00e3o liberados conforme o esperado. No entanto, h\u00e1 uma desvantagem consider\u00e1vel quando se trata de velocidade e mem\u00f3ria usada. Al\u00e9m disso, todo objeto deve ser declarado com IShared&lt;object&gt; e criado com Shared.Make(object.create). Nossa recomenda\u00e7\u00e3o de smartpointers \u00e9 que \u00e9 um pouco demais em desempenho e c\u00f3digo para ser usado como uma estrutura geral f\u00e1cil de usar para liberar objetos automaticamente no Delphi.<\/p>\n<p>References:<\/p>\n<p><a href=\"https:\/\/blog.marcocantu.com\/blog\/2018-october-Delphi-ARC-directions.html\">https:\/\/blog.marcocantu.com\/blog\/2018-october-Delphi-ARC-directions.html<\/a><\/p>\n<p><a href=\"http:\/\/blog.barrkel.com\/2008\/09\/smart-pointers-in-delphi.html\">http:\/\/blog.barrkel.com\/2008\/09\/smart-pointers-in-delphi.html<\/a><\/p>\n<p><a href=\"https:\/\/delphisorcery.blogspot.com\/2015\/01\/smart-pointers-in-delphi.html\">https:\/\/delphisorcery.blogspot.com\/2015\/01\/smart-pointers-in-delphi.html<\/a><\/p>\n<p><a href=\"https:\/\/en.delphipraxis.net\/topic\/402-smart-pointers-generics-vrs-non-generic-implementastion\/\">https:\/\/en.delphipraxis.net\/topic\/402-smart-pointers-generics-vrs-non-generic-implementastion\/<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"featured_media":0,"parent":0,"template":"","class_list":["post-1728","knowledge","type-knowledge","status-publish","hentry"],"acf":{"author":"","type_hero":"compact","hero_image":1027,"hero_image_position":"","hero_title":"","hero_content":"","hero_link":null,"hero_show_h1":false},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Smartpointers em Delphi - GDK Software<\/title>\n<meta name=\"description\" content=\"Fizemos algumas pesquisas para ver se os Smartpointers s\u00e3o adequados para a libera\u00e7\u00e3o autom\u00e1tica de objetos.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Smartpointers em Delphi\" \/>\n<meta property=\"og:description\" content=\"Fizemos algumas pesquisas para ver se os Smartpointers s\u00e3o adequados para a libera\u00e7\u00e3o autom\u00e1tica de objetos.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi\" \/>\n<meta property=\"og:site_name\" content=\"GDK Software\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-13T19:54:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/gdksoftware.com\/wp-content\/uploads\/2020\/07\/1.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. tempo de leitura\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/smartpointers-em-delphi\",\"url\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/smartpointers-em-delphi\",\"name\":\"Smartpointers em Delphi - GDK Software\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/smartpointers-em-delphi#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/smartpointers-em-delphi#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/gdksoftware.com\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/1.png\",\"datePublished\":\"2020-05-08T14:17:55+00:00\",\"dateModified\":\"2023-02-13T19:54:30+00:00\",\"description\":\"Fizemos algumas pesquisas para ver se os Smartpointers s\u00e3o adequados para a libera\u00e7\u00e3o autom\u00e1tica de objetos.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/smartpointers-em-delphi#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/smartpointers-em-delphi\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/smartpointers-em-delphi#primaryimage\",\"url\":\"https:\\\/\\\/gdksoftware.com\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/1.png\",\"contentUrl\":\"https:\\\/\\\/gdksoftware.com\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/1.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/smartpointers-em-delphi#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Knowledgebase\",\"item\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Delphi\",\"item\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/knowledgebase-category\\\/arquivo-delphi\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Smartpointers em Delphi\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br#website\",\"url\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\",\"name\":\"GDK Software\",\"description\":\"Zet de stip op je horizon\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pt-BR\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Smartpointers em Delphi - GDK Software","description":"Fizemos algumas pesquisas para ver se os Smartpointers s\u00e3o adequados para a libera\u00e7\u00e3o autom\u00e1tica de objetos.","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:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi","og_locale":"pt_BR","og_type":"article","og_title":"Smartpointers em Delphi","og_description":"Fizemos algumas pesquisas para ver se os Smartpointers s\u00e3o adequados para a libera\u00e7\u00e3o autom\u00e1tica de objetos.","og_url":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi","og_site_name":"GDK Software","article_modified_time":"2023-02-13T19:54:30+00:00","og_image":[{"url":"https:\/\/gdksoftware.com\/wp-content\/uploads\/2020\/07\/1.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. tempo de leitura":"3 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi","url":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi","name":"Smartpointers em Delphi - GDK Software","isPartOf":{"@id":"https:\/\/gdksoftware.com\/pt-br#website"},"primaryImageOfPage":{"@id":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi#primaryimage"},"image":{"@id":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi#primaryimage"},"thumbnailUrl":"https:\/\/gdksoftware.com\/wp-content\/uploads\/2020\/07\/1.png","datePublished":"2020-05-08T14:17:55+00:00","dateModified":"2023-02-13T19:54:30+00:00","description":"Fizemos algumas pesquisas para ver se os Smartpointers s\u00e3o adequados para a libera\u00e7\u00e3o autom\u00e1tica de objetos.","breadcrumb":{"@id":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi"]}]},{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi#primaryimage","url":"https:\/\/gdksoftware.com\/wp-content\/uploads\/2020\/07\/1.png","contentUrl":"https:\/\/gdksoftware.com\/wp-content\/uploads\/2020\/07\/1.png"},{"@type":"BreadcrumbList","@id":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/smartpointers-em-delphi#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gdksoftware.com\/pt-br"},{"@type":"ListItem","position":2,"name":"Knowledgebase","item":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento"},{"@type":"ListItem","position":3,"name":"Delphi","item":"https:\/\/gdksoftware.com\/pt-br\/knowledgebase-category\/arquivo-delphi"},{"@type":"ListItem","position":4,"name":"Smartpointers em Delphi"}]},{"@type":"WebSite","@id":"https:\/\/gdksoftware.com\/pt-br#website","url":"https:\/\/gdksoftware.com\/pt-br","name":"GDK Software","description":"Zet de stip op je horizon","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gdksoftware.com\/pt-br?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pt-BR"}]}},"_links":{"self":[{"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/knowledge\/1728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/knowledge"}],"about":[{"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/types\/knowledge"}],"wp:attachment":[{"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/media?parent=1728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}