Java Memory Management - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Java Memory Management

Description:

When a program is executed, its memory area typically consists of: the code segment: a storage area for a program or the part of the program that is executing ... – PowerPoint PPT presentation

Number of Views:133
Avg rating:3.0/5.0
Slides: 16
Provided by: jasonm6
Category:
Tags: area | code | java | management | memory

less

Transcript and Presenter's Notes

Title: Java Memory Management


1
Topic 7
  • Java Memory Management
  • (before Recursion)

2
Chapter Objectives
3
Memory Allocation in General
  • When a program is executed, its memory area
    typically consists of
  • the code segment a storage area for a program or
    the part of the program that is executing
  • the runtime stack used when calling methods, to
    hold local variables, parameters, returned
    values, etc.
  • the heap used to hold dynamic data (e.g. objects)

4
Memory Allocation in Java
  • separate areas of memory are allocated for each
    class, interface, object, and running method
  • runtime stack used for method information
  • heap used for static information (interfaces and
    classes) and instance information (objects)
  • example what happens when an object is created
    by new?
  • a reference variable is put on the runtime stack
  • an object is created using memory in the heap

5
Runtime Stack
  • runtime stack is the memory space used when a
    method is executed
  • keeps track of method invocations, holds local
    variables, parameters, etc.
  • runtime stack is also called program stack or
    system stack
  • when a method is invoked, an activation record
    that represents the invocation is pushed onto the
    runtime stack

6
Activation Record
  • an activation record ( or call frame ) contains
  • address to return to after method ends
  • copy of the methods local variables
  • copy of the methods parameters for that
    invocation

7
Example
  • when the main method is invoked, an activation
    record for main is created and pushed onto the
    runtime stack
  • (diagram)

8
Example (contd)
  • suppose main calls the method m2
  • an activation record for m2 is created and pushed
    onto the runtime stack
  • suppose m2 calls m3
  • an activation record for m2 is created and pushed
    onto the runtime stack
  • when m3 terminates, its activation record is
    popped off and control returns to m2
  • when m2 terminates, its activation record is
    popped off and control returns to main
  • when main terminates, its activation record is
    popped off and control returns to the operating
    system

9
Heap
  • static space contains one copy of each class and
    interface named in the program
  • contains their static variables and methods
  • object space
  • information is stored about each object
  • value of its instance variables
  • type of object (i.e. name of class)
  • memory address of object (unique)

10
Object Creation
  • memory is allocated in the heap area when an
    object is created using new
  • the reference variable is put in the call frame
    on the runtime stack
  • an object is created using memory in the heap

11
Memory Deallocation
  • what happens when a method returns?
  • on the runtime stack, the call frame is
    automatically popped when the method returns
  • so, that memory is deallocated

12
Garbage Collection
  • what happens to objects on the heap?
  • an object stays on the heap even if there is no
    longer a variable referencing it
  • so, Java has automatic garbage collection it
    regularly identifies objects which no longer have
    a variable referencing them, and deallocates the
    memory

13
Parameter Passing in Java
  • pass by value used with simple variables
    (integers, doubles, etc.)
  • a copy of the contents of the actual parameter is
    used by the method
  • the method can change the copy
  • can it change the original?

14
Parameter Passing (contd)
  • pass by reference used with reference variables
  • a copy of the contents of the actual parameter is
    used by the method
  • but, what is that? a reference to the object
  • so, the method can change the object, since it
    knows where it is
  • note that in fact, the reference is being passed
    by value

15
Examples
Write a Comment
User Comments (0)
About PowerShow.com