Friday, May 14, 2010

Java CPU Usage Profiling

Recently I was working on a Java application that was pegging my CPU. Since this application was multi-threaded, I started looking for simple ways to determine which part of the application was the culprit. Since JConsole and VisualVM come bundled with Java 6 SDK I started looking at what they had to offer.

First we'll look at JConsole. Out of the box, JConsole provides a Threads tab that allows you to view what threads are running and generate a stack trace for a thread. It's essentially thread dumps with a GUI. It's not so useful for determining which threads are most active.

After a little more digging and I found a JConsole plugin called topthreads which is an enhanced version of the JTop plugin that ships with the JDK. For an overview of running topthreads, go here. You'll need to download the jar mentioned toward the bottom of the article and then fire up JConsole with the plugin:
jconsole -pluginpath /path/to/topthreads.jar 


The top threads plugin gives a much better view of the threads that are consuming the most CPU time:


Once you find a suspicious thread, you can switch back to the Threads tab in JConsole and get a stack trace of the thread. To really see what the thread is doing you'll likely need to take multiple snapshots of the thread and see what progress, if any, the thread is making.

Topthreads may help you find a rogue thread in a multithread application but it is not much help if you have a single threaded application that is performing poorly. Or perhaps you have a multithread application where several similar threads are taking up a large amount of cpu.

In my own debugging, I found that it wasn't the threads I cared about so much as the methods. Ultimately, you want to know which methods are taking up the most processing time. Enter VisualVM.

VisualVM contains a pretty impressive profiling tool that's easy to use. To get started with profiling with VisualVM, go here.

The profiler in VisualVM gives a nice view of top methods which can be sorted by time and invocations:



One downside to VisualVM's profiling is that it tanks the CPU when you first start profiling. The application your profiling will be limping for about 10 seconds while VisualVM digs in its profiling. It also adds a small bit of drag after profiling begins. By contrast, topthreads didn't add any noticable drag or hit on startup. Despite the performance penalty, I found VisualVM much better for profiling application performance.

7 comments:

  1. W.r.t. TopThreads: you don't have to switch back to the Threads tab to get a stack of the thread; simply click on the thread and the stack trace will appear in the lower half of the window...

    ReplyDelete
  2. I was diagnosed with stage 3 breast cancer in August 2010. A valuable friend told me about Dr. Itua Herbal Center in West Africa. She gave me her phone number and email address. I quickly contacted him to guarantee that his herbal medicines will heal my cancer and I will heal forever I said OK.I ask him what is the healing process, he asks me to pay the fees I did and within 7 working days he sent me the herbal medicine and then he asked me I told my friend Gomez about the herbal drug so that he gave me to go and drink it.So after drinking for two weeks, I was cured, I am so grateful and I promise that I will do it I recommend to anyone who has cancer and that that I am doing. Herbal medicine Dr. Itua makes me believe that there is hope for people with Parkinson's disease, schizophrenia, scoliosis, bladder cancer, colorectal cancer, breast cancer, kidney cancer. , Leukemia, lung cancer, skin cancer, uterine cancer, prostate cancer Fibromyalgia,
    Fibrodysplasia Syndrome, Epilepsy,Sclerosis sickness, Dupuytren's Disease, Diabetes, Celiac Disease, Angiopathy, Ataxia, Arthritis, Amyotrophic Lateral Sclerosis, Alzheimer's Disease, Lupus, Adrenocortic Carcinoma.Asthma, Allergic Diseases.HIV Help, Bladder cancer,Brain cancer,Esophageal cancer,Gallbladder cancer,Gestational trophoblastic disease,Head and neck cancer,Hodgkin lymphoma
    Intestinal cancer,Liver cancer,Melanoma,Mesothelioma,Multiple myeloma,Neuroendocrine tumors
    Non-Hodgkin lymphoma,Cervical Cancer,Oral cancer,Ovarian cancer,Sinus cancer,Soft tissue sarcoma,Spinal cancer,Stomach cancer
    ,Testicular cancer,Throat cancer,Meniere's disease,Thyroid Cancer,Vaginal cancer,Vulvar cancer
    HIV Aids, Herpes, Disease Chronic inflammatory, Memory disorder,
     Here is his contact information ...... [Email ... drituaherbalcenter@gmail.com. Whatsapp ... + 2348149277967]

    ReplyDelete
  3. Thanks. Now I also want to say something: This section will cover the process of understanding the mood of a songwriter by analyzing their lyrics. It will also cover the various techniques that can be used to analyze the mood of a songwriter.
    Lyrics and Ringtones collection
    ghungroo toot gaye lyrics
    chahunga main tujhe hardam lyrics

    ReplyDelete
  4. Thanks for sharing such amazing post. I was lookin for the exact information. I would like to share the same information to my online community. I am a Digital Marketer and I have a big community of people interested in Learning Wordpress, Learning Blogging, Learnign Search Engine Optimazation (SEO) etc. I'll share your post to them. This way your blog will get more exposure. By the way, thanks for sharing.
    digital marketing seekhen
    website kaise banaye
    seo settings to a new publishing blog
    expired domains kya hote hain fayade aur nuksan
    blog kya hota hai blogging kaise karte hain

    ReplyDelete
  5. Thanks. You have share the same for which I was on the internet from the last one hour. We appreciate your work. Keep it up. Thanks. If any of your reader is interested in Angel Number, Angel Number Meaning and symbolis, they can chek it here.
    4040 angel number meaning
    4455 angel number meaning
    seeing 1253 angel number
    .

    ReplyDelete