2

I am doing a java program which has two versions : one with a GUI and one without.

I am happy with the GUI one because it uses 100% of the CPU all the time but the one without (which is supposed to be faster) only uses 5-25% of the CPU. Both versions are exactly the same in terms of computations. The same problem happens when I run from Eclipse and from the shell.

I thought about allocating more memory with -Xms and -Xmx but it won't change the CPU usage.

I also tried to set the priority to nice my process to -15 in terminal but there was no noticeable difference.

How can I ensure, in Eclipse and in terminal that my code has 100% of the CPU.

asked Aug 27, 2013 at 8:59
1
  • Make your application multithreaded. Commented Aug 27, 2013 at 9:05

2 Answers 2

3

I see 2 possibilities

  1. Are you sure it is CPU bound? Drop an infinite loop in there and see if it spikes.

  2. Is your PC quad core? Possibly 100% is 25 % for your PC and the GUI, being multithreaded uses the CPU better, perhaps.

  3. Since you say this is non GUI version, do you go overboard with println, i.e non buffered printing?

Parallelism if possible will fix #2, and perhaps with some design changes even #1

answered Aug 27, 2013 at 9:01

5 Comments

Yes it spikes with an infinite loop and I only have two cores so it's not a problem of multithreading. I understand it even less. How is it that my code wants to use only 25% before it's finished.
@DeviousCoder then perhaps it is not CPU bound, are you sure your code is written such that it should be? Perhaps there is too much Disk reading or syncronized printing to the screen
Yeah it could be a synchronization problem but then how is it that I don't see that problem when the GUI is there ?
@DeviousCoder do you still do a lot of println in the GUI version of the program?
It was 4 println done during the execution that were slowing everything down. Good call Karthik ;)
1

100% CPU utilization is a problem, not a goal.

Usually, although I am sure there are exceptions, one would see a program beeing stuck at 100% CPU usage as an indication of an error.

The reason for this is that whenever your process uses peripherial components the program will wait for the component to return, and while the process is waiting it will pass control to other processes.. In this context peripherals include things like RAM.

So a single treaded program staying at 100% CPU usage means that it's cycling through instructions but never touching a very limited set of resources. In particular your program would not be touching RAM, in java this means that your program makes no function calls. - So probably your program is stuck.

Multi threaded programs is much more complicated since the program can pass control to other parts of the same program.

Ultimately the question you should be considering is; does the GUI program finish tasks faster than the non-GUI program? If it does then there are reasons to look at why that is, and most likely those reasons boil down to parallel processing*. If the GUI uses more time than the non-GUI then the low CPU usage is an added perk, when comparing the GUI program to the non-GUI program.

*Edited to add: programming for concurrent processing is a huge complication. Make sure the potential benefit is worth it before going down this path.

answered Aug 27, 2013 at 9:14

3 Comments

Thanks for your clarification. It is true except if you are doing mathematical simulations on servers and if you want them to finish the fastest possible ;)
Even with heavy mathematical simuations, unless you have written your program with a very heavy emphasis on how memory is managed, you would not expect 100% CPU usage. - This level of control over memory usage is simply not availiable in java, it's something you give up for type safety and garbage collection.
@Taemyr From all that he has said, it does look like he is writing an exception, and he does need to max his CPU usage, especially in light of the cause of the issue.. ==100 or ~= 100 is a moot point

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.