Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li>"Where are the heap and stack allocated?" The <a href="https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap">accepted answer to this question covers this.</a> Each thread gets its own stack and they all share one heap. The operating system controls the exact memory locations of the stacks and heap items and it varies.</li> <li>"Why is stack memory easier to access" Each thread has its own stack, so there are fewer concurrency issues. The stack and heap are both eligible for caching in the <a href="http://en.wikipedia.org/wiki/Memory_hierarchy" rel="nofollow noreferrer">L1, L2, and L3 portions of the memory hierarchy</a>, so I disagree with Daniel's answer here. Really I would not say that one kind of memory is particularly easier to access than the other.</li> <li>"Why can't we allocated objects in stack memory?" This is a design decision taken by the JVM. In other languages like C/C++ you can allocate objects on the stack. Once you return from the function that allocated that <a href="http://en.wikipedia.org/wiki/Call_stack" rel="nofollow noreferrer">stack frame</a> such objects are lost. A common source of errors in C/C++ programs is sharing a pointer to such a stack allocated object. I bet that's why the JVM designers made this choice, though I am not sure.</li> <li>The PermGen is another piece of the heap. Constant strings are stored here for the lifetime of the JVM. It <a href="https://stackoverflow.com/questions/3796427/in-java-is-permanent-generation-space-garbage-collected">is garbage collected just like the rest of the heap.</a></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload