Memory organization - storing variables efficiently in the RAM memory.

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Advertisements

Ch. 8 Functions.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Lecture 2 Introduction to C Programming
The computer memory and the binary number system.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CS 536 Spring Code generation I Lecture 20.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Recursion.
Run time vs. Compile time
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
The different kinds of variables in a Java program.
The different types of variables in a Java program.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Nachos Phase 1 Code -Hints and Comments
Computer Security and Penetration Testing
Comp 249 Programming Methodology Chapter 10 – Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Unit 3: Java Data Types Math class and String class.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Stack and Heap Memory Stack resident variables include:
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Problem Solving for Programming Session 8 Static Data Structures.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Introduction to programming in the Java programming language.
C++ Memory Overview 4 major memory segments Key differences from Java
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
The life time of local variables (in a method). Local variables Local variable: A local variable is used to store information that is relevant for the.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Function Recursion to understand recursion you must understand recursion.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Object Lifetime and Pointers
Java Memory Management
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Stacks, Queues, and Deques
The method invocation mechanism and the System Stack
9-10 Classes: A Deeper Look.
Data Structures & Algorithms
Where is all the knowledge we lost with information? T. S. Eliot
9-10 Classes: A Deeper Look.
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Memory organization - storing variables efficiently in the RAM memory

Previously discussed: storing program instructions in RAM memory Instructions (encoded using binary numbers) of a computer program are stored in the RAM memory - (See: /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 byte RAM memory in the example - that's why the last address is )

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.) Example:

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

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

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.) Example: Create variable 1:

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

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

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:

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

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 Consider the following computer program:

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.

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