{"id":2817,"date":"2018-04-10T12:39:17","date_gmt":"2018-04-10T12:39:17","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2817"},"modified":"2018-04-10T12:39:56","modified_gmt":"2018-04-10T12:39:56","slug":"item-6-eliminate-obsolete-object-reference","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/item-6-eliminate-obsolete-object-reference\/","title":{"rendered":"Item 6 : Eliminate Obsolete Object Reference"},"content":{"rendered":"<p>An <strong>obsolete reference<\/strong> is one that is kept around but will never be used, preventing the object it refers to from being eligible for garbage collection, thus causing a memory leak.<\/p>\n<p><strong>Manually Setting to NULL<\/strong><br \/>\nNulling out a reference to remove obsolete references to an object is good, but one must not overdo it. The best way to eliminate an obsolete reference is to reuse the variable in which it was contained or to let it fall out of scope.<\/p>\n<p>Lets take a Simple Stack implementation as in effective Java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic Object pop() {\r\n    if (size == 0)\r\n        throw new EmptyStackException();\r\n    Object result = elements&#x5B;--size];\r\n    elements&#x5B;size] = null; \/\/ Eliminate obsolete reference\r\n    return result;\r\n}\r\n<\/pre>\n<p>In the above code you can see that the stack size is shrinked when ever a pop operation is carried out and is set to null allowing the garbage collector to access the unused space to reclaim<\/p>\n<p><strong>Using WeakHashMap<\/strong><br \/>\nWeakHashMap is an implementation of the Map interface. WeakHashMap is almost same as HashMap except in case of WeakHashMap, if object is specified as key doesn\u2019t contain any references- it is eligible for garbage collection even though it is associated with WeakHashMap. i.e Garbage Collector dominates over WeakHashMap.<\/p>\n<p><strong>How HashMap Works<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/ Java program to illustrate \r\n\/\/ Hashmap \r\nimport java.util.*;\r\nclass HashMapDemo\r\n{\r\n    public static void main(String args&#x5B;])throws Exception\r\n    {\r\n        HashMap m = new HashMap();\r\n        Demo d = new Demo();\r\n         \r\n        \/\/ puts an entry into HashMap\r\n        m.put(d,&quot; Hi &quot;); \r\n         \r\n        System.out.println(m); \r\n        d = null;\r\n         \r\n        \/\/ garbage collector is called\r\n        System.gc();\r\n         \r\n        \/\/thread sleeps for 4 sec\r\n        Thread.sleep(4000); \r\n         \r\n        System.out.println(m);\r\n        }\r\n    }\r\n    class Demo\r\n    {\r\n        public String toString()\r\n        {\r\n            return &quot;demo&quot;;\r\n        }\r\n         \r\n        \/\/ finalize method\r\n        public void finalize()\r\n        {\r\n            System.out.println(&quot;Finalize method is called&quot;);\r\n        }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n{demo=Hi}\r\n{demo=Hi}\r\n<\/pre>\n<p><strong>How WeakHashMap Works<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/ Java program to illustrate \r\n\/\/ WeakHashmap \r\nimport java.util.*;\r\nclass WeakHashMapDemo\r\n{\r\n    public static void main(String args&#x5B;])throws Exception\r\n    {\r\n        WeakHashMap m = new WeakHashMap();\r\n        Demo d = new Demo();\r\n         \r\n        \/\/ puts an entry into WeakHashMap\r\n        m.put(d,&quot; Hi &quot;); \r\n        System.out.println(m);\r\n         \r\n        d = null;\r\n         \r\n        \/\/ garbage collector is called\r\n        System.gc(); \r\n         \r\n        \/\/ thread sleeps for 4 sec\r\n        Thread.sleep(4000); .\r\n         \r\n        System.out.println(m);\r\n    }\r\n}\r\n \r\nclass Demo\r\n{\r\n    public String toString()\r\n    {\r\n        return &quot;demo&quot;;\r\n    }\r\n     \r\n    \/\/ finalize method\r\n    public void finalize()\r\n    {\r\n        System.out.println(&quot;finalize method is called&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n{demo = Hi}\r\nfinalize method is called\r\n{ }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An obsolete reference is one that is kept around but will never be used, preventing the object it refers to from being eligible for garbage collection, thus causing a memory leak. Manually Setting to NULL Nulling out a reference to remove obsolete references to an object is good, but one must not overdo it. The&hellip; <a href=\"https:\/\/codethataint.com\/blog\/item-6-eliminate-obsolete-object-reference\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[245],"tags":[],"class_list":["post-2817","post","type-post","status-publish","format-standard","hentry","category-effective-java"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2817"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2817\/revisions"}],"predecessor-version":[{"id":2818,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2817\/revisions\/2818"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}