This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "possible causes for CPU spike" Watch "possible causes for CPU spike" New topic
Author

possible causes for CPU spike

Ishita Saha
Ranch Hand

Joined: May 30, 2007
Posts: 39
Hi,

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.

Thanks
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.

~Rajesh.B
Ishita Saha
Ranch Hand

Joined: May 30, 2007
Posts: 39
thanks,

I'll try thread dump
Ilja Preuss
author
Sheriff

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: possible causes for CPU spike
 
Similar Threads
Difference in opening binary stream in jsp & servlet
Is the answer correct?
Thread Eats Processor Power
% Used CPU
JTree Cell Renderer CPU usage