<p>I am calling a vendor's Java API, and on some servers it appears that the JVM goes into a low priority polling loop after logging into the API (CPU at 100% usage).  The same app on other servers does not exhibit this behavior. This happens on WebSphere and Tomcat.  The environment is tricky to set up so it is difficult to try to do something like profiling within Eclipse.</p>

<p>Is there a way to profile (or some other method of inspecting) an existing Java app running in Tomcat to find out what methods are being executed while it's in this spinwait kind of state?  The app is only executing one method when it gets in this state (vendor's method). Vendor can't replicate the behavior (of course).</p>

<hr>

<p>Update:  </p>

<p>Using JConsole I was able to determine who was running and what they were doing.  It took me a few hours to then figure out why it was doing it.  The problem ended up being that the vendor's API jar that was being used did not match exactly to the the database configuration that it was using.  It was defaulting to having tracing and performance monitoring enabled on the servers that had the slight mis-match in configuration.  I used a different jar and all is well.</p>

<p>So thanks, Joshua, for your answer. JConsole was extremely easy to setup and use to monitor an existing application. </p>

<p>@Cringe - I did some experimenting with some of the options you suggested.  I had some problems with getting JProfiler set up, it looks good (but pricey).  Going forward I went ahead and added the Eclipse Profiler plugin and I'll be looking over the different open source profilers to compare functionality.</p>

