Pregunta

This is a very peculiar question. I'm wondering the best way to create a lot of lag when running this Java program. No matter the speed of the computer, I want this Java program to cause annoying processor lag until the program is stopped. How am I guaranteed to achieve this? What are the most efficient methods?

So far the only thing I've been moderately successful with is

while (true) {}

But even that can sometimes be unreliable if the user's computer is fast.

I am using this to perform some scientific experiments on how much a group of computers can handle.

¿Fue útil?

Solución

I'm not sure I do understand exactly what you want, but it is improbable that you will get it using a sungle thread and an endless empty loop. First, that program will use at most one system thread, and therfore, a single CPU core. Second, the hotspot compiler is likely to optimize away your empty loop (or any arithmetic operation or assignement to a variable that will never be used). So what ever you want to accomplish, you need to do something that the optimizer won't attemp to improve. So...

If you want to slow down the execution of a program (rather than the computer), then spread around some Thread.sleep(1000). The JVM will yield that time back to the OS.

If your intention is to have the JVM do some work while waiting for some external event (as in "computing bogomips of the system"), then you should run code that will have side effect, so that the optimzer won't remove useless variable allocations. You may, for example, do some arithmetic operations on an array, then System.out.print() (or whatever else...) any value from that array. Note however that your process will still be limited to a single thread, and that if your process performs no IO operation for a long period, then it will most likely be depriorized by the operating system, so dont expect too much reliability from that type of measurement technique.

If you're looking at stalling the whole system, then you must run several threads, and have them do either intensive memory allocations, IO operations, or external program execution. You may for example run thousands of threads, each od them running a loop that execute an external Java program that allocate huge buffers, then write the content of that buffer to a file... Well, you get the idea ;)

Otros consejos

You could also just sleep the current thread. This doesn't create lag, but it'll give the program that effect.

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

More information

You might appreciate the utility I posted recently addressing Techniques for causing consistent GC Churn. I use this utility to simulate load on a system during benchmarks, which sounds quite similar to what you're looking to do.

It creates and destroys a large number of objects for a set period of time, or continuously in the background, and can be configured to put varying amounts of strain on the JVM. Try running several at once to be extra mean :)

The full code is available here: GC Churn Gist.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top