setting stack size to thread seems to make no difference in android
I using following thread api to set stack size , say i am 开发者_StackOverflow社区assigning 1 or 64 or some small value so that i can simulate a stackoverflow exception but the value set does not seem to make any difference.
Thread(ThreadGroup group, Runnable target, String name, long stackSize)
The javadoc documentation says that you can't expect any certain behavior so you have to experiment.
I haven't ever tested with small numbers, but creating a thread in Android with
new Thread(getMainLooper().getThread().getThreadGroup(), myRunnableThatNeedsLotOfStack, "My Thread", 65536)
caused that runnable that would get a StackOverflowError when run in the main thread to go through beautifully, so I guess you can set the stack size of a thread to 64K in Android.
The stack size for the main thread is very small and can't be changed. The size varies by the version of Android; see What is the android UI thread stack size limit and how to overcome it? . New threads that you create can be given more stack as Karl says in his answer.
This is well documented in the JavaDoc for Thread. Quoting:
The details of the relationship between the value of the stackSize parameter and the maximum recursion depth and concurrency level are platform-dependent. On some platforms, the value of the stackSize parameter may have no effect whatsoever.
Android is open source, so questions like this can be quite easily answered without the guesswork some others are suggesting. Simply put, the stack size is truncated to lie between a minimum (1536 bytes) and maximum (257Kbytes), so using values outside of these ranges will make no difference. Also, the suggestion that the main thread size is fixed is not entirely true; it is controlled by the -Xss command line option to the VM, which I believe can be set using adb shell setprop dalvik.vm.extra-opts
.
精彩评论