View
216
Download
0
Embed Size (px)
Citation preview
Memory organization and usage
• A computer’s memory is, at its lowest level, composed of binary digits (bits).
• The memory is organized into bytes, which are groups of eight bits.
• Each byte has a unique address in memory.• A byte is the smallest addressable unit of
memory (i.e. nothing smaller than a byte has its own address)
Memory organization
Process BProcess A Process C
STATICSEGMENT
RUNTIMESTACK
FREE/AVAILABLEMEMORY
HEAP
dynamically allocated memory
Memory organization
STATICSEGMENT
RUNTIMESTACK
FREE/AVAILABLEMEMORY
HEAP
main(){…methodA()…}methodA(){…methodB()…}methodB(){…methodC()…}methodC(){…}
main()
Memory organization
STATICSEGMENT
RUNTIMESTACK
FREE/AVAILABLEMEMORY
HEAP
main(){…methodA()…}methodA(){…methodB()…}methodB(){…methodC()…}methodC(){…}
main() methodA()
Memory organization
STATICSEGMENT
RUNTIMESTACK
FREE/AVAILABLEMEMORY
HEAP
main(){…methodA()…}methodA(){…methodB()…}methodB(){…methodC()…}methodC(){…}
main() methodA() methodB()
Memory organization
STATICSEGMENT
RUNTIMESTACK
FREE/AVAILABLEMEMORY
HEAP
main(){…methodA()…}methodA(){…methodB()…}methodB(){…methodC()…}methodC(){…}
main() methodA() methodB() methodC()
Memory organization
STATICSEGMENT
RUNTIMESTACK
FREE/AVAILABLEMEMORY
HEAP
main(){…methodA()…}methodA(){…methodB()…}methodB(){…methodC()…}methodC(){…}
main() methodA() methodB()
Memory organization
STATICSEGMENT
RUNTIMESTACK
FREE/AVAILABLEMEMORY
HEAP
main(){…methodA()…}methodA(){…methodB()…}methodB(){…methodC()…}methodC(){…}
main() methodA()
Memory organization
STATICSEGMENT
RUNTIMESTACK
FREE/AVAILABLEMEMORY
HEAP
main(){…methodA()…}methodA(){…methodB()…}methodB(){…methodC()…}methodC(){…}
main()
Memory organization
• Table at right shows 16 bytes, each consisting of 8 bits
• Each byte has an address, shown in the column to the left
21380000
21380001
21380002
21380003
21380004
21380005
21380006
21380007
21380008
21380009
21380010
21380011
21380012
21380013
21380014
21380015
Arrays
• A collection of variables, all of the same type.
• Each variable in an array is accessed by an index.
• An array of size n has indices from 0 to n-1.
• An array occupies a contiguous block of memory.
• The size of an array is fixed at creation time.
Accessing an array member
• t type of elements in array
• s size (in bytes) of an element of type t
• b base address of array
• address of element i isb + i * s
Array access example
In Java, an int occupies 4 bytes:
int [] a = new int[5];
The base address of ‘a’ is 21380002.
Indices are 0, 1, 2, 3 and 4.
Total size needed for array is 20 bytes
(5 cells times 4 bytes per cell)
21380000
21380001
21380002
21380003
21380004
21380005
21380006
21380007
21380008
21380009
21380010
21380011
21380012
21380013
21380014
21380015
21380016
21380017
21380018
21380019
21380020
21380021
21380021
21380022
Array access example
Where is a[0]?
Address of a[0] is:
b + s * i
where b = 21380002, s = 4 and i = 0:
21380002 + 4 * 0 = 21380002
a[0] occupies bytes 21380002, 21380003, 21380004 and 21380005.
21380000
21380001
21380002
21380003
21380004
21380005
21380006
21380007
21380008
21380009
21380010
21380011
21380012
21380013
21380014
21380015
21380016
21380017
21380018
21380019
21380020
21380021
21380021
21380022
a[0]
Array access example
Where is a[3]?
Address of a[3] is:
b + s * i
where b = 21380002, s = 4 and i = 3:
21380002 + 4 * 3 = 21380014
a[3] occupies bytes 21380014, 21380015, 21380016 and 21380017.
21380000
21380001
21380002
21380003
21380004
21380005
21380006
21380007
21380008
21380009
21380010
21380011
21380012
21380013
21380014
21380015
21380016
21380017
21380018
21380019
21380020
21380021
21380021
21380022
a[3]
Array access example
• Where is a[7]? There is no such array cell, so what happens if we try to access it?
• In some languages (e.g. C and C++) the access is permitted, with unpredictable results.
• Java throws an ArrayIndexOutOfBoundsException
Scope and Lifetime
• Variables in Java have two properties that relate to their storage and accessibility in memory, their scope, and their lifetime.
Variable Scope
• The scope of a variable is the region of a source program in which it can be used.
• the scope of a local variable extends from the point of declaration of the variable to the end of the smallest program block enclosing the declaration. This is often the method in which it is declared, but in the inline declaration of a for loop, it is only the for loop.
Scope Continued
public void foo (){try {
int i = 0;}catch (Exception e){
System.out.println(“Oops”);}…i += 23; // NOT OK
}
Scope Continued
• The scope of an instance variable is the body of the class in which it is declared.
• For a public/protected variable the scope extends into subclasses.
Scope Continued
public class Foo {int i = 0;public void bar(){
if (true){int i = 10; // OKSystem.out.println(“i is”+i); // OK
}System.out.println(“i is”+i); // OK
}}
Scope Continued
public class Foo {
int i = 0;
public void bar(int i){
System.out.println(“i is”+i); // OK
}
}
Lifetime
• The lifetime of a variable is the time period during the running of the program that the variable exists (and is accessible).
• Once the lifetime of a variable is past, it is available to be garbage collected.
Lifetime Continued
• The lifetime of a local variable is the duration of a call to the method in which the variable is declared.
Lifetime Continued
• The lifetime of an instance variable is exactly the same as the lifetime of the variable's object (the object of which the variable is a part). This will be as long as a valid reference to the object exists.
Memory Allocation
• Space for a local (non object) variable is allocated on the runtime stack.
• Since objects in Java are all given space on the heap, instance variables exist on the heap.
• Arrays and objects cannot be allocated on the stack because they may persist after the method which created them has exited.