Wednesday, February 22, 2012

Figure out why is JAVA eating CPU?

In a production environment profiling is not an option, we have seen several times that our CPU has reached almost 100%, we are running tomcat on Redhat 5.5, so what is happening?

Fortunately, java comes with some great debugging tools, cooperating those tools with Linux built-in tools will let you know what is happening.

Here is what i am going to explain:
  • An introduction about java threads and its relation to what-so-called Linux LWP
  • A step-by-step
1- An introduction

as you may know, any java program starts when the JVM calls the main method, this creates a thread called the main thread and any thread you create using java code will be derived from the main thread (out-of-focus: it is always a good practice to give your threads a name, it is very useful in debugging, however, it is not required here) the same exact behavior occurs on the Linux level, the main thread for java means a process for the OS, and every thread you create using java the OS will create a Light-weight-process or LWP.
to cut it short, Java main thread = Linux process and Java thread = Linux LWP.

the idea here:
  • Ask Linux which LWP is eating the CPU.
  • Ask Java for a Thread Dump.
  • Map this LWP to a Java thread.
  • Get the part of code causing the issue.

2- A Step-By-Step:
  • Get the PID: the very first step is to know what is the Java process ID, we will use Linux tools ps and grep
    ps -A|grep java
    if you are running multiple java processes we can execute
    ps -ef|grep java

  • the next step is to get CPU usage per each LWP related to the main process, again we will use  ps  and  grep
    ps -eLo pid,lwp,nlwp,ruser,pcpu,stime,etime,args|grep {pid} > lwpthread.txt
    the file lwpthread.txt will contain some thing similar:
    PID NLWP SZ RUSER %CPU STIME ELAPSED COMMAND
    8234 8234 1110 admin 0.3 08:11 30:15 /usr/java/jdk1.6.0_24/bin/java
    8234 8245 1110  admin 99.0 08:45 10:15 /usr/java/jdk1.6.0_24/bin/java 

    as you can see, we have an LWP(mapped to a java thread) eating the CPU, get the NLWP, that will be our lead in the next step.

  • the next step is to generate a java thread dump, there are two main ways to create a thread dump, the first one is to use the JDK tool jstack and pass the PID to it, the second way is to send a kill signal to the JVM, however in the first way you will have control over where you want to save the thread dump, while on the second way you will have the thread dump written on the java process standard output stream.
     as said, we are using tomcat, so the thread dump will be in catalina.out
    kill -3 {pid}
    the thread dump will be printed to the file with full stack trace. the file will contain the thread you are after, but first convert your LWP id from DEC to HEX so 8245 would be 2035, now open the thread dump with text editor and search for 2035, you will find something similar:
    "TP-Processor234786" daemon prio=10 tid=0x00002aaad8024800 nid=0x2035 runnable [0x00002aaadef29000]
    java.lang.Thread.State: RUNNABLE
    at java.util.HashMap.get(HashMap.java:303)
    at ......

and you are done!

60 comments:

javin paul said...

This is fantastic tip man, Indeed useful. Production environment is always hard for debugging with logs level upto WARN and ERROR and limited access. Please share more of your experience. bookmarked it. I have also written about Java remote debugging in Eclipse you may like.

Javin

Anonymous said...

Another way to do it is using jconsole if you had started the JVM with the following
-Dcom.sun.management.jmxremote.port=9003

you can install jconsole plugin called topthreads

and then start jconsole with the following from any machine and connect to the JVM machine on the above port

jconsole -pluginpath topthreads.jar

Moutaz Salem said...

Thanks!!!
i will be posting more findings as i go :)

Moutaz Salem said...

Hi Sumit,

as i mentioned, profiling is not an option here :)

the same applies on Jconsole and VisualVM

Mamdouh said...

That was definitely a life saver :)

Moutaz Salem said...

thanks Mamdouh :)

Boudy said...

Man we are having the same problem at our live environment .. And we're trying to figure out a way to do this for almost 2 weeks now .. Thanks a lot, keep em comin ; D

Moutaz Salem said...

Most welcomed Boudy ;)

Rajkumar Selvaraj said...

awesome article.. thanks

Moutaz Salem said...

My pleasure, Rajkumar

Hazem said...

Excellent work moutaz :)

Looking forward to read more articles by you

