Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

memory - How does Java (JVM) allocate stack for each thread

A Java application starts up with one heap for all threads. Each thread has its own stack.

When a Java application is started, we use the JVM option -Xms and -Xmx to control the size of heap and -Xss to control the stack size.

My understanding is that the heap being created becomes a "managed" memory of JVM and all the object being created are placed there.

But how does the stack creation work? Does Java create a stack for each thread when it is created? If so, where exactly the stack is on the memory? It is certainly not in the "managed" heap.

Does JVM create stack from native memory or does it pre-allocate a section of managed memory area for stack? If so, how does JVM know how may threads will be created?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a few things about thread stacks that the Java specification tells us. Among other things:

  • Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread.

  • Because the Java Virtual Machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java Virtual Machine stack does not need to be contiguous.

  • Specification permits Java Virtual Machine stacks either to be of a fixed size or to dynamically expand and contract as required by the computation.

Now, if we focus on JVM implementations such as HotSpot, we can get some more information. Here are a few facts I've collected from different sources:

  • The minimum stack size in HotSpot for a thread seems to be fixed. This is what the aforementioned -Xss option is for. (Source)

In Java SE 6, the default on Sparc is 512k in the 32-bit VM, and 1024k in the 64-bit VM. ... You can reduce your stack size by running with the -Xss option. ... 64k is the least amount of stack space allowed per thread.

  • JRockit allocates memory separate from the heap where stacks are located. (Source)

Note that the JVM uses more memory than just the heap. For example Java methods, thread stacks and native handles are allocated in memory separate from the heap, as well as JVM internal data structures.

  • There is a direct mapping between a Java Thread and a native OS Thread in HotSpot. (Source).

  • But the Java thread stack in HotSpot is software managed, it is not an OS native thread stack. (Source)

It uses a separate software stack to pass Java arguments, while the native C stack is used by the VM itself. A number of JVM internal variables, such as the program counter or the stack pointer for a Java thread, are stored in C variables, which are not guaranteed to be always kept in the hardware registers. Management of these software interpreter structures consumes a considerable share of total execution time.

  • JVM also utilizes the same Java thread stack for the native methods and JVM runtime calls (e.g. class loading). (Source).

  • Interestingly, even allocated objects may be sometimes located on stack instead on heap as a performance optimization. (Source)

JVMs can use a technique called escape analysis, by which they can tell that certain objects remain confined to a single thread for their entire lifetime, and that lifetime is bounded by the lifetime of a given stack frame. Such objects can be safely allocated on the stack instead of the heap.

And because an image is worth a thousand words, here is one from James Bloom Java memory


Now answering some of your questions:

How does JVM knows how may threads will be created?

It doesn't. Can be easily proved by contradiction by creating a variable number of threads. It does make some assumptions about the maximum number of threads and stack size of each thread. That's why you may run out of memory (not meaning heap memory!) if you allocate too many threads.

Does Java create a stack for each thread when it is created?

As mentioned earlier, each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread. (Source).

If so, where exactly the stack is on the memory? It is certainly not in the "managed" heap.

As stated above, Java specification allows stack memory to be stored on heap, technically speaking. But at least JRockit JVM uses a different part of memory.

Does JVM create stack from native memory or does it pre-allocate a section of managed memory area for stack?

The stack is JVM managed because the Java specification prescribes how it must behave: A Java Virtual Machine stack stores frames (§2.6). A Java Virtual Machine stack is analogous to the stack of a conventional language. One exception are Native Method stacks used for native methods. More about this again in the specification.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...