Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory Management & Method Calls in Java Program Execution © Allan C. Milne v15.3.1.

Similar presentations


Presentation on theme: "Memory Management & Method Calls in Java Program Execution © Allan C. Milne v15.3.1."— Presentation transcript:

1

2 Memory Management & Method Calls in Java Program Execution © Allan C. Milne v15.3.1

3 Agenda. The role of the JVM. The stack & the heap. Stack management. Calling methods. Creating objects.

4 The JVM … … automatically manages memory; … allocates memory for variables, parameters and objects; … frees memory locations when they are no longer reachable.

5 JVM will allocate memory… … on the stack for local variables and formal parameters. … on the heap for objects; this will include space for the fields.

6 Stack & heap memory. int i = 123; String s = "dog"; Stack Heap i 123 s "dog"

7 Stack management. A stack is used because storage is –allocated on the top of the stack (pushed) –freed from the top of the stack (popped), The stack is used for local variables, temporary values in expression evaluation and for handling the calling of methods.

8 Local variables. … … … { double d; String s; Stack On block entry storage locations for local variables are allocated on the stack. … … … d s } On block exit the storage locations for the local variables is freed from the stack.

9 Local variables … … … { double d; String s; Stack … … … }

10 Formal parameters. class MyClass { … … … public void myMethod (int i, String s) { … … … } These are known as formal parameters. Within the method body these are treated as for local variables.

11 Actual parameters. MyClass obj = new MyClass(); String s = "wow"; obj.myMethod (123, s); … … … These are known as actual parameters. Actual parameters can be constants, variables or expressions. These are evaluated when the method is called.

12 Method calling mechanism. 1.Allocate a stack frame including storage locations for the formal parameters. 2.Evaluate the actual parameters and assign the values to the formal parameters. 3.Execute the method body;  this includes memory alocation for any local variables. 4.On return from the method all stack allocations are freed.

13 The method call in action. … … … Stack Heap MyClass obj = new MyClass (); obj String s = "wow" s "wow" obj.myMethod (123, s); 1.Allocate a stack frame including storage locations for the formal parameters. i s 2.Evaluate the actual parameters and assign the values to the formal parameters. 123 3.Execute method body – first allocate storage locations for local variables. … 4.On return from the method all stack allocations are freed.

14 The method call in action. … … … Stack Heap obj s "wow" 4.On return from the method all stack allocations are freed.

15 Stack summary. So we've seen how the stack is pushed & popped as storage locations are allocated and freed in a structured way. This process reflects –the entering & exiting of program blocks –calling & returning from method calls. It reflects the scope and the lifetime of variables.

16 Using the heap. Space for objects is always allocated on the heap. This allocation is on a demand basis. The space allocated for an object includes the space for all fields. Space for an object is automatically freed when it is no longer accessible; that is, when it is not referenced by any variables or fields.

17 The new operator. This is the main mechanism used to create objects. –It requests the correct amount of heap memory from the JVM, –calls the appropriate constructor method, –returns the reference to the new object.

18 So draw the picture of … class Message { private int mNumber; private String mText; public Message (int i, String s) { mNumber = i; mText = s; } … … … } Message msg = new Message (5, “Hello”);  With the type defined as …


Download ppt "Memory Management & Method Calls in Java Program Execution © Allan C. Milne v15.3.1."

Similar presentations


Ads by Google