This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to reduce Java application's CPU utilization  RSS feed

 
Jon Hayden
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I wrote a Java application for creating search indexes. When run, this application continuously utilizes almost 100% of CPU until it ends. This application performs lots of IO operations. I need to reduce its CPU usage to about 50-60% sacrificing speed (i.e., it is fine if it takes an extra 10-15 minutes to finish).

So far, I tried changing different Java HotSpot VM options but no success.

"ps -p PID -o pid,nlwp" shows me that the applications NLWP is 13.

Could any one offer advice regarding reducing CPU usage?

Thanks for any help,
Jon
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

If you're looking for something to apply from the outside, then the "renice" command can do that.

From inside of the running applications, you might periodically call Thread.sleep to slow it down.
 
Jon Hayden
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer,

Thank you so much for your quick reply. I tried both solutions - 'renice' and calling Thread.sleep(). Using Thread.sleep() gives consistent results compared to 'renice'. I'm much obliged to you ..

Thanks again,
Jon
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to reduce the CPU utilisation? That sounds like a spurious requirement. What harm does 100% CPU utilisation do?

I guess that your real requirement is to avoid starving other applications of CPU. That's what process and thread priorities are for.

You can set the priority of your whole Java application process to be lower than other applications; that's what the "nice" or "renice" commands do for your on Unix (or Mac OS X). Alternatively, and I think nicer, you can start a reduced-priority thread within your application, to do the CPU-intensive work. Look at the API for java.lang.Thread, noting that you must set the thread priority before starting the thread.

Either way will mean that other applications always get priority use of the CPU. But, when they don't need it, your application takes up all the slack. This is much better than Thread.sleep(), where there will be periods when no application is using the CPU, which is just a waste of potential processing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!