2

As per what I understand as a Java programmer:

Each thread is given its stack (for Java default stack size per thread is 1M). Each method called from a thread is given a stack-frame (just a contigous part of thread stack memory), thread stack has nothing more than stack-frames. My guess is that a process also given some stack (so that every thread of that process gets a part of its stack).

Does process has its own stack, or some stack that it further gives (parts of that stack) to threads ?

P.S. The question arose as I was thinking of escape analysis: I learned that if an object (reference) never escapes a method - it is on stack (not heap), if a reference does escape a single method, but is used only within one Thread (= never escapes its run() method) - it is also on stack.

This implies that a process has no limit on its "stack" size, but there is a limit how many threads I can create (fork/clone) from one process (what is this number typically?) and minimum thread stack size exists. So I guess stack for process itself is not allocated and kind of "unlimited" (100% depends on thread stack allocation).

Related: "stack frame" = "call stack"

user10777718
  • 723
  • 4
  • 16
  • What is your question ? For the stack point of view the process is just the first thread, the next threads do not get their stack into the process stack ( supposing this is what _so that every thread of that process gets a part of its stack_ means ) – bruno Dec 23 '18 at 15:31
  • @bruno I corrected my question: **Does process has its own stack, or some stack that it further gives (parts of that stack) to threads ?** – user10777718 Dec 23 '18 at 15:40
  • 1
    Again no, each thread has its own stack, this is not because you create a new thead that the stack of the process (being the first thread of the group) is reduced. This is why when you create a new thread the global size of the process grows – bruno Dec 23 '18 at 15:49
  • 1
    A variable which references an object might live on the stack, but the object itself is on the heap. Java isn't C, where you can return a pointer to a stack based (or, automatic storage duration) variable. – vgru Dec 23 '18 at 15:55
  • Process has its own stack and each thread has its own stack created while thread is spawned. Process stack is not given off of its part for thread. Thread stack is fixed size for each thread defined by kernel when it is compiled. – anand Dec 24 '18 at 03:52

0 Answers0