Object Oriented Programming in Java Habib Rostami Lecture
Object Oriented Programming in Java Habib Rostami Lecture 4
Today’s Presentation What is Memory Management? Stack and Heap Memory Management in Java
What is Memory Management? Allocation of memory for variables (States, Objects) Freeing unnecessary memories public void example(int x){ int y = x + 1; }
Managing Memory on Stack 1, 4 args=… public void example(int x){ int y = x + 1; Stack 3 Y=? X=3 } public static void main(String[] args) 2 { 1 example(3); 4 args=… 2 Stack } 3 Y=4 X=3 args=… Stack
Memory Management in Heap public static void main(String[] args) { 1 int a = 10; Point p = new Point(); int b = 20; b=? p=? } a=? 1 args = … Stack Heap
Memory Management in Heap public static void main(String[] args) { int a = 10; 2 Point p = new Point(); int b = 20; b=? p=? } a = 10 2 args = … Stack Heap
Memory Management in Heap public static void main(String[] args) { int a = 10; Point p = new Point(); 3 int b = 20; b=? p = 1048 } 1048 a = 10 Point x=? y=? 3 args = … Stack Heap
Memory Management in Heap public static void main(String[] args) { int a = 10; Point p = new Point(); int b = 20; 4 b = 20 p = 1048 } 1048 a = 10 Point x=? y=? 4 args = … Stack Heap
Memory Management in Heap public static void main(String[] args) { int a = 10; Point p = new Point(); int b = 20; } b = 20 p = 1048 5 1048 a = 10 Point x=? y=? 5 args = … Stack Heap
Memory Management in Heap public static void main(String[] args) { int a = 10; q=? Point p = new Point(); int b = 20; a Point q = p; b b = 20 p = 1048 } 1048 a = 10 Point x=? y=? a args = … Stack Heap
Memory Management in Heap public static void main(String[] args) { int a = 10; q = 1048 Point p = new Point(); int b = 20; a Point q = p; b b = 20 p = 1048 } 1048 a = 10 Point x=? y=? b args = … Stack Heap
Memory Management in Heap public static void main(String[] args) { int a = 10; q = 1048 Point p = new Point(); } int b = 20; a Point q = p; b p = null; c 1048 b = 20 p = 1048 a = 10 Point x=? y=? b args = … Stack Heap
Memory Management in Heap public static void main(String[] args) { int a = 10; q = 1048 Point p = new Point(); } d int b = 20; a Point q = p; b p = null; c 1048 b = 20 p=0 a = 10 Point x=? y=? c args = … Stack Heap
Memory Management in Heap public static void main(String[] args) { int a = 10; q = 1048 Point p = new Point(); } d int b = 20; a Point q = p; b p = null; c 1048 b = 20 p=0 a = 10 Point x=? y=? c args = … Stack Heap
Garbage Collection • A mechanism in java which removes objects that there is no reference to them from the heap memory.
More Insight Memory Management public void example(Point p){ p = new Point(); } public static void main(String[] args) { Point q = new Point(); 1 q = 1048 example(p); 2 args = … } 1048 x=? y=? Point 1 Stack
More Insight Memory Management public void example(Point p){ p = new Point(); 3 } public static void main(String[] args) { Point q = new Point(); p = 1048 1 q = 1048 example(p); 2 args = … } 1048 x=? y=? Point 2 Stack
More Insight Memory Management public void example(Point p){ p = new Point(); 3 } public static void main(String[] args) { Point q = new Point(); p = 2012 1 q = 1048 example(p); 2 4 } args = … 1048 x=? y=? 2012 x=? y=? Point 3 Stack
More Insight Memory Management public void example(Point p){ p = new Point(); 3 } public static void main(String[] args) { Point q = new Point(); p = 2012 1 q = 1048 example(p); 2 4 } args = … 1048 x=? y=? 2012 x=? y=? Point 4 Stack
More Insight Memory Management public void example(Point p){ p. set. X(20); 3 } public static void main(String[] args) { Point q = new Point(); p = 1048 1 q = 1048 example(p); 2 args = … } 1048 x=? y=? Point 2 Stack
More Insight Memory Management public void set. X(int x 1){ x = x 1; public void example(Point p){ p. set. X(20); } 3 } X 1 = 20 public static void main(String[] args) { Point q = new Point(); p = 1048 1 q = 1048 example(p); 2 4 } args = … 1048 x = 20 y=? Point 3 Stack
More Insight Memory Management public void set. X(int x 1){ x = x 1; public void example(Point p){ p. set. X(20); } 3 } X 1 = 20 public static void main(String[] args) { Point q = new Point(); p = 1048 1 q = 1048 example(p); 2 4 } args = … 1048 x = 20 y=? Point 4 Stack
- Slides: 22