You are an artist!!

Moutaz Salem said...

thanks, Hazem

i learned from a Master :)

Hazem said...

How humble you are!!!

My dear friend you are a real distinguished master, we learned a lot from you, really we did, and we still as you see :)

keep doing well, and always share :)

Stas Ostapenko said...

Hi. Thnaks for the post :) Btw, instead of "ps -Aef | grep java" you can use jps tool.

Moutaz Salem said...

of course, Stas
thanks for the tip :)

Anonymous said...

Very useful indeed, thanks very much.

Moutaz Salem said...

Most Welcomed, yorichunt

Mayur Choubey said...

This is just incredible... thanks a ton...

Moutaz Salem said...

you are more than welcome, Mayur

de said...

Great Article

Online Java Training
Online Java Training India
Java Training Institutes in Chennai
Java Training in Chennai

Java Training




Best Recommended books for Spring framework
Java Interview Questions

fatih tekin said...

command should have been as below without grep otherwise it only gives your own grep command process id

ps -eLo pid,lwp,nlwp,ruser,pcpu,stime,etime,args

Moutaz Salem said...

@fatih tekin: that is not entirely true, we are grepping on a certain PID, so the PID of the grep will be ignored

Unknown said...

I find this post on identifying high CPU usage issue was really useful. And the way you explained it was really nice and clear.

Thanks a lot.!!

IT said...

Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Java Spring Hibernate Training Institutes in Chennai | Hibernate Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai

Unknown said...
This comment has been removed by the author.
Dinesh said...

When using visual VM CPU is at 100%..however the system is only using 40% of CPU overall as seen from top..using Websphere..is there a place any where which can restrict CPU in a JVM?

Rahul said...

Thankx for sharing may be useful but my java eating cpu 200+ issue not resolved so please sharing any other option for resolving

Government Jobs said...

Thanks for sharing this unique information with us. Your post is really awesome. Your blog is really helpful for me..
Top college in Jaipur
law college
law college in Jaipur
Best law college in Jaipur

service care said...

This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me..
samsung mobile service center near me
samsung service centres in chennai
samsung mobile service center in velachery
samsung mobile service center in porur
samsung mobile service center in vadapalani

digitalsourabh said...

PHP Training in Bhopal
Graphic designing training in bhopal
Python Training in Bhopal
Android Training in Bhopal
Machine Learning Training in Bhopal
Digital Marketing Training in Bhopal
https://99designs.com/blog/trends/top-10-web-design-trends-for-2014/

Techxinx said...

Thanks for this post, I really appriciate. I have read posts,
all are in working condition. and I really like your writing style.
autocad in bhopal
3ds max classes in bhopal
CPCT Coaching in Bhopal
java coaching in bhopal
Autocad classes in bhopal
Catia coaching in bhopal

verdure skills india pvt ltd said...

nice blog
get best placement at VSIPL

digital marketing services
Web development Services
seo network point

vijay said...

Awesome..I read this post so nice and very imformative information...thanks for sharing
aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore

hami said...


I am obliged to you for sharing this piece of statistics
here and updating us together with your inventive steerage.
sd movies point

Durai Moorthy said...

This is very good quality article and interesting..& This post has a nice one. share more updates.
aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore

Shopclues Winner List said...

Shopclues winner list 2020 here came up with a list of offers where you can win special shopclues prize by just playing a game & win prizes.
Shopclues lucky draw 2020
Shopclues winner name 2020
Shopclues prize list 2020

Rajesh Anbu said...

Your article is very informative. Thanks for sharing the valuable information.
aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore

Anonymous said...

Informative post, i love reading such posts. Read my posts here
Unknownsource
http://unsurpassedesports.esportsify.com/profile/globalemployees
http://unsurpassedesports.esportsify.com/forums/scrims-ps4/277/hire-laravel-developers

Unknown said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot.

python certification Online Training in bangalore

python certification courses in bangalore

python certification classes in bangalore

python certification Online Training institute in bangalore

python certification course syllabus

best python certification Online Training

python certification Online Training centers

Unknown said...

Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.

ORACLE APPS FINANCE FUNCTIONAL training in bangalore

ORACLE APPS FINANCE FUNCTIONAL training institutes in bangalore

ORACLE APPS FINANCE FUNCTIONAL courses in bangalore

