Download presentation
Presentation is loading. Please wait.
Published byNigel Warren Modified over 9 years ago
1
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java
2
References When you declare an array or class instance, you don’t actually create memory for them. Instead you create a reference. A reference tells Java where the class instance or array is stored Initially it is null, meaning it does not point anywhere. When you set it to a new array or class instance, the reference now points to the memory you created. All variables that contain composite types (arrays and instances of classes) have references. Primitive types (boolean, int, double, references) do not have references
3
For Example int[] myArray; –Creates a reference –No array has been created –No memory yet allocated myArray = new int[60]; –Creates an array –myArray now references the new array
4
Assigning Composite Types Assigning a reference to a composite type causes two references to point to the same object. –Different from primitive types –Assigning a primitive type copies the value of one into the other. int a=5,b=6; b=a; //b becomes 5. b=7; //b becomes 7. a stays 5. Does not work the same way for composite types...
5
Assigning Composite Types Example int[] a = {1,2,3}; int[] b = a; System.out.println(b[1]);//Prints 2. a[1]=6; System.out.println(b[1]);//Prints 6! B has been changed because it is set on the second line to point to the same array as a. –b’s and a’s references point to the same place. –Therefore, anything that happens to a, also happens to b.
6
Variable “a” references an array with three elements int[] a = {1,2,3}; int[] b = a; System.out.println(b[1]); a[1]=6; System.out.println(b[1]); a b 123 0 12
7
Variable “b” is set equal to “a”. They now reference the same array int[] a = {1,2,3}; int[] b = a; System.out.println(b[1]); a[1]=6; System.out.println(b[1]); a b 123 0 12
8
Output element 1 of the array that “b” references. int[] a = {1,2,3}; int[] b = a; System.out.println(b[1]); a[1]=6; System.out.println(b[1]); a b 123 0 12 2
9
Set element 1 of the array that “a” references to 6 int[] a = {1,2,3}; int[] b = a; System.out.println(b[1]); a[1]=6; System.out.println(b[1]); a b 163 0 12 2
10
Output element 1 of the array that “b” references again int[] a = {1,2,3}; int[] b = a; System.out.println(b[1]); a[1]=6; System.out.println(b[1]); a b 163 0 12 2 6
11
Another Example Consider the following code: Student st1; Student st2; st1 = new Student(); st2 = new Student(); st1 = st2;
12
Declare st1, a reference to a student. Starts out null. Student st1; Student st2; st1 = new Student(); st2 = new Student(); st1 = st2; ST1
13
Declare a second reference, st2, also null Student st1; Student st2; st1 = new Student(); st2 = new Student(); st1 = st2; ST2 ST1
14
Create a new Student instance. st1 points to it Student st1; Student st2; st1 = new Student(); st2 = new Student(); st1 = st2; ST2 ST1 Student
15
Create a second Student instance. st2 points to it Student st1; Student st2; st1 = new Student(); st2 = new Student(); st1 = st2; ST2 ST1 Student
16
st1’s reference is changed to refer to st2’s student Student st1; Student st2; st1 = new Student(); st2 = new Student(); st1 = st2; ST2 ST1 Student
17
Nothing points to the first student any more. –Java cannot use it –What happens to it? ST2 ST1 Student
18
Answer: When memory no longer has any references to it, it is “cleaned up” by Java. This behavior is called garbage collection ST2 ST1 Student
19
Answer: When memory no longer has any references to it, it is “cleaned up” by Java. This behavior is called garbage collection ST2 ST1 Student
20
Answer: When memory no longer has any references to it, it is “cleaned up” by Java. This behavior is called garbage collection ST2 ST1 Student
21
For Historical Context C++ does not have garbage collection –If you lose all the references to something it’s still there. –But you can’t use it. –This is called a memory leak The main reason why Java is a big improvement on C++
22
The Stack and the Heap Two place Java stores data, the stack and the heap –The stack stores local variables. These are all primitive types (boolean, int, double, references) –The heap stores composite types (arrays and instances of classes) and the primitive types they contain. Rule of thumb: –Memory on the stack is created each time a variable declaration in a method is executed. –Memory on the heap is created only when new is used.
23
Memory on the Stack Stores variables local to a method Very quick - takes no time to allocate Lasts until the method exits Stored in blocks of memory called frames –Each call to a method creates a stack frame –Frame disappears when method exits
24
Memory on the Heap Stores class instances and arrays created with the new operator Takes time to allocate memory –Can occasionally slow a program down Lasts until no longer needed.
25
Summary Primitive types stored on the stack. –Copied when assigned –Disappear when method exits Composite types stored on the heap –References point to them –References initially set to null –The composite type is created when the new operator is used. –Composite types last until no longer needed –When assigned, two references point to the same place
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.