{"id":2815,"date":"2018-04-10T12:17:33","date_gmt":"2018-04-10T12:17:33","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2815"},"modified":"2018-04-10T12:17:33","modified_gmt":"2018-04-10T12:17:33","slug":"memory-leaks-in-java","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/memory-leaks-in-java\/","title":{"rendered":"Memory Leaks in Java"},"content":{"rendered":"<p><strong>When Memory Leaks happen in Java<\/strong><\/p>\n<p><em>Objects inaccessible by running code but still stored in memory<\/em><\/p>\n<p>Class allocates a large chunk of memory (e.g. new byte[1000000]), stores a strong reference to it in a static field, and then stores a reference to itself in a ThreadLocal. Allocating the extra memory is optional (leaking the Class instance is enough), but it will make the leak work that much faster.<\/p>\n<p><strong>Static field holding object reference<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass MemorableClass \r\n{\r\n    static final ArrayList list = new ArrayList(100);\r\n}\r\n<\/pre>\n<p><strong>Calling String.intern() on lengthy String<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nString str=readString(); \/\/ read lengthy string any source db,textbox\/jsp etc..\r\n\/\/ This will place the string in memory pool from which you can't remove\r\nstr.intern();\r\n<\/pre>\n<p><strong>Unclosed open streams ( file , network etc&#8230; )<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ntry {\r\n    BufferedReader br = new BufferedReader(new FileReader(inputFile));\r\n    ...\r\n    ...\r\n} catch (Exception e) {\r\n    e.printStacktrace();\r\n}\r\n<\/pre>\n<p><strong>Unclosed connections<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ntry {\r\n    Connection conn = ConnectionFactory.getConnection();\r\n    ...\r\n    ...\r\n} catch (Exception e) {\r\n    e.printStacktrace();\r\n}\r\n<\/pre>\n<p>The thread clears all references to the custom class or the ClassLoader it was loaded from.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When Memory Leaks happen in Java Objects inaccessible by running code but still stored in memory Class allocates a large chunk of memory (e.g. new byte[1000000]), stores a strong reference to it in a static field, and then stores a reference to itself in a ThreadLocal. Allocating the extra memory is optional (leaking the Class&hellip; <a href=\"https:\/\/codethataint.com\/blog\/memory-leaks-in-java\/\">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":[193],"tags":[],"class_list":["post-2815","post","type-post","status-publish","format-standard","hentry","category-interview-questions-java"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2815","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=2815"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2815\/revisions"}],"predecessor-version":[{"id":2816,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2815\/revisions\/2816"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}