Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory organization - storing variables efficiently in the RAM memory.

Similar presentations


Presentation on theme: "Memory organization - storing variables efficiently in the RAM memory."— Presentation transcript:

1 Memory organization - storing variables efficiently in the RAM memory

2 Previously discussed: storing program instructions in RAM memory Instructions (encoded using binary numbers) of a computer program are stored in the RAM memory - (See: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/01 /intro-computer2.html)

3 Previously discussed: storing program instructions in RAM memory (cont.) Alternate (more comprehensive) view of the content of the RAM memory: (I am using a 1000000 byte RAM memory in the example - that's why the last address is 999999)

4 Previously discussed: storing program instructions in RAM memory (cont.) Instructions in a Java program are static while the program is running: The instructions of a Java program will not change for the entire duration that the Java program is executing

5 Creating variables: how to reserve RAM memory Variables are also placed in RAM memory Example: create one variable We have used the memory cell with address 1000 to store the value of the variable

6 Creating variables: how to reserve RAM memory $64,000 question: If we want to create another variable, how can the computer know which memory cell to use ??? I.e.: how can the computer tell that the memory cell 1000 is now used (while previously, it was unused)

7 Creating variables: how to reserve RAM memory (cont.) Answer: The computer program reserves some RAM memory for special system variables in advance:

8 Creating variables: how to reserve RAM memory (cont.) One of these system variables will remember the following information: The start of the unused portion of memory A list of memory locations that has been used.

9 Creating variables: how to reserve RAM memory (cont.) Example:

10 Creating variables: how to reserve RAM memory (cont.) What happens when you create a variable: Look up the start of the unused memory: We find: 2000

11 Creating variables: how to reserve RAM memory (cont.) Check in the list of used memory location if it is still unused:

12 Creating variables: how to reserve RAM memory (cont.) Reserve memory location 2000 for the variable by marking the location 2000 as used:

13 Creating variables: how to reserve RAM memory (cont.) The next time a variable is created, the system knows that the memory location 2000 is used !!!

14 Destroying variables Here is the situation after creating 3 variables: The memory locations 2000, 2001 and 2002 have been marked as used.

15 Destroying variables (cont.) If variable 1 is destroyed, the memory call 2000 is no longer used. Furthermore, the memory call can be re-used by some other variable !

16 Destroying variables (cont.) How to destroy a variable: We simply remove the memory location from the used memory location list:

17 Destroying variables (cont.) Note: If the program creates a new variable while in this situation: It can detect that memory call 2000 is not used and can re- assign this memory call to another variable !!!

18 More efficient way to represent used memory cells Our current solution: We enter an individual memory address in the used memory location list to indicate that the memory location has been used:

19 More efficient way to represent used memory cells (cont.) Problem with this approach: Need to use a large number of entries

20 More efficient way to represent used memory cells (cont.) A more efficient solution: use a range:

21 The Swiss cheese effect If the process of variable creation and variable destruction has gone on for some time, there will be regions of used and unused memory cells all over the place in memory:

22 The Swiss cheese effect (cont.) I call this the Swiss cheese effect Note: We need to use 1 pair of addresses to record a used region of memory locations

23 A special sequence of variable creation and destruction The is one special sequence of variable creation and destruction that will not create holes in the RAM memory: the Last In, First Out sequence: A variable that is created later is always destroyed first.

24 A special sequence of variable creation and destruction (cont.) Example: Create variable 1:

25 A special sequence of variable creation and destruction (cont.) Create variable 2:

26 A special sequence of variable creation and destruction (cont.) Create variable 3:

27 A special sequence of variable creation and destruction (cont.) When you destroy the variables in the reverse order, you preserve the region... Destroy variable 3:

28 A special sequence of variable creation and destruction (cont.) Destroy variable 2:

29 Terminology: stack and heap Stack: Heap: Stack = an area of RAM memory used for variables that are created and destroyed in a Last In, First Out (LIFO) manner Heap = an area of RAM memory used for variables that are created and destroyed in a non-LIFO manner

30 Terminology: stack and heap (cont.) Note: The name "stack" is derived from the similarity with a "stack of book":

31 Terminology: stack and heap (cont.) The last book you put on a stack sits at the top (stack top) The first book that you can remove without causing the stack to collapse is the book at the top of the stack The sequence of book inserting and book deletion is Last In, First Out.

32 Memory organization: where different things are stored in memory In order to use the computer RAM memory as efficiently as possible, the computer RAM memory is organized into 3 parts:

33 Memory organization: where different things are stored in memory In order to use the computer RAM memory as efficiently as possible, the computer RAM memory is organized into 3 parts:

34 Memory organization: where different things are stored in memory (cont.) Explanation: Area 1 contains information that are used throughout the execution of the entire program (or what I call "persistent information") Information stored in this area consists of 2 types of items: Computer instructions (you need all instructions in the computer program throughout its execution). Class variables (this kind of variables store information that is used throughout the program execution).

35 Memory organization: where different things are stored in memory (cont.) The amount of memory space in area 1 remains unchanged throughout the execution of the program !!!

36 Memory organization: where different things are stored in memory (cont.) Area 2 contains instance variables that used by multiple methods (long term information) The space of Area 2 can grow or shrink: Area 2 will grow when the program creates new instance variables Area 2 will shrink when some c instance variables are destroyed

37 Memory organization: where different things are stored in memory (cont.) Area 3 contains local and parameter variables that used by one single method (short term information) Local and parameter variables are created when a method is invoked (starts executing) Local and parameter variables are destroyed when a method is returns (exits)

38 Memory organization: where different things are stored in memory (cont.) The space in Area 3 can also grow or shrink: Area 3 will grow when a method is invoked Area 3 will shrink when a method is returns (exits)

39 Memory organization: where different things are stored in memory (cont.) Direction of growth of the areas 2 and 3: In order to use the available RAM memory as efficient as possible, the direction of growth of areas 2 and 3 is towards each other Graphically:

40 Initial memory organization Consider the following computer program:

41 Initial memory organization (cont.) When a program first starts running, the computer RAM memory will only contain the persistent information I.e., only the program instructions and class variables are stored in RAM:

42 Initial memory organization (cont.)

43 (Since we do not learned about class variables, our example only has program instructions) The area 2 and area 3 will grow and shrink depending on whether some variables are created and destroyed.

44 Out-of-memory error It is possible that the area 2 or area 3 grows so large that the RAM memory becomes exhausted:

45 Out-of-memory error (cont.) Out-of-memory error: When you try to create a variable and the computer has run out of unused RAM memory, it will cause a out-of- memory error The program will terminate immediately

46 Out-of-memory error (cont.) Some situations that can cause out-of-memory errors: The program has an infinite loop and inside the loop, the program creates a new variable When we discuss recursion later in the course, I will warn you about the "infinite recursion" scenario that can cause out-of-memory errors.

47 Terminology: System stack and System heap Later in the course, you will learn that the sequence of variable creation and destruction in area 3 is LIFO Therefore: Area 3 is a stack !!! In fact, this region of memory is called the System stack (There are no "holes" of unused regions in this area)

48 Terminology: System stack and System heap (cont.) Area 2 however, is a heap This area is called the System heap.

49 Terminology: System stack and System heap (cont.) Summary:


Download ppt "Memory organization - storing variables efficiently in the RAM memory."

Similar presentations


Ads by Google