Re: How to limit and change CPU and memory of a thread?
On Mon, 29 Oct 2007 09:20:14 -0700, Ulrich Scholz wrote:
Dear all,
is it possible to create a thread with limited memory and limited
clock cycles? I guess this is highly dependent on the operating
system and the virtual machine.
For example, it seems to be possible to pose a memory limit if a Java
program is started from the console under Linux (options -Xmx and -
Xss). What other possibilities are there?
Thank you,
Ulrich
You can limit the heap size when you start the JVM, but not memory used by
an individual thread. Memory isn't really owned by a thread. Once
allocated any thread can access it. As far as CPU goes.. there really
isn't any way to limit it. You could have a second thread that watches
how much CPU has been used and does some action if it gets past a certain
limit, but I don't think you are looking for this.
What you probably want is for a thread to use no more than 10% of the CPU
over some time interval. I don't think there is a way to do this in Java.
I've been looking at some of these same issues myself and Java just
doesn't have a lot of capability when it comes to severely limiting a
process.
I did figure out a way to limit a Java process to only one thread. You
create a custom class loader for it and check for the class
"java.lang.Thread". If it tries to instantiate one then throw an
exception. I haven't actually tried it, but I think it would work.
You are probably writing the code you are trying to limit, so this doesn't
really apply to you.
Good luck.
--
Kenneth P. Turvey <kt-usenet@squeakydolphin.com>