{"id":4758,"date":"2014-03-30T15:14:32","date_gmt":"2014-03-30T15:14:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-i-optimize-the-code-to-always-use-the-same-reference-to-a-string-without-increasing-the-memory-usage-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:14:32","modified_gmt":"2014-03-30T15:14:32","slug":"how-can-i-optimize-the-code-to-always-use-the-same-reference-to-a-string-without-increasing-the-memory-usage-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-i-optimize-the-code-to-always-use-the-same-reference-to-a-string-without-increasing-the-memory-usage-collection-of-common-programming-errors\/","title":{"rendered":"How can I optimize the code to always use the same reference to a String without increasing the memory usage?-Collection of common programming errors"},"content":{"rendered":"<p>I am working on an application that has a lot of duplicate Strings and my task is to eliminate them to decrease memory usage. My first thought was to use <code>String.intern<\/code> to guarantee that only one reference of a String would exist. It worked to decrease the heap memory, but it increased the PermGen way too much; in fact, because there are many strings that are declared only once, the total amount of memory used by the application increased, actually.<\/p>\n<p>After searching for another ideas, I found this approach: http:\/\/stackoverflow.com\/a\/725822\/1384913.<\/p>\n<p>It happened the same thing as String.intern: The String usage decreased, but the memory that I saved is being used in the <code>WeakHashMap<\/code> and <code>WeakHashMap$Entry<\/code> classes.<\/p>\n<p>Is there an effective way to maintain only one reference for each String that doesn&#8217;t spend the same amount of memory that I&#8217;m recovering doing it?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am working on an application that has a lot of duplicate Strings and my task is to eliminate them to decrease memory usage. My first thought was to use String.intern to guarantee that only one reference of a String would exist. It worked to decrease the heap memory, but it increased the PermGen way [&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-4758","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4758","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=4758"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4758\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}