17

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

11
  • 3
    Wherever the implementation allocates it... Jun 14, 2012 at 1:08
  • Keep in mind that Java code can be running under a JIT compiler, which also might change that. Jun 14, 2012 at 1:11
  • The stack can be allocated to any of the three memory spaces, depending on the implementation of the JVM (i.e. Oracle and Apple develop different implementations of Java).
    – FThompson
    Jun 14, 2012 at 1:22
  • What happens for hotSpot jvm? Many thanks to you
    – jiafu
    Jun 14, 2012 at 1:26
  • In C 32-bity or 64-bit, the heap is limited by the virtual memory the OS allows. The physical memory is not important, thus the name "virtual" ;) Jun 14, 2012 at 7:26

2 Answers 2

25

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.)

17
  • if So. Can I think java thread will be allocated in native heap? Due to both JVM heap and PermGen space has be used for special use.
    – jiafu
    Jun 14, 2012 at 1:29
  • 1
    You can also think nothing of it, it's an implementation detail. Jun 14, 2012 at 1:52
  • I am interesting on it. nothing of it. where it is?
    – jiafu
    Jun 14, 2012 at 1:55
  • 3
    @jiafu - you can think whatever you like. However, I don't have any evidence of what you are saying. Like I said in my answer, in the case I looked at the thread stack is NOT allocated in regular heap, NOT allocated in permgen, and NOT allocated in the C native heap.
    – Stephen C
    Jun 14, 2012 at 2:58
  • @ta.speot.is, it's not impl. detail - although you can speculate that it can be, anything but the hardware stack is just stupid.
    – bestsss
    Jun 16, 2012 at 19:21
-1

If you can find access to those things in any which way, the second Sun (or Oracle it is now adays?) will put out a patch quickly.

Being able to access those sorts of things is a HUGE security risk. A lot of systems for big companies used Java. To leave a hole where it is possible to track down allocation space wouldnt be possible.

as above, MMAP will allocate space and return it for the program and space requirements needed. I do a lot of stuff with MMAP almost every day.

Maybe because i think a bit darker in how things can be used (to better protect my systems), pretty sure there would be a hot fix for it to cancel out your work, or you have Team America World Police knocking at your door suspecting you of foul acts, and in the process seize all your equipment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.