Post on 29-Dec-2015
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)
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)
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
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
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)
Creating variables: how to reserve RAM memory (cont.)
• Answer:
• The computer program reserves some RAM memory for special system variables in advance:
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.
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
Creating variables: how to reserve RAM memory (cont.)
• Check in the list of used memory location if it is still unused:
Creating variables: how to reserve RAM memory (cont.)
• Reserve memory location 2000 for the variable by marking the location 2000 as used:
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 !!!
Destroying variables
• Here is the situation after creating 3 variables:
The memory locations 2000, 2001 and 2002 have been marked as used.
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 !
Destroying variables (cont.)
• How to destroy a variable:
• We simply remove the memory location from the used memory location list:
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 !!!
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:
More efficient way to represent used memory cells (cont.)
• Problem with this approach:
• Need to use a large number of entries
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:
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
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.
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:
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
Terminology: stack and heap (cont.)
• Note:
• The name "stack" is derived from the similarity with a "stack of book":
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.
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:
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:
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).
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 !!!
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
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)
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)
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:
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:
Initial memory organization (cont.)
• (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.
Out-of-memory error
• It is possible that the area 2 or area 3 grows so large that the RAM memory becomes exhausted:
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
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.
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)
Terminology: System stack and System heap (cont.)
• Area 2 however, is a heap
This area is called the System heap.