views:

702

answers:

9

Greets-

We gots a few nutters in work who enjoy using

while(true) { //Code } 

in their code. As you can imagine this maxes out the CPU. Does anyone know ways to reduce the CPU utilization so that other people can use the server as well.

The code itself is just constantly polling the internet for updates on sites. Therefore I'd imagine a little sleep method would greatly reduce the the CPU usage.

Also all manipulation is being done in String objects (Java) anyone know how much StringBuilders would reduce the over head by?

Thanks for any pointers

+2  A: 

Make it wait some time before firing again, like this:

while(true) { 
  //Code 
  Thread.sleep (1000); //Wait 1 second
} 

As for the second question, it would reduce memory and possibly CPU usage as well, but the gains really depend on what's happening with those strings.

Nick Craver
+3  A: 

How often do those sites update? You're probably really annoying the hosts. Just stick a Thread.sleep(60 * 1000); at the end of the loop and you can avoid this. That'll poll them once a minute—surely that's enough?

Samir Talwar
+2  A: 

A sleep would reduce the CPU usage. As for the StringBuilders they could lower the memory usage and improve performance.

Darin Dimitrov
A: 

You could also try

Thread.yield()
Helper Method
@Helper Method - "Yield" only causes the currently executing thread object to temporarily pause and allow other threads to execute... so this will only reduce CPU usage significantly if there are any other user applications running in parallel. "Sleep" will be more adequate to what he wants to achieve.
XpiritO
@XpritO Sry, you're right. Didn't quite get the point of the OP. Maybe I shouldn't read post before my 2nd coffee ^^.
Helper Method
+3  A: 

I'll start off with your second question, I would like to agree with the rest that StringBuilder vs String is very much dependent on the particular string manipulations. I had "benchmarked" this once and generally speaking as the amount of new string allocations went up (usually in the form of concatenations) the overall execution time went up. I won't go into the details and just say that StringBuilder turned out to be most efficient overtime, when compared to String, StringBuffer, String.format(), MessageFormat...

My rule of thumb is that whenever I wish to concatenate more than 3 string together I always use StringBuilder.

As for your first question. We had a requirement to bring CPU usage to 5%. Not an easy task. We used Spring's AOP mechanism to add a Thread.sleep() to before any method execution of a CPU intensive method. The Thread.sleep() would get invoked only if some limit had been exceeded. I am sorry to say that the computation of this limit is not that simple. And even sorrier to say that I still have not obtained the permission to post it up on the net. So this is just in order to put you on an interesting but complicated track that has proven to work over time.

Yaneeve
What possible reasoning could lead to a requirement of "no more than 5% CPU time"? If you have a CPU-intensive task, then you have a CPU-intensive task. There's no way around that, and adding sleeps to your code simply makes that task take longer. If you're concerned about a particular program's impact on overall system resources, then it's far more intelligent to simply reduce the priority of that program. Or buy another computer.
kdgregory
Personally, I couldn't agree with you more! Yet I am but a mere programmer and did not "engineer" this solution. My only point was that this type of thing COULD be done, if need be :)
Yaneeve
+3  A: 

A lot of the folk wisdom about StringBuilders is incorrect. For example, changing this:

String s = s1 + ":" + s2 + ":" + s3;

will probably not be improved by explicitly using a StringBuilder. This is because the Java compiler actually translates the concatenation sequence into an equivalent sequence of appends to a temporary StringBuilder. Unless you are concatenating Strings in a loop, you are better of just using the String concatenation operator. Your code will be easier to read.

The other point that should be made is that you should use a profiler to identify the places in your code that would benefit from work to improve performance. Most developers' intuition about what is worth optimizing is not that reliable.

Stephen C
A: 

If their code is in a separate JVM and is running on Linux or some other Unix, require them to run their program at nice 20.

Or you could have them run inside a virtual machine like VirtualBox and use it to limit the processor utilization.

Or you could fire them if they continue burn cycles like that instead of using some event-driven model or Thread.sleep.

Rex Kerr
A: 

Never assume something you see in the code is bad (or good) for performance. Performance issues are notoriously deceptive.

Here's an easy way to see for sure what is taking the time.

Mike Dunlavey
A: 
  1. What sites is the code polling? Do they support an RSS push?
  2. If they do, then there is no need to poll the sites, and instead plug a module to wait for updates. The current method then waits using wait();
  3. The new module then notifies the object with this method using notifyAll().
  4. Admittedly this is some extra work, but it saves a whole lot of bandwidth and computation.
Varun Garde