cpu usage?
I have been running a java program on my vps and i noticed its always using 100% cpu. Is this normal? I know when I run this java program on my personal windows computer it doesn't take any cpu. My java program is just a basic program that waits for incoming connections and packets for a networked game, even when just sitting there waiting it takes 100% cpu.
*also separate question, top is the best way to check cpu right, ps -ef just shows an average?
14 Replies
Is it better to not block and to just check every certain amoutn of ms?
@disastorm:
*also separate question, top is the best way to check cpu right, ps -ef just shows an average?
Not being familiar with java, I can't help with your problem, but top is the best if you need to keep a real-time check on your CPU and memory usage. If you need a "snapshot" of the current usage, you can use "ps aux" (no quotes). That will spit out a list of all running processes, and their CPU and memory. That's good for if you need to post it here (make sure to post it as code using the code button above where you type your post).
@disastorm:
yes there is a ServerSocket blocking on accept and also a DatagramSocket blocking on receive. I also have a bufferedreader reading incoming line from System.in.
Is it better to not block and to just check every certain amoutn of ms?
Blocking or not depends on the architecture of your application, but I meant are you using e/poll or are you having a loop that constantly checks for data, AFTER the accept?
The new thread will continue to be in a loop that waits for incoming packets, however, the 100% cpu usage is even when there is no one connected or connecting.
Look into setting up JMX debugging and run an environment that let's you remotely debug like Eclipse.
But regardless of this issue, check out the Java.nio polling for better performance. The idea is to check for new data and sit and wait at the kernel level until there's something to read, not in the application loop.
@disastorm:
Is this normal, does nohup make the cpu go crazy for some reason?
No, nohup doesn't make the CPU go crazy. But nohup does redirect standard input to /dev/null and standard output to 'nohup.out'.
@disastorm:
I also have a bufferedreader reading incoming line from System.in.
Now, I'm not a java programmer, but I'm betting that the redirection of standard input to /dev/null is causing your program to go into a tight polling loop around the bufferedreader (read 0 bytes from System.in, do nothing, select/poll all descriptors including System.in).
This is done in a manor similar to the OOM killer in Linux by targeting the biggest processes in memory.
That would always be your Java process.
How to fix this?
Either make sure you never go above by forcefully setting your max heap low enough ( -Xmx256m )
Keep in mind that out of heap space is added on top of your heap, so don't set it for 512mb
You definitely should use "ps aux" it has an interface of list of current running processes, memory used and CPU Consumption. Pretty much all you need to track CPA usage and processes mapping.
I am using the same for a long now.
@vpsgeek:
VZ let's you burst to memory that doesn't belong to your VPS and at any time in the future this memory can be reclaimed.
This is done in a manor similar to the OOM killer in Linux by targeting the biggest processes in memory.
That would always be your Java process.
How to fix this?
One could also avoid VZ by using a provider such as Linode.
@hoopycat:
@vpsgeek:VZ let's you burst to memory that doesn't belong to your VPS and at any time in the future this memory can be reclaimed.
This is done in a manor similar to the OOM killer in Linux by targeting the biggest processes in memory.
That would always be your Java process.
How to fix this?
One could also avoid VZ by using a provider such as Linode.
Sure thing, Linode is always a very good Choice.
@vpsgeek:
VZ let's you burst to memory that doesn't belong to your VPS and at any time in the future this memory can be reclaimed.
This is done in a manor similar to the OOM killer in Linux by targeting the biggest processes in memory.
personality development That would always be your Java process.
Inspirational Quotes
As for burst ram in VZ, I don't know.
I've been at providers like Servint where you could burst all the time without getting processes reclaimed.
I think it's reclaimed when other VPS instances need the memory and there is no more physical memory.