[{'answer_id': 12961, 'author': 'Joshua McKinnon', 'author_id': 235, 'author_profile': 'https://Stackoverflow.com/users/235', 'pm_score': 5, 'selected': True, 'text': '<p>If you are using Java 5 or later, you can connect to your application using <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html" rel="noreferrer">jconsole</a> to view all running threads. jstack also will do a stack dump. I think this should still work even inside a container like Tomcat.</p>\n\n<p>Both of these tools are included with JDK5 and later (I assume the process needs to be at least Java 5, though I could be wrong)</p>\n\n<p>Update:\nIt\'s also worth noting that starting with JDK 1.6 update 7 there is now a bundled profiler called <a href="http://java.sun.com/javase/6/docs/technotes/guides/visualvm/" rel="noreferrer">VisualVM</a> which can be launched with \'jvisualvm\'. It looks like it is a <a href="http://visualvm.java.net/" rel="noreferrer">java.net project</a>, so additional info may be available at that page. I haven\'t used this yet but it looks useful for more serious analysis.</p>\n\n<p>Hope that helps</p>\n'}, {'answer_id': 13016, 'author': 'cringe', 'author_id': 834, 'author_profile': 'https://Stackoverflow.com/users/834', 'pm_score': 2, 'selected': False, 'text': '<p>If it\'s for professional purpose and you have some money to spend, try to get your hands on <a href="http://www.ej-technologies.com/products/jprofiler/overview.html" rel="nofollow noreferrer">JProfiler</a>. If you just want to get some insights, try out the <a href="http://eclipsecolorer.sourceforge.net/index_profiler.html" rel="nofollow noreferrer">Eclipse Profiler Plugin</a>. I used it several times, but I don\'t know the current state.</p>\n\n<p>A new(?) project from the eclipse project itself is available too: <a href="http://www.eclipse.org/tptp/" rel="nofollow noreferrer">http://www.eclipse.org/tptp/</a> (See <a href="http://www.eclipse.org/articles/Article-TPTP-Profiling-Tool/tptpProfilingArticle.html" rel="nofollow noreferrer">this article</a>). Never used it, so I can\'t tell if it is worth the effort.</p>\n\n<p>There\'s also a very good list of open source profilers available at <a href="http://www.manageability.org/blog/stuff/open-source-profilers-for-java" rel="nofollow noreferrer">http://www.manageability.org/blog/stuff/open-source-profilers-for-java</a></p>\n'}, {'answer_id': 13271, 'author': 'Ran Biron', 'author_id': 931, 'author_profile': 'https://Stackoverflow.com/users/931', 'pm_score': 3, 'selected': False, 'text': '<p>Facing the same problem I used <a href="http://www.yourkit.com/overview/index.jsp" rel="noreferrer">YourKit profiler</a>. It\'s loader doesn\'t activate unless you actually connect to it (though it does open a port to listen for connections). The profiler itself has a nice "get amount of time spent in each method" while working in it\'s less obtrusive mode.</p>\n\n<p>Another way is to detect CPU load (via JNI, so you\'d need an external library for this) in a "watchdog" thread with highest priority and start logging all threads when the CPU is high enough for a long enough time. You might find <a href="http://www.javaworld.com/javaworld/javaqa/2002-11/01-qa-1108-cpu.html" rel="noreferrer">this article</a> enlightining.</p>\n'}, {'answer_id': 16733, 'author': 'Boris Terzic', 'author_id': 1996, 'author_profile': 'https://Stackoverflow.com/users/1996', 'pm_score': 0, 'selected': False, 'text': '<p>For completeness sake: even though my company more or less standardizes on Eclipse we use Netbeans (6 and up) with its <a href="http://profiler.netbeans.org/" rel="nofollow noreferrer">included, free profiler</a> on a daily basis. It works better than the Eclipse TPTP plugin (last checked 3 months ago) and for us it removes any need for a commercial profiler such as JProfiler, which is excellent, but fast becoming unnecessary.</p>\n'}, {'answer_id': 19660, 'author': 'svrist', 'author_id': 86, 'author_profile': 'https://Stackoverflow.com/users/86', 'pm_score': 0, 'selected': False, 'text': '<p>VisualVM should be the profiler from netbeans as standalone. I tried the TPTP for eclipse but visualVm seems as a much nicer option!</p>\n'}, {'answer_id': 19767, 'author': 'Marcus Downing', 'author_id': 1000, 'author_profile': 'https://Stackoverflow.com/users/1000', 'pm_score': 1, 'selected': False, 'text': "<p><strong>Use a profiler.</strong> Yes they cost money, and using them can occasionally be a bit awkward, but they do provide you with a great deal more real evidence rather than guesswork.</p>\n\n<p>Human beings are universally bad at guessing where performance bottlenecks are. It just seems to be something our brains aren't build to do very well. It may seem obvious, you may have great ideas about what the problem is, but the real world often turns out to be doing something different. And optimising the wrong part of code means, at best, lots of work for minimal benefit. More often it makes things slower, and sometimes it breaks things entirely. So before you make any changes for the sake of optimisation, you should <em>always</em> have real evidence from a profiler or other accurate tool.</p>\n\n<p>As mentioned, both JProfiler and YourKit are both fairly good and not prohibitively expensive. Last time I looked, they both had free demos too.</p>\n"}, {'answer_id': 24082, 'author': 'Marcel', 'author_id': 2554, 'author_profile': 'https://Stackoverflow.com/users/2554', 'pm_score': 2, 'selected': False, 'text': "<p>If JConsole can't be used you can</p>\n\n<ul>\n<li>press <kbd>CTRL</kbd>+<kbd>BREAK</kbd> under Windows</li>\n<li>send <code>kill -3 &lt;process id&gt;</code> under Linux</li>\n</ul>\n\n<p>to get a full Thread Dump. This doesn't affect performance and can always be run in production.</p>\n"}, {'answer_id': 150636, 'author': 'Kire Haglin', 'author_id': 2049208, 'author_profile': 'https://Stackoverflow.com/users/2049208', 'pm_score': 2, 'selected': False, 'text': '<p><strong>JRockit Mission Control Latency Analyzer.</strong></p>\n\n<p>The Latency Analyzer that comes with JRockit shows you what the JVM is "doing" when it\'s not doing anything. In the latest version you can see latencies for:</p>\n\n<ul>\n<li>Java wait/blocked/sleep/parked.</li>\n<li>File I/O</li>\n<li>Network I/O</li>\n<li>Memory allocation</li>\n<li>GC pauses</li>\n<li>JVM latencies, e.g code generation and class loading </li>\n<li>Thread suspension </li>\n</ul>\n\n<p>The tool will give you the stack trace when the latency occurred. You can view the latency data in many different ways (aggregated traces, as a histogram, in a thread graph etc.). The tool also allows you to see transitions between threads, for instance when one thread notifies another.</p>\n\n<p><a href="http://blogs.oracle.com/hirt/WindowsLiveWriter/The.0LatencyAnalyserMigratedfromtheoldBE_7246/latency_graph_2.png" rel="nofollow noreferrer">latency analyzer http://blogs.oracle.com/hirt/WindowsLiveWriter/The.0LatencyAnalyserMigratedfromtheoldBE_7246/latency_graph_2.png</a></p>\n\n<p>The overhead is negligible and unlike many other tools it can be used in a production environment. \nThis <a href="https://blogs.oracle.com/hirt/entry/the_mission_control_30_latency" rel="nofollow noreferrer">blog post</a> gives you a brief introduction and the program can be downloaded <a href="http://www.oracle.com/technology/products/jrockit/missioncontrol/" rel="nofollow noreferrer">here</a>.</p>\n\n<p>It\'s free to use for development!</p>\n'}]
['https://Stackoverflow.com/questions/12927', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/791/']