Home > Back-end >  Value of data change inside the heap memory?
Value of data change inside the heap memory?

Time:07-16

So, I am trying to figure out how the value inside data changes accordingly. I have written comments to what i have understood. can anyone please explain how values stored in heap and stack memory for this problem.

public class CBV
{
    static int data = 10;
    public static void main(String []args)
    {
        // data is a local variable
        //int data = 10;
        System.out.println("Before data: " data);
        changeData(data);
        System.out.println("After data: " data);
    }
    public static void changeData(int data)
    {
        // data is changed and its scope is within this method
        // so the original value is not changed
        // In java, garbage collection is automatically handled
        data = data   500;
        // so  here data is garbage collected
    }
}

CodePudding user response:

Only objects go on the heap. And (aside from autoboxing) Objects are created using the new operator.

So in your example, the class object for CBV (including its static data field) goes on the heap (but will never be garbage collected since it isn't dynamically loaded). In changeData, data is on the stack

CodePudding user response:

You need to understand the difference between stack and heap. Or better yet, you need to understand Memory Model of Java and a little bit on Garbage Collection (resourced added in the end).

There is a lot to explain on this subject and this thread could never be enough. But to your particular question, data exists in 2 contexts here.

  • As class data (a static variable in this case) of CBV - this gets stored in heap (see the explanation below). Since this is a static property it will remain in memory as long as the class itself is loaded. On the other hand, if this were a non-static field, it would exist separately with each class instance. It's corresponding memory for each copy would only be reclaimed when the corresponding object itself gets garbage collected.
  • As a method argument of the method changeData - this gets stored in stack and goes out of scope as soon as method execution is over (see explanation below)

From this answer on stackoverflow

  • If a variable is intended to go out of scope shortly after its creation - say, at the end of the method in which it's created, or even earlier, then it's appropriate for that variable to be created on the stack. Local variables and method arguments fit this criterion; if they are primitives, the actual value will be on the stack, and if they are objects, a reference to the object (but not the object itself) will be on the stack.

  • If you have a variable which is intended to outlive the execution of the method that created it, it needs to be on the heap. This applies both to any objects that you create, and any primitives that are stored within those objects.

Garbage Collection only triggers for objects created dynamically i.e. using new operator (they are always stored in heap).

These explanations might be little confusing if you are new, therefore I advice you to go through these below mentioned literature first.

Some other resources

  1. Understanding Java Memory Model - It talks about Object Allocation, Heap and Garbage Collection
  2. Structure of Java Virtual Machine
  • Related