DEV Community

ivinieon
ivinieon

Posted on • Updated on

Relationship between Variables, Objects, and Memory

Relationship between Variables, Objects, and Memory

  • What is an Application
    → A set of commands that a computer can execute to provide functions for general users.

  • What is a Memory?
    → The place where the executed application resides. Memory allocated to the application is internally divided into several areas.
    An identifier is a “name given to entity” in a program whereas, a variable is a “name given to memory location”.

  • What is a CPU?
    → The hardware component of a computer performs most of the processing tasks, executing instructions or commands that are stored in memory based on the instructions it receives from software.

## Stack Memory
Local variables and parameters of functions or methods are stored. A stack frame is stacked each time a function or method is called.

public class Main {
    public static void main(String[] args) {
            int a = 100;
            a = wow(a);
    }
    public static int wow(int num) {
            int b = num * 4;
            return b;
    }
}

Enter fullscreen mode Exit fullscreen mode

When the main(String[] args) method is called, a stack frame for main() is created in the stack memory, and each parameter (args) and the value (100) for each local variable are stored, and a name (a) is attached.

When wow(a) is called, a stack frame for wow() is created again in the stack memory, and the parameter int num is stored in it, and since num = a, num becomes 100, b becomes 400, and that value is returned. Since the wow method call is over, the wow stack frame will be deleted, and the returned 400 will become the value of a.
In addition, if this stack memory is full, the application will terminate.

## Heap Memory
Objects are stored.

public class Main{
    public static void main(String[] args) {
            Counter c = new Counter();
            tow(c);
            int count = c.get();
    }
    public static void two(Counter C) {
            c.increment();
            c.increment();
    }
}
public class Counter {
    private int state = 0;
    public void increment() { state++;}
    public int get() {return state;}
}
Enter fullscreen mode Exit fullscreen mode
  • args is a parameter.
  • c is a local variable.
  • state is an instance variable.

When the main() method is called, a stack frame for main() is created in the stack, and the args parameter and variable c are stored. c is a variable of Counter type, and when this class is created, i.e., when an object is created, it will be stored in the heap memory. Of course, a stack frame for the Counter() method is also created in the stack, and a variable called "this" is created inside it, and the value of this stores the address of the data stored in the heap memory. After the object creation is complete, the Counter stack frame in the stack disappears.

Then, the two(c) method is called, and a stack frame for two() is created in the stack memory, and c as a parameter is stored in it. This method calls the increment() method of c, and another stack frame is created for this, and state becomes 1. Then, another increment is called, and a stack frame is created for it, and state becomes 2, and then it disappears.

The variable count creates a stack frame for calling c's get() method, so it retrieves the state value (2). Therefore, the count value in the main method's stack frame becomes 2.

## Garbage Object

public class Main {
    public static void main(String[] args) {
            Counter c = make();
    }

    public static Counter make() {
            Counter c = new Counter();//Object creation
            return new Counter(); //Creating a new object again
    }
}

    public class Counter {
        private int state = 0;
        public void increment() {state++;}
        public int get() {return state;}
}

Enter fullscreen mode Exit fullscreen mode

The make() method creates two objects. Since an object has been created, the two objects are stored in the heap memory. However, the object that variable c points to is the second object created by the return. Therefore, there is no way to access the first object created. Thus, it wastes the heap memory. Depending on the language, there is a Garbage Collector that cleans up and removes these objects. This is usually referred to as GC.

This posting is just a study note which was written after watching youtube videos in Korean.

Youtube Link

Top comments (0)