I have some doubt on Heap and stack memory in java..

  1. whether those are memory space or algorithm(procedure) of storing data at run time.

  2. Here is the class

    class HeapStack {
        String str = "test"; // heap memory
        int a = 10;// heap memory
        private void Test(){
            int a = 10;// stack memory
            String str = "test";// heap memory

am i correct?

closed as unclear what you're asking by m3th0dman, user53019, gnat, user22815, Dan Pichelman Oct 3 '14 at 19:46

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Nope, the pointer str in the method is in stack, while the String itself is in the String Pool, the location of which depends on the JVM. Same for the field - pointer in heap, string in string pool. But I have 2 questions to you: 1) Why did you think this is a good Programmers.SE question? 2) Why would you care at all? For most practical uses it doesn't matter whether it's stack or heap. – Ordous Oct 3 '14 at 12:52
  • All objects are will be in heap memory only... that i know very well.. y do u put down vote .. ? need some clarification – prakash Oct 3 '14 at 13:04

You are partially correct. Java works based on pointers, and all variables pointing to objects are pointers (officially called references). The memory for these variables are allocated on stack in case there are defined inside a method or on heap if they are fields. The value of data itself is as you defined it: all objects + primitives defined as fields, live on heap while primitives defined inside methods live on stack.

In Java memory allocation is pretty much the same for both stack and heap (as different from C which has a more complex allocation logic for heap): for stack is just push and for heap there is a mechanism called bump the pointer. The big difference is in memory reclaiming; for stack is jut pop while for heap there is the renowned garbage collector.

A more accurate commented class would be:

class HeapStack {

    String str /*heap*/ = "test"; // heap memory    
    int a /*heap*/ = 10;// heap memory

    private void Test() {    
        int a /*stack*/ = 10;// stack memory    
        String str /*stack*/ = "test";// heap memory    
  • 1
    It's more complex than that - the string is interned and both instances there are the same object. – user40980 Oct 3 '14 at 13:05
  • 2
    Java can allocate objects on the stack if the compiler can prove they don't escape the method where they were created, so it's not a sure thing that the instance fields are in the heap. – Doval Oct 3 '14 at 13:06
  • @MichaelT Yeah, but it's still in the heap (at least from Java 7). – m3th0dman Oct 3 '14 at 13:12
  • 1
    @Doval That's pretty cool; didn't knew about it. – m3th0dman Oct 3 '14 at 13:17

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