Re: Recursion Depth in Java
- From: Adam Warner <usenet@xxxxxxxxxxxxxxxxx>
- Date: Fri, 24 Feb 2006 13:39:18 +1300
On Thu, 23 Feb 2006 17:26:14 -0500, George Karabotsos wrote:
I have a question pertinent to how deep I can go with Java as compared
with C and whether there is any way to match or at least closely
approximate the C depth.
For illustration purposes I have written 2 small programs, which are
attached, one is java the other one is in C.
The environment I run these two programs is as follows:
> uname -a
Linux xxxxxxx 2.6.14-1.1644_FC4smp #1 SMP Sun Nov 27 03:39:31 EST 2005
i686 i686 i386 GNU/Linux
> limit
cputime unlimited
filesize unlimited
datasize unlimited
stacksize unlimited
coredumpsize 0 kbytes
memoryuse unlimited
vmemoryuse unlimited
descriptors 1024
memorylocked 32 kbytes
maxproc 16360
These days it is somewhat unusual for the main stack in Linux userspace
to be unlimited. Mine isn't:
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) unlimited
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
Here a program is restricted to 8MB of stack space. You are fortunate
your program ran with an "unlimited" size default stack. What is more
likely to be comparable is Java and a multithreaded C application. In a
JVM implementation with native threads running on Linux a Java thread will
have comparable resource limitations to a native POSIX thread.
Here's a Java program to test some startup tradeoffs:
public final class MaxThreads implements Runnable {
static int threads=0;
public void run() {
do {
Thread.yield();
} while(true);
}
public static void main(String[] args) {
try {
do {
new Thread(new MaxThreads()).start();
System.out.print("Scheduled thread number ");
System.out.println(++threads);
System.out.flush();
} while (true);
} catch (OutOfMemoryError e) {
System.out.println("Out of memory. Exiting.");
System.out.flush();
System.exit(1);
}
}
}
Some outcomes with a Sun JVM on Debian IA32:
$ java -Xms1000M -Xmx1000M -Xss2M MaxThreads
....
Scheduled thread number 952
Out of memory. Exiting.
$ java -Xms1000M -Xmx1000M -Xss8M MaxThreads
....
Scheduled thread number 233
Out of memory. Exiting.
$ java -Xms2000M -Xmx2000M -Xss8M MaxThreads
....
Scheduled thread number 108
Out of memory. Exiting.
The number of threads is limited by the address space available for
allocation to threads. Increasing the heap size decreases the address
space available for thread allocation and hence the number of threads that
can be created.
As you comment: "I tried using the -Xms, -Xmx, and -Xss flags but the
results did not changed by much. When tried the same program in SunOS
things improved but I had to allocate a huge stack to make it work."
That's the tradeoff: If you need at least one huge stack you will limit
the number of threads you can create. The point where you can't even
create one additional thread is similar to your single threaded C
situation.
Because address space is the primary issue you may be able to resolve this
by moving to a 64-bit platform and 64-bit JVM.
The alternatives are, if feasible, to rewrite your code to eliminate the
deep recursion or even to implement a language on top of Java with
dynamically heap allocated stacks. People often make do with broken
abstractions because doing the right thing is slower. Dynamic stacks
will require reallocation as they grow which imposes an extra level of
indirection upon access to stack items.
Regards,
Adam
.
- Follow-Ups:
- Re: Recursion Depth in Java
- From: George Karabotsos
- Re: Recursion Depth in Java
- Prev by Date: Re: Another generics foul-up
- Next by Date: Re: Recursion Depth in Java
- Previous by thread: Re: Recursion Depth in Java
- Next by thread: Re: Recursion Depth in Java
- Index(es):
Relevant Pages
|
|