Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where is the Java stack allocated?

Tags:

java

I have noted the following from a website: The JVM HotSpot memory is split between 3 memory spaces:

  • The Java Heap
  • The PermGen (permanent generation) space
  • The Native Heap (C-Heap)

Where is the stack allocated in hotSpot JVM? In native heap?

update: another reference info: For a 64-bit VM, the C-Heap capacity = Physical server total RAM & virtual memory – Java Heap - PermGen

like image 859
jiafu Avatar asked Jun 14 '12 01:06

jiafu


People also ask

Where is stack allocated?

Stack Allocation: The allocation happens on contiguous blocks of memory. We call it a stack memory allocation because the allocation happens in the function call stack. The size of memory to be allocated is known to the compiler and whenever a function is called, its variables get memory allocated on the stack.

Where is heap and stack allocated?

Stack memory is allocated in a contiguous block whereas Heap memory is allocated in any random order. Stack doesn't require to de-allocate variables whereas in Heap de-allocation is needed.

Does Java allocate on heap?

Heap space is used for the dynamic memory allocation of Java objects and classes at runtime. New objects are always created in the heap space, and references to these objects are stored in the stack memory.

Where do you find stack in the memory?

As shown above, the stack segment is near the top of memory with high address. Every time a function is called, the machine allocates some stack memory for it. When a new local variables is declared, more stack memory is allocated for that function to store the variable.


1 Answers

The answer is:

  1. It is implementation dependent.

  2. In the implementation I looked at, the thread stack allocation was handled by the standard C native thread library, and it looked like the library was going to the OS to allocate a memory segment for the stack. So "none of the above".

  3. You can confirm this by delving into the OpenJDK source code relevant to your platform.

UPDATE

From an old question, here is the snippet of code from pthread_create that requests the allocation of the thread stack. This method used by the JVM thread implementation to create the native thread.

 mmap(0, attr.__stacksize, 
     PROT_READ|PROT_WRITE|PROT_EXEC, 
     MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)

As you can see, it just uses the mmap system call to request a memory segment from the operating system. As I said in a comment, this is NOT the regular Java heap, NOT the Permgen heap, and NOT the C native heap. It is a segment of memory specifically requested from the operating system.

For reference, here's a link to the mmap syscall manual entry.


update: another reference info: For a 64-bit VM, the C-Heap capacity = Physical server total RAM & virtual memory – Java Heap - PermGen

IMO, that is an oversimplification. (And please provide a link to where you found this information ... so that we can read it in its original form.)

like image 124
Stephen C Avatar answered Oct 14 '22 21:10

Stephen C