{"id":5497,"date":"2014-03-30T23:08:16","date_gmt":"2014-03-30T23:08:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/trimexcess-on-listof-t-in-net-collection-of-common-programming-errors\/"},"modified":"2014-03-30T23:08:16","modified_gmt":"2014-03-30T23:08:16","slug":"trimexcess-on-listof-t-in-net-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/trimexcess-on-listof-t-in-net-collection-of-common-programming-errors\/","title":{"rendered":"Trimexcess on List(Of t) in .net-Collection of common programming errors"},"content":{"rendered":"<p>Calling <code>TrimExcess<\/code> after you load the list will likely save you some memory. But remember that if the items in your list are of a reference type (and strings are reference types), then all you&#8217;re saving is the memory required to hold the references.<\/p>\n<p>So, for example, if you have a <code>List(of String)<\/code> that&#8217;s allocated for 2,000 items and there are only 1,000 items in it, calling <code>TrimExcess<\/code> is going to save you the memory occupied by 1,000 references. That&#8217;s 4,000 bytes on the 32-bit runtime, and 8,000 bytes on the 64-bit runtime.<\/p>\n<p>As Andrew Hare mentioned, calling <code>TrimExcess<\/code> after loading a list that&#8217;s going to hang around in memory for a while is probably a good thing. You might also consider calling <code>TrimExcess<\/code> if you remove a whole bunch of things from a list, and then you&#8217;re going to keep the list around. But calling <code>TrimExcess<\/code> repeatedly for the same list, unless it&#8217;s really getting large, is just wasting time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Calling TrimExcess after you load the list will likely save you some memory. But remember that if the items in your list are of a reference type (and strings are reference types), then all you&#8217;re saving is the memory required to hold the references. So, for example, if you have a List(of String) that&#8217;s allocated [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5497","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5497"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5497\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}