Example 1 Managing Memory on Stack public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(String[] args) { int a = 2; f(a); System.out.println(a); } } public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(...) { int a = 2; f(a); System.out.println(a); } } main(...) a args Stack public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(...) { int a = 2; f(a); System.out.println(a); } } main(...) a 2 args Stack public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(...) { int a = 2; f(a); System.out.println(a); } } f(2) z ? y ? x 2 main(...) a 2 args ... Stack public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(...) { int a = 2; f(a); System.out.println(a); } } f(2) z ? y 1 x 2 main(...) a 2 args ... Stack public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(...) { int a = 2; f(a); System.out.println(a); } } f(2) z ? y 3 x 2 main(...) a 2 args ... Stack public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(...) { int a = 2; f(a); System.out.println(a); } } f(2) z 10 y 3 x 2 main(...) a 2 args ... Stack public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(...) { int a = 2; f(a); System.out.println(a); } } f(2) z 10 y 3 x 30 main(...) a 2 args ... Stack public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(...) { int a = 2; f(a); System.out.println(a); } } f(2) z 10 y 3 x 30 main(...) a 2 args ... Stack public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(...) { int a = 2; f(a); System.out.println(a); } } f(2) z 10 y 3 x 30 main(...) a 2 args ... Stack public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(...) { int a = 2; f(a); System.out.println(a); } } main(...) a 2 args ... Stack public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(...) { int a = 2; f(a); System.out.println(a); } } main(...) a 2 args ... Stack public class Test1 { public static void f(int x) { int y = 1; y = x + y; int z = 10; x = y * z; } public static void main(...) { int a = 2; f(a); System.out.println(a); } } Stack Example 2 Managing Memory in Heap public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); boolean b; b = lt.isOn(); System.out.println(b); } } main(...) b ? lt ? args ... Stack public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); boolean b; b = lt.isOn(); System.out.println(b); } } main(...) b ? lt ? args ... Stack public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); boolean b; b = lt.isOn(); System.out.println(b); } } 18540 on main(...) false b ? lt 18540 args Heap ... Stack public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); boolean b; b = lt.isOn(); System.out.println(b); } } 18540 on main(...) false b ? lt 18540 args Heap ... Stack public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); boolean b; b = lt.isOn(); System.out.println(b); } } 18540 on switchOn() main(...) true b ? lt 18540 args Heap ... Stack public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); boolean b; b = lt.isOn(); System.out.println(b); } } 18540 on main(...) true b true lt 18540 args Heap ... Stack Example 3 Generating Garbage public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); lt = new Light(); System.out.println(lt.isOn()); } } 18540 on true main(...) lt args Heap 18540 ... Stack public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); lt = new Light(); System.out.println(lt.isOn()); } } 18540 on true main(...) 18560 on Heap lt false args 18560 ... Stack public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); lt = new Light(); System.out.println(lt.isOn()); } } 18540 on true main(...) 18560 on Heap lt false args 18560 ... Stack Example 4 Aliasing public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); Light lt2 = lt; lt2.switchOff(); System.out.println(lt.isOn()); } } 18540 on main(...) true lt2 ? lt 18540 args Heap ... Stack public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); Light lt2 = lt; lt2.switchOff(); System.out.println(lt.isOn()); } } 18540 on main(...) true lt2 18540 lt 18540 args Heap ... Stack public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); Light lt2 = lt; lt2.switchOff(); System.out.println(lt.isOn()); } } 18540 on main(...) false lt2 18540 lt 18540 args Heap ... Stack public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); Light lt2 = lt; lt2.switchOff(); System.out.println(lt.isOn()); } } 18540 on main(...) false lt2 18540 lt 18540 args Heap ... Stack Example 5 Avoiding Garbage public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); Light lt2 = lt; lt = new Light(); System.out.println(lt.isOn()); } } 18540 on main(...) true lt2 ? lt 18540 args Heap ... Stack public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); Light lt2 = lt; lt = new Light(); System.out.println(lt.isOn()); } } 18540 on main(...) true lt2 18540 lt 18540 args Heap ... Stack public class LightProg { public static void main(...) { Light lt; lt = new Light(); lt.switchOn(); Light lt2 = lt; lt = new Light(); System.out.println(lt.isOn()); } } 18540 on main(...) true 18560 on Heap false lt2 18540 lt 18560 args ... Stack Example 6 Allocating Arrays public class ArrayTest { public static void main(...) { int[] array; array = new int[5]; array[1] = 12; } } main(...) Heap array ? args ... Stack public class ArrayTest { public static void main(...) { int[] array; array = new int[5]; array[1] = 8; } } 18540 0 0 0 0 0 Heap main(...) array 18540 args ... Stack public class ArrayTest { public static void main(...) { int[] array; array = new int[5]; array[1] = 8; } } 18540 0 8 0 0 0 Heap main(...) array 18540 args ... Stack
© Copyright 2025 Paperzz