ORACLE APPS FINANCE FUNCTIONAL course in bangalore

Oracle apps finance functional training institutes in bangalore

ORACLE APPS FINANCE FUNCTIONAL classes in bangalore

ORACLE APPS FINANCE FUNCTIONAL course fees in bangalore

best ORACLE APPS FINANCE FUNCTIONAL training institutes in bangalore

ORACLE APPS FINANCE FUNCTIONAL training centers in bangalore

ORACLE APPS FINANCE FUNCTIONAL certification in bangalore

Oracle apps finance functional courses in bangalore

ORACLE APPS FINANCE FUNCTIONAL certification courses in bangalore

ORACLE APPS FINANCE FUNCTIONAL course fees

Oracle apps finance functional training in bangalore

Oracle apps finance functional training in bangalore

best Oracle apps finance functional training institutes in bangalore

Oracle apps finance functional training in bangalore

ORACLE APPS FINANCE FUNCTIONAL training

ORACLE APPS FINANCE FUNCTIONAL training in btm layout

ORACLE APPS FINANCE FUNCTIONAL course in marathahalli

Unknown said...

Thank you for excellent article.You made an article that is interesting.

SAP Online Training

SAP Classes Online

SAP Training Online

Online SAP Course

SAP Course Online

Unknown said...

I have recently visited your blog profile. I am totally impressed by your blogging skills and knowledge.

SAP ABAP Online Training

SAP ABAP Classes Online

SAP ABAP Training Online

Online SAP ABAP Course

SAP ABAP Course Online

James Williams said...

Great Post. Thanks for sharing your idea.
Java Online Training
Python Online Training
PHP Online Training

vidyasthali law college said...



such an amazing college for law students to build career amazing study material with high profile facilities and teachers.
best law college

About College

Best Law College in jaipur

Information about college

Best Law College in jaipur
www.vidyasthalilawcollege.com

saketh321 said...


I have a mission that I’m just now working on, and I have been at the look out for such information ExcelR Data Science Course In Pune

Josh said...

Male fertility doctor in chennai
Std clinic in chennai
Erectile dysfunction treatment in chennai
Premature ejaculation treatment in chennai

Best Training Institute said...

Thankyou for sharing this kind of valuable information with us. Your post give great knowledge
Servicenow training in bangalore

IVR Call Center Solutions said...

I really want to appreciate the way to write this
omni-channel
ivrs
ip-pbx

Persephone Summers said...

buy juicy fruit online
buy gelato strain online
Buy dark star strain online
buy hawaiian skunk strain online
buy bc big bud strain leafly
buy auto flowering seeds online
buy brass knuckles vape recall 2018
buy alaskan thunder fuck online
buy cannabis seeds bank online

Jobi said...

Thank you very much for this great post.
Baron Zemo Coat

salome said...

useful article.thanks for sharing Angular training in Chennai

Unknown said...

what is contrave

silicon wives

sky pharmacy

atx 101 uk

macrolane buttock injections london

hydrogel buttock injections

buying vyvanse online legit

buy dermal fillers online usa

mesotherapy injections near me

xeomin reviews

3RI Technologies said...

Good information posted for all of us, keep writing such blogs.

Data Science Training in Pune

andrewjackson said...

Our the purpose is to share the reviews about the latest Jackets,Coats and Vests also share the related Movies,Gaming, Casual,Faux Leather and Leather materials available Boston Starter Jacket

mirovia media said...

The best quality hair extensions are available to clients directly. If only you could feel how SILKY this hair is and will continue to be for over a year. This hair is worth the investment and will give you stress-free beautiful hair. The hand-tied weft is ideal for thin hair because it is extremely flat.

NJC said...

https://www.facebook.com/Jack-russell-puppies-looking-for-a-lovely-home-103472108733880/

mrbobystone said...

Hi , Thank you so much for writing such an informational blog. If you are Searching for latest Jackets, Coats and Vests, for more info click on given link-Resident Evil 4 Jacket

eddielydon said...

Great information about wilderness for beginners giving the opportunity for new people. connor jessup locke & key puffer jacket

Jobi Johnson said...

I really enjoyed reading your articles. It looks like you’ve spent a lot of time and effort on your blog. daryl dixon vest

Valluva said...

inno m7