Beginner : Memory Management¶
Introduction to stack¶
Idli Maker Examples¶
Stack¶
Success
There are a lot of quizzes in this session, please take some time to think about the solution on your own before reading further.....
Introduction to call stack¶
Example 1¶
Consider the below code:
int add(int x, int y) {
return x + y;
}
int product(int x, int y) {
return x * y;
}
int subtract(int x, int y) {
return x - y;
}
public static void main() {
int x = 10;
int y = 20;
int temp1 = add(x, y);
int temp2 = product(x, y);
int temp3 = subtract(x, y);
System.out.println(temp1 + temp2 + temp3);
}
Following is the call stack execution for above code:
Ouput: 220
Example 2¶
Consider the below code:
int add(int x, int y) {
return x + y;
}
public static void main() {
int x = 10;
int y = 20;
int temp1 = add(x, y);
int temp2 = add(temp1, 30);
int temp3 = add(temp2, 40);
System.out.println(temp3);
}
Output:
100
Example 3¶
Consider the below code:
int add(int x, int y) {
return x + y;
}
static int fun(int a, int b) {
int sum = add(a, b);
int ans = sum * 10;
return ans;
}
static void extra(int w){
System.out.println("Hello");
System.out.println(w);
}
public static void main() {
int x = 10;
int y = 20;
int z = fun(x, y);
System.out.println(z);
extra(z);
}
Following is the call stack execution for above code:
Output:
300
Hello
310
Types of Memory in Java¶
Following are the types of memory present in Java -
- Stack -
All the primitive data type and reference will be stored in stack. - Heap -
Container of that reference is stored in heap. Arrays, ArrayList, Objects are created inside heap.
Example 1
Consider the below code:
public static void main() {
int x = 10;
int[] ar = new int[3];
System.out.println(ar); // #ad1
System.out.println(ar[2]); // 0
ar[1] = 7;
}
Note:
- Primitive data types: [int, float, double, char, boolean, long] memory will be assigned in stack.
- Reference/ address of the container: will be stored in stack.
- Container: [Array/ Arraylist] will be stored in heap.
Example 2
Consider the below code:
public static void main() {
int x = 10;
int[] ar = new int[3];
int[] ar2 = ar;
System.out.println(ar); // 4k
System.out.println(ar2); // 4k
}
Now, lets analyze the given code -
Example 3
Consider the below code:
public static void main() {
int[] ar = new int[3];
System.out.println(ar); // 5k
ar[1] = 9;
ar[2] = 5;
ar = new int[5];
System.out.println(ar); // 7k
}
Now, lets analyze the given code -
Example 4
Consider the below code:
static void fun(int[] a){
System.out.println(a); // 9k
a[1] = 5;
}
public static void main() {
int[] ar = new int[3];
System.out.println(ar); // 9k
ar[0] = 90;
ar[1] = 50;
fun(ar);
System.out.println(ar[1]); // 5
}
Now, lets analyze the given code -
Example 5
Consider the below code:
public static void main() {
float y = 7.84f;
int[][] mat = new int[3][4];
System.out.println(mat); // 9k
System.out.println(mat[1]); // 3k
System.out.println(mat[1][3]); // 0
}
Now, lets analyze the given code -
Example 6
Consider the below code:
static void sum(int[][] mat){
System.out.println(mat); // 2k
System.out.println(mat[0][0] + mat[1][0]); // 40
}
public static void main() {
int[][] mat = new int[2][3];
mat[0][0] = 15;
mat[1][0] = 25;
sum(mat);
}
Now, lets analyze the given code -
Example 7
Consider the below code:
static int sumOfRow(int[] arr){
System.out.println(arr); // 7k
int sum = 0;
for (int i = 0; i < arr.length; i++){
sum = sum + arr[i];
}
return sum;
}
public static void main() {
int[][] mat = new int[2][3];
mat[0][0] = 9;
mat[0][1] = 5;
mat[0][2] = 1;
int ans = sumOfRow(mat[0]); // 7k
System.out.println(ans); // 15
}
Now, lets analyze the given code -
Question¶
Predict the Output :
static void change(int a) {
a = 50;
}
public static void main(String args[]) {
int a = 10;
change(a);
System.out.println(a);
}
Choices
- 10
- 50
- Error
Explanation
- The parameter variable 'a' of change function is reassigned to the value of 50, because both the functions have their own variables, so the variable "a" of main function is different than of variable "a" in change function.
- Stack changes are temporary.
Question¶
Predict the output :
static void change(int[]a) {
a[0] = 50;
}
public static void main(String args[]) {
int[]a = {10};
change(a);
System.out.println(a[0]);
}
Choices
- 10
- 50
- Error
Explanation:
- The array a in change method and main method both refer to the same array object in the heap.
- Heap changes are permanent changes.
Question¶
Predict the output :
static void test(int[]a) {
a = new int[1];
a[0] = 50;
}
public static void main(String args[]) {
int[]a = {10};
test(a);
System.out.println(a[0]);
}
Choices
- 10
- 50
- Error
Explanation:
Inside the test method, a new integer array with length 1 is allocated on the heap memory, and the reference to this array is assigned to the parameter variable a. Hence, now the variable 'a' inside test function and main function point to different references.Heap changes are permanent.
Question¶
Predict the output:
static void fun(int[] a) {
a = new int[1];
a[0] = 100;
}
public static void main() {
int[] a = {10, 20, 30};
fun(a);
System.out.println(a[0]);
}
Choices
- 10
- 100
- Error
- inky pinky po
Explanation:
Inside the fun method, a new integer array with length 1 is allocated on the heap memory, and the reference to this array is assigned to the parameter variable a. Hence, now the variable 'a' inside test function and main function point to different references.
Question¶
Predict the output :
static void swap(int a,int b) {
int temp = a;
a = b;
b = temp;
}
public static void main(String args[]) {
int a = 10;
int b = 20;
swap(a,b);
System.out.println(a + " " + b);
}
Choices
- 10 20
- 20 10
- 10 10
- Error
Explanation:
- Swap function is called by value not by reference.
- So, the changes made in the swap function are temporary in the memory stack.
- Once we got out of the swap function, the changes will go because they are made in temporary variables.
- Hence no swapping is done and variable have the same value as previous.
Question¶
Predict the output :
static void swap(int[]a,int[]b) {
int temp = a[0];
a[0] = b[0];
b[0] = temp;
}
public static void main(String args[]) {
int[]a = {10};
int[]b = {20};
swap(a,b);
System.out.println(a[0] + " " + b[0]);
}
Choices
- 10 20
- 20 10
- 10 10
- Error
Explanation:
Inside swap function, the array variables 'a' & 'b' are passed by reference, so they are pointing to same references in the heap memory as of 'a' & 'b' variables inside main function.
Question¶
Predict the output :
static int[] fun(int[]a) {
a = new int[2];
a[0] = 50; a[1] = 60;
return a;
}
public static void main(String args[]) {
int[]a = {10,20,30};
a = fun(a);
System.out.println(a[0]);
}
Choices
- 10
- 50
- Error
Explanation:
- When fun method is called on array a, then a new integer array is allocated on the heap memory.
- But since, we are returning the new array in the main method, so now the changes done in fun method persists.
Question¶
Predict the output :
static void test(int[]a) {
a = new int[2];
a[0] = 94;
}
public static void main(String args[]) {
int[]a = {10,20,30};
test(a);
System.out.println(a[0]);
}
Choices
- 10
- 94
- Error
Explanation:
Inside the test function, a new integer array with length 2 is allocated on the heap memory, and the reference to this array is assigned to the parameter variable a. Hence, now the variable 'a' inside test function and main function point to different references.