{"id":38072,"date":"2016-07-29T18:55:59","date_gmt":"2016-07-29T13:25:59","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=38072"},"modified":"2016-07-30T06:18:23","modified_gmt":"2016-07-30T00:48:23","slug":"how-a-newer-investigated-a-java-out-of-memory-error","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-a-newer-investigated-a-java-out-of-memory-error\/","title":{"rendered":"How a Newer Investigated a Java Out Of Memory Error."},"content":{"rendered":"<p>So one fine day newer came to office and got an interesting problem from his team lead.<br \/>\nOne particular API was giving Java Out Of memory Error with only 10 concurrent threads.<\/p>\n<p>So the Newer started to investigate the problem<\/p>\n<p>so at first he reaches to the server and try to collect stats about the particular JVM Process that was giving error . He Needed to get id of this process. So he found this<strong> java\/bin\/jps<\/strong> Utility of java that gives the <strong>process ids<\/strong> of all the java processes running on that system. So he got the id of that process.<\/p>\n<p>Next he ran command<strong> java\/bin\/jstat<\/strong> with GC parameter to get the garbage collector stats of that java process. But oh snap there was nothing. That was odd.<\/p>\n<p>So Newer tried to google it and found that if a process is running from a different user and if <strong>java\/bin\/jstat<\/strong> is being used with different user that there may be problem in getting result.<\/p>\n<p>He used the <strong>top<\/strong> command to get the user of running process. It was <strong>tomcat 7<\/strong> . So that was handy .<\/p>\n<p>Now he just needed to login into system with that user. He tried to <strong>sudo su tomcat7<\/strong>.<\/p>\n<p>But nothing happened.<\/p>\n<p>What could it be &#8230;<\/p>\n<p>He opened <strong>\/etc\/passwd<\/strong> file and he saw there is <strong>no bash for tomcat7<\/strong> user. So now he cannot login as user tomcat7.<\/p>\n<p>Now he decided to replicate the issue on the local system so he can get the issue investigated.<\/p>\n<p>He could get the code but now the thing he was needed to start was <strong>jvm settings<\/strong> on the server so he could use those<strong> jvm settings<\/strong> to run process locally.<\/p>\n<p>He found this <strong>ps<\/strong> utility of linux. He used it as <strong>ps -ef | grep tomcat7<\/strong>.<strong> E<\/strong> and<strong> f<\/strong> flags are used to show the full format and environment.<\/p>\n<p>He found out that Max memory that as allowed to process was 128mb and everything all about that process.<\/p>\n<p>Now he has <strong>jvm setting<\/strong>s, code and everything which was needed to test this code locally.<\/p>\n<p>He installed this tool <strong>visualVM<\/strong> tool that gives all this information about java process with pretty visualization.<\/p>\n<p>He executed the code on local machine and started monitoring with visualVM. He went to monitor tab. VisualVM was showing him all the <strong>threads, heap, cpu and classes<\/strong>. There was an option for <strong>heap dump<\/strong>. He dumped the heap snapshot and started analyzing with <strong>Heap Analyzer.<br \/>\n<\/strong><br \/>\nHe looked for the <strong>most dominating objects<\/strong> which were taking most space on heap. There were lot&#8217;s of objects of various classes . He checked with the help of <strong>regex<\/strong> if there was any mentions of his project classes. There were his classes but objects of these classes and retain able heap was negligible. Most dominating objects were from native java packages and some apis.<br \/>\nSo there was nothing he could do. He reported that this was issue of less memory on which the process was running so to solve that problem they were needed to give more memory to process.<\/p>\n<p>So this is how a simple Java Memory Out Of Error was investigated.<\/p>\n<p>Now we have these tools in our Tool box<\/p>\n<p><strong>1. Jps.(for process id)<\/strong><br \/>\n<strong> 2. jstat (statistics about jvm process)<\/strong><br \/>\n<strong> 3. jmap(to take a heap dump)<\/strong><br \/>\n<strong> 4. jhat(to visualize the heap dump)<\/strong><\/p>\n<p>some tools like<strong> visualVm , jconsole<\/strong> and <strong>jvmmonitor<\/strong> provides all of this in a single app. But these are all gui based . So when you don&#8217;t have gui on any system than above mentioned command are the savior.<\/p>\n<p>There is also option of<strong> profiling in visualVm<\/strong> , Where you see the <strong>time taken<\/strong> by every method and <strong>invocation count<\/strong> of every method. So next time when you see that something is taking time then you can really know what is taking time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So one fine day newer came to office and got an interesting problem from his team lead. One particular API was giving Java Out Of memory Error with only 10 concurrent threads. So the Newer started to investigate the problem so at first he reaches to the server and try to collect stats about the [&hellip;]<\/p>\n","protected":false},"author":937,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1],"tags":[4844,3839,3838,3836,3837],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38072"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/937"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=38072"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38072\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=38072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=38072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=38072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}