JVM中堆的学习 —— Java default stack size

How much a stack can grow?

You can use a VM option named ss to adjust the maximum stack size. A VM option is usually passed using -X{option}. So you can use java -Xss 1M to set the maximum of stack size to 1M.

Each thread has at least one stack. Some Java Virtual Machines(JVM) put Java stack(Java method calls) and native stack(Native method calls in VM) into one stack, and perform stack unwinding using a Managed to Native Frame, known as M2NFrame. Some JVMs keep two stacks separately. The Xssset the size of the Java Stack in most cases.

For many JVMs, they put different default values for stack size on different platforms.

Can we limit this growth?

When a method call occurs, a new stack frame will be created on the stack of that thread. The stack will contain local variables, parameters, return address, etc. In java, you can never put an object on stack, only object reference can be stored on stack. Since array is also an object in java, arrays are also not stored on stack. So, if you reduce the amount of your local primitive variables, parameters by grouping them into objects, you can reduce the space on stack. Actually, the fact that we cannot put objects on java stack affects the performance some time(cache miss).

理解:当调用一个方法的时候,一个新的栈帧会在线程的栈中创建,它包含了本地变量,参数和返回地址等。java中,你永远不能将一个对象放在栈中,而只能将对象的引用放入栈中。数组对象在java中也不是存储在栈中。所以,如果你想减少栈的空间大小,你可以将基本变量,参数放进对象中。实际上,你不能将对象放入栈中的真正原因是它影响了运行时的性能(cache丢失)。

Does stack has some default minimum value or default maximum value?

As I said before, different VMs are different, and may change over versions. See here.

how does garbage collection work on stack?

Garbage collections in Java is a hot topic. Garbage collection aims to collect unreachable object in heap. So that need the definition of reachable. Everything on stack consists part of the root set references in GC. Everything that reachable from every stack of every thread should be considered as live. There is also some other root set references like Thread objects and some class objects.

This is only a very vague use of stack on GC. Currently most JVMs are using a generational GC. This article gives brief introduction about Java GC. And recently I read a very good article talking about the GC on .net. The GC on oracle jvm is quite similar so I think that might also help you.

理解:GC的目的是用来回收堆中不可达的对象(也就是没有被引用的对象)。

 

posted on 2015-12-28 15:36  gyt929458988  阅读(857)  评论(0)    收藏  举报