This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Performance and the fly likes possible causes for CPU spike Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "possible causes for CPU spike" Watch "possible causes for CPU spike" New topic

possible causes for CPU spike

Ishita Saha
Ranch Hand

Joined: May 30, 2007
Posts: 39

I've a piece of code which is reading data from sockets and all that stuff, it also uses infinite loops for the purpose of reading data. While running the code under load - i see a cpu usage of 100%.

request you to please mention some common possible causes of CPU spiking in java code.

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
100% CPU load is not necessarily wrong. In a mathematical computation program, for instance, you positively want the CPU to spend all its spare time doing your big computation.

People running things like Web servers generally do use average CPU load as one measure of how stressed the server is. A Web server generally would not be expected to run at 100% CPU load for long periods.

In your application, you are reading data from sockets. Usually, such a program is "I/O-bound". That is, it should use little CPU, because most of its time is spent in a blocked state, waiting for data from the socket.

You say you have infinite loops. Infinite loops will cause 100% CPU utilisation in a single-CPU single-core system, if there are no blocking operations in the loop. You haven't told us much about your loops, so I can't say if they are expected to use 100% CPU.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
rajesh bala
Ranch Hand

Joined: Jan 14, 2003
Posts: 66
Take a thread dump and look at the threads. It might give a clear picture on where the CPU is spending most of the time. Please note that this is at very very high level and based on its output, you may want to profile the application.

If you are using windows, try ctrl+page-break button to get the threaddump.
If you are using linux, try kill -QUIT <java_pid> to get the threaddump in the console.

All the best.

Ishita Saha
Ranch Hand

Joined: May 30, 2007
Posts: 39

I'll try thread dump
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
As Peter indicated, it might help if you showed us your code for those loops.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: possible causes for CPU spike
jQuery in Action, 3rd edition