25
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

Embed Size (px)

Citation preview

Page 1: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

Memory Management in Java

Computer Science 3Gerb

Objective: Understand references to composite types in Java

Page 2: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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

Page 3: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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

Page 4: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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...

Page 5: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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.

Page 6: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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

1 2 3

0 1 2

Page 7: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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

1 2 3

0 1 2

Page 8: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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

1 2 3

0 1 22

Page 9: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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

1 6 3

0 1 22

Page 10: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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

1 6 3

0 1 22

6

Page 11: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

Another Example

• Consider the following code:

Student st1;

Student st2;

st1 = new Student();

st2 = new Student();

st1 = st2;

Page 12: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

• Declare st1, a reference to a student. Starts out null.Student st1;

Student st2;

st1 = new Student();

st2 = new Student();

st1 = st2;

ST1

Page 13: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

• Declare a second reference, st2, also nullStudent st1;

Student st2;

st1 = new Student();

st2 = new Student();

st1 = st2;

ST2

ST1

Page 14: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

• Create a new Student instance. st1 points to itStudent st1;

Student st2;

st1 = new Student();

st2 = new Student();

st1 = st2;

ST2

ST1Student

Page 15: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

• Create a second Student instance. st2 points to itStudent st1;

Student st2;

st1 = new Student();

st2 = new Student();

st1 = st2;

ST2

ST1Student

Student

Page 16: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

• st1’s reference is changed to refer to st2’s studentStudent st1;

Student st2;

st1 = new Student();

st2 = new Student();

st1 = st2;

ST2

ST1Student

Student

Page 17: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

• Nothing points to the first student any more. – Java cannot use it

– What happens to it?ST2

ST1Student

Student

Page 18: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

• Answer: When memory no longer has any references to it, it is “cleaned up” by Java.

• This behavior is called garbage collection

ST2

ST1Student

Student

Page 19: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

• Answer: When memory no longer has any references to it, it is “cleaned up” by Java.

• This behavior is called garbage collection

ST2

ST1Student

Student

Page 20: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

• Answer: When memory no longer has any references to it, it is “cleaned up” by Java.

• This behavior is called garbage collection

ST2

ST1Student

Page 21: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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++

Page 22: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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.

Page 23: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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

Page 24: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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.

Page 25: Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

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