html5-img
1 / 37

Memory organization and usage

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.

jania
Télécharger la présentation

Memory organization and usage

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Memory organization Process A Process B Process C

  3. Memory organization Process A Process B Process C STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP dynamically allocated memory

  4. Memory organization STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP main() main(){…methodA()…} methodA(){…methodB()…} methodB(){…methodC()…} methodC(){…}

  5. Memory organization STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP main() methodA() main(){…methodA()…} methodA(){…methodB()…} methodB(){…methodC()…} methodC(){…}

  6. Memory organization STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP main() methodA() methodB() main(){…methodA()…} methodA(){…methodB()…} methodB(){…methodC()…} methodC(){…}

  7. Memory organization STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP main() methodA() methodB() methodC() main(){…methodA()…} methodA(){…methodB()…} methodB(){…methodC()…} methodC(){…}

  8. Memory organization STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP main() methodA() methodB() main(){…methodA()…} methodA(){…methodB()…} methodB(){…methodC()…} methodC(){…}

  9. Memory organization STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP main() methodA() main(){…methodA()…} methodA(){…methodB()…} methodB(){…methodC()…} methodC(){…}

  10. Memory organization STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP main() main(){…methodA()…} methodA(){…methodB()…} methodB(){…methodC()…} methodC(){…}

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

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

  13. 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 is b + i * s

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

  15. Array access example a[0] 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.

  16. 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. a[3]

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

  18. Scope and Lifetime • Variables in Java have two properties that relate to their storage and accessibility in memory, their scope, and their lifetime.

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

  20. Scope Continued public void foo (){ int i = 0; … i += 23; // OK }

  21. Scope Continued public void foo (){ if (true){ int i = 0; } … i += 23; // NOT OK }

  22. Scope Continued public void foo (){ for (int i = 0; i < 10; i++){ } … i += 23; // NOT OK }

  23. Scope Continued public void foo (){ try { int i = 0; }catch (Exception e){ System.out.println(“Oops”); } … i += 23; // NOT OK }

  24. Scope Continued public void foo (){ int i = 0; if (true){ int i = 5; // NOT OK } … i += 23; }

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

  26. Scope Continued public class Foo { int i = 0; public void bar(){ i += 23; // OK } }

  27. Scope Continued public class MyFoo extends Foo { public void useSuperclass(){ i += 23; // OK } }

  28. Scope Continued public class Foo { int i = 0; public void bar(){ if (true){ int i = 10; // OK System.out.println(“i is”+i); // OK } System.out.println(“i is”+i); // OK } }

  29. Scope Continued public class Foo { int i = 0; public void bar(int i){ System.out.println(“i is”+i); // OK } }

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

  31. Lifetime Continued • The lifetime of a local variable is the duration of a call to the method in which the variable is declared.

  32. Lifetime Continued public void foo (){ int i = 0; … i += 23; }

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

  34. Lifetime Continued public void Bar (){ Object foo = new Foo(); }

  35. Lifetime Continued public void Bar (){ Object foo = new Foo(); sendReference(foo); }

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

  37. public class Foo{ // INSTANCE VARIABLES private inti = 0; // persists as long as object private Bag b = new Bag(); // as above (persistent) public void bar(){ // LOCAL VARIABLES intj = 10; // fleeting – exists only for method call Bag b2 = new Bag(); // as above (fleeting) } }

More Related