3

This might seem a lot of questions but they are all interrelated.I'm little confused as in where is the heap space allocated and where is the stack memory located ?
If both are present in main memory then why it is said that stack memory is easier to access and why can't we allocate objects in stack memory ?
Since classes are stored in PermGen where is this space allocated and how does it differ from heap space and where are constant strings stored ?

user401445
  • 1,014
  • 1
  • 16
  • 41

2 Answers2

4
  1. "Where are the heap and stack allocated?" The accepted answer to this question covers this. 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.
  2. "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 L1, L2, and L3 portions of the memory hierarchy, 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.
  3. "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 stack frame 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.
  4. The PermGen is another piece of the heap. Constant strings are stored here for the lifetime of the JVM. It is garbage collected just like the rest of the heap.
Community
  • 1
  • 1
Spike Gronim
  • 6,154
  • 22
  • 21
  • "The size of the stack is set when a thread is created." ... now suppose a thread is such that in which a function is calling itself recursively...how can memory be fixed in such a case ? – user401445 Nov 03 '11 at 19:54
  • The memory is fixed and your program will eventually crash when it exhausts the stack. Java is not friendly to deeply recursive functions. You can control the stack min and max sizes with -Xss argument to Sun JVMs. – Spike Gronim Nov 03 '11 at 20:26
  • one last question... suppose I have thread x and y ... will same size of stack memory be allocated to them or does it depend on some other factor like based on some pattern of local variables present but for that it will be tough to estimate...on the other hand if same memory is allocated...then it will cause a lot of wasted space. – user401445 Nov 03 '11 at 20:40
  • I don't think it's a big concern -- allocating substantial blocks of virtual memory is cheap, it doesn't need to be zero'd out or anything. It's normal for a program to allocate much more virtual memory than it uses. – Daniel Lubarov Nov 03 '11 at 20:57
  • It is a concern if you have many threads. They all get the same stack size. They actually allocate this memory, it is not just virtual memory. This will run you out of memory if you use large stacks and have many thousands of threads. – Spike Gronim Nov 04 '11 at 16:30
  • I disagree with the 4th point @Spike Gronim. PermGen is not a part of Heap in Java 6 atleast. http://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html – Zeena Mar 10 '15 at 08:01
2

If both are present in main memory then why it is said that stack memory is easier to access

There's speed of access and speed of allocation. Stack allocation (as in alloca) is fast because there's no need to search for an unused block of memory. But Java doesn't allow stack allocation, unless you count the allocation of new stack frames.

Accessing stack memory is fast because it tends to be cached. Not only are locals near one another, they are also stored very compactly.

and why can't we allocate objects in stack memory ?

This would be useful, but dangerous. A user could allocate an object on the stack, create references to it from permanent objects, and then try to access the object after the associated stack frame is gone.

It's safe to store primitives on the stack because we can't create references to them from elsewhere.

Since classes are stored in PermGen where is this space allocated and how does it differ from heap space and where are constant strings stored ?

PermGen is just another heap space. String literals are stored in the literal pool, which is just a table in memory which is allocated when a class is loaded.

Daniel Lubarov
  • 7,796
  • 1
  • 37
  • 56