Storage Management.

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
CS 536 Spring Automatic Memory Management Lecture 24.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Run-Time Storage Organization
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
25-Jun-15 Storage. 2 Parts of a computer For purposes of this talk, we will assume three main parts to a computer Main memory, once upon a time called.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
Linked lists and memory allocation Prof. Noah Snavely CS1114
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Object-Oriented Programming in C++
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
Memory Management -Memory allocation -Garbage collection.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Smart Pointers. Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Memory Management CSCI 2720 Spring What is memory management? “the prudent utilization of this scarce resource (memory), whether by conservation,
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CSE 220 – C Programming malloc, calloc, realloc.
Constructors and Destructors
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Memory Management Memory Areas and their use Memory Manager Tasks:
Storage 18-May-18.
Dynamic Memory Allocation
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Memory Management Memory Areas and their use Memory Manager Tasks:
Automatic Memory Management
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Dynamically Allocated Memory
Storage.
Simulated Pointers.
Simulated Pointers.
Storage 30-Nov-18.
Closure Representations in Higher-Order Programming Languages
Constructors and Destructors
Tonga Institute of Higher Education IT 141: Information Systems
Dynamic Memory.
Heap storage & Garbage collection
Tonga Institute of Higher Education IT 141: Information Systems
Memory Management Memory Areas and their use Memory Manager Tasks:
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Heap storage & Garbage collection
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
COMP755 Advanced Operating Systems
Automating Memory Management
Run-time environments
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Storage Management

The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support dynamic storage allocation on both a stack and a heap It is possible to have many stacks and heaps Allocated storage must eventually be deallocated (freed)

The stack When you enter a subprogram (function, procedure, method), space is allocated on the stack for local variables formal parameters This space persists until the subprogram returns Storage allocation/deallocation is simple

Using the stack Stack allocation/deallocation is automatic You have names for variables on the stack because they're local variables or parameters Stack allocation is perfect for recursion Recursive calls use the same names but different storage The storage is released when function exits This is not always what you want

Purpose of the heap Suppose you write a function whose job is to create a data structure, and suppose that data structure is placed on the stack The data structure disappears when you exit the function You need dynamic storage that you, the programmer, can control The heap provides that dynamic storage

Pointers Allocating storage from the heap is easy Person p = new Person ( ); You get a pointer to the new storage C and C++ provide operations on pointers For example, p++; You can have many pointers to the same storage Pointers are pervasive in C and C++; you can't avoid them

References The Java name for pointers is references The only real difference is that Java does not provide (many) operations on pointers Pointer arithmetic is inherently unsafe You can accidentally point to the wrong thing You cannot in general be sure of the type of the thing you are pointing to Java avoids these problems

Advantages/disadvantages Pointers give you: Greater flexibility and (maybe) convenience A much more complicated syntax More ways to create hard-to-find errors References give you: Less flexibility (no pointer arithmetic) Simpler syntax, like that of any other variable Much safer programs with fewer mysterious bugs

Deallocation There are two potential errors when deallocating (freeing) storage yourself: Deallocating too soon, so that you have dangling references (pointers to storage that has been freed and possibly reused for something else) Forgetting to deallocate, so that unused storage accumulates and you have a memory leak

Ownership If you have to deallocate storage yourself, a good strategy is that of ownership The function that owns the storage is responsible for deallocating it Ownership can be transferred You just need a clearly defined policy for determining ownership In practice, this is easier said than done

Discipline Most C/C++ advocates say: However: It's just a matter of being disciplined I'm disciplined, even if other people aren't Besides, there are good tools for finding memory problems However: Virtually all large C/C++ programs have memory problems

Garbage collection Garbage is storage that has been allocated but is not longer available to the program It's easy to create garbage; just assign a new value to the pointer to that storage A garbage collector automatically searches out garbage and deallocates it Practically every modern language, except C++, uses a garbage collector

Garbage collection algorithms There are two well-known algorithms (and several not so well known ones) for doing garbage collection: Reference counting Mark and sweep

Reference counting When storage is allocated, make it a bit larger so it can hold an integer reference count The reference count keeps track of how many pointers the program has to the storage Any assignment to a pointer variable modifies reference counts When a reference count reaches zero, the storage can be garbage collected

Problems with reference counting If A points to B, and B points to A, then each is referenced, even if nothing else in the program points to either one This fools the garbage collector, which doesn't collect either A or B Thus, reference counting is imperfect and unreliable; memory leaks still happen

Mark and sweep When memory runs low, stop processing and run the garbage collector The collector marks every bit of heap storage It then looks at every program variable, follows every pointer, and unmarks all the storage it can reach When done, the marked storage must not be accessible; it is garbage that can be freed

Problems with mark and sweep Mark-and-sweep is a complex algorithm that takes substantial time Unlike reference counting, it must be done all at once--nothing else can be going on The program stops responding during garbage collection This is unsuitable for real-time applications

Garbage collection in Java Java uses mark-and-sweep Highly reliable, but may cause unexpected slowdowns You can ask Java to do garbage collection when the program has some time to spare But not all implementations respect your request This problem is known and being worked on

The End