1 / 13

C H A P T E R F I V E

C H A P T E R F I V E. Memory Management. Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan. Memory organization:. Memory management is the process of binding values to memory locations. A process is a program in execution.

kasie
Download Presentation

C H A P T E R F I V E

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. C H A P T E R F I V E Memory Management Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

  2. Memory organization: • Memory management is the process of binding values to memory locations. • A process is a program in execution. • All the memory used by a process must reside in the process’s address space. • How the address space is organized depends on the operating system and the programming language being used.

  3. Memory organization: There are generally three different categories of assigned memory: • Static memory – storage requirements known before run-time can be pre-allocated when the program is loaded (examples?). • Run-time stack – local variables that get allocated each time a function is called (activated) are allocated space on the stack. • Heap – dynamically allocated storage goes on the heap. It is the least organized and most dynamic storage area.

  4. The Structure of Run-Time Memory Figure 5.1 Note that the stack grows from one direction and the heap grows from the other (sort of). Also note that the book’s layout is backwards.

  5. Static memory: • Global variables that can be statically allocated get placed in the static area. • Constants may also be placed in the static area depending on their type. • The static area may be split into different parts for variables and for constants (why?). • Values that can be statically bound (e.g. at compile time) can be placed here.

  6. Run-time stack: • The stack is a contiguous region of memory that grows and shrinks as a process runs. • It is used to hold activation records for functions and procedures. These are also called stack frames. • When a function is called (activated) storage for its local variables, the calling parameters, and return linkage is allocated by growing the stack. • When control is return from the function the stack frame is deallocated and the stack shrinks. • A function’s stack frame exists as long as the function is active.

  7. The heap: • Variable storage that is dynamically allocate at run-time is placed in the heap. • The heap is managed by dividing it into blocks. • As a process runs space is allocated to new variables from heap space (malloc, new). • When a variable’s lifetime expires its space may be returned to the heap (deallocated). This can leave holes in the heap causing fragmentation. • Some languages leave managing the heap in the hands of the programmer (C, C++, etc.). • Others do heap management (Java, Perl, etc.).

  8. Method calls: • Whenever a method is called space for its local variable declarations is allocated on the stack (the stack frame). • Space for the calling arguments may also reside within the stack frame. • The address to return to when the method completes is also saved (return link). • A pointer to the static storage area called the static link may be present. • A pointer to the stack frame of the calling method called the dynamic link will be present.

  9. Structure of a Called Method’s Stack Frame Figure 5.2 Local variables are referenced as offsets from the stack pointer, the start of the current stack frame. Static variables are referenced as offsets from the start of the static area.

  10. Example Program with Methods and Parameters Figure 5.3 The variables h and I are static. All the other declarations create local variables.

  11. Run-Time Stack with Stack Frames for Method Invocations Figure 5.4 But what happens with dynamic scope?

  12. Dynamic scoping: • Much more must happen at run-time (like?). • When a variable is referenced we must follow the chain of dynamic links until we find it! • This implies that the names of variables (or at least some form of id) be kept in the stack frames. • If dynamic typing is used the type of the variable found must also be checked. • Since dynamical typed variables can change their size they can’t be stored within the stack frame (so what to do?). • Put names and pointers in the stack frame and keep the actual variable storage on the heap.

  13. Next time… Passing Arguments

More Related