Adapted from Prof. Necula CS 169, Berkeley1 Memory Management and Debugging V22.0474-001 Software Engineering Lecture 20.

Slides:



Advertisements
Similar presentations
Paging: Design Issues. Readings r Silbershatz et al: ,
Advertisements

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Dynamic Memory Allocation I Topics Simple explicit allocators Data structures Mechanisms Policies CS 105 Tour of the Black Holes of Computing.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
Garbage Collection Introduction and Overview Christian Schulte Excerpted from presentation by Christian Schulte Programming Systems Lab Universität des.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
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.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
CS510 Advanced OS Seminar Class 10 A Methodology for Implementing Highly Concurrent Data Objects by Maurice Herlihy.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
More on protocol implementation Packet parsing Memory management Data structures for lookup.
Lecture 36: Programming Languages & Memory Management Announcements & Review Read Ch GU1 & GU2 Cohoon & Davidson Ch 14 Reges & Stepp Lab 10 set game due.
Register Allocation (via graph coloring)
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
Garbage collection (& Midterm Topics) David Walker COS 320.
Linked lists and memory allocation Prof. Noah Snavely CS1114
4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
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.
Chapter 1 Computer System Overview Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
IT253: Computer Organization
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
Chapter 4 Memory Management Virtual Memory.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
C++ Memory Overview 4 major memory segments Key differences from Java
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
CSCI Rational Purify 1 Rational Purify Overview Michel Izygon - Jim Helm.
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
CS333 Intro to Operating Systems Jonathan Walpole.
GARBAGE COLLECTION IN AN UNCOOPERATIVE ENVIRONMENT Hans-Juergen Boehm Computer Science Dept. Rice University, Houston Mark Wieser Xerox Corporation, Palo.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Consider Starting with 160 k of memory do: Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p1 (50 k) Allocate p2 (30 k) Allocate p2 (30 k)
CSC 213 – Large Scale Programming. Explicit Memory Management  Traditional form of memory management  Used a lot, but fallen out of favor  malloc /
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Memory-Related Perils and Pitfalls in C
YAHMD - Yet Another Heap Memory Debugger
CSE 374 Programming Concepts & Tools
Storage Management.
Automatic Memory Management
The Hardware/Software Interface CSE351 Winter 2013
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Memory Management Kathryn McKinley.
Closure Representations in Higher-Order Programming Languages
CS5123 Software Validation and Quality Assurance
CSE451 Virtual Memory Paging Autumn 2002
CS703 - Advanced Operating Systems
Presentation transcript:

Adapted from Prof. Necula CS 169, Berkeley1 Memory Management and Debugging V Software Engineering Lecture 20

Adapted from Prof. Necula CS 169, Berkeley2 Outline Overview of memory management –Why it is a software engineering issue Styles of memory management –Malloc/free –Garbage collection –Regions Detecting memory errors

Adapted from Prof. Necula CS 169, Berkeley3 Memory Management A basic decision, because –Different memory management policies are difficult to mix Best to stick with one in an application –Has a big impact on performance and quality Different strategies better in different situations Some more error prone than others

Adapted from Prof. Necula CS 169, Berkeley4 Distinguishing Characteristics Allocation is always explicit Deallocation –Explicit or implicit? Safety –Checks that explicit deallocation is safe

Adapted from Prof. Necula CS 169, Berkeley5 Explicit Memory Management Allocation and deallocation are explicit –Oldest style –C, C++ x = new Foo; … delete x;

Adapted from Prof. Necula CS 169, Berkeley6 A Problem: Dangling Pointers X = new Foo;... Y = X;... delete X;... Y.bar(); Foo X Y

Adapted from Prof. Necula CS 169, Berkeley7 A Problem: Dangling Pointers X = new Foo;... Y = X;... delete X;... Y.bar(); X Y Dangling pointers

Adapted from Prof. Necula CS 169, Berkeley8 Notes Dangling pointers are bad –A system crash waiting to happen Storage bugs are hard to find –Visible effect far away (in time and program text) from the source Not the only potentially bad memory bug in C

Adapted from Prof. Necula CS 169, Berkeley9 Notes, Continued Explicit deallocation is not all bad Gives the finest possible control over memory –May be important in memory-limited applications Programmer is very conscious of how much memory is in use –This is good and bad Allocation and deallocation fairly expensive

Adapted from Prof. Necula CS 169, Berkeley10 Automatic Memory Management I.e., automatic deallocation This is an old problem: –studied since the 1950s for LISP There are well-known techniques for completely automatic memory management Until recently unpopular outside of Lisp family languages

Adapted from Prof. Necula CS 169, Berkeley11 The Basic Idea When an object is created, unused space is automatically allocated –E.g., new X –As in all memory management systems After a while there is no more unused space Some space is occupied by objects that will never be used again –This space can be freed to be reused later

Adapted from Prof. Necula CS 169, Berkeley12 The Basic Idea (Cont.) How can we tell whether an object will “never be used again”? –in general, impossible to tell –use heuristics Observation: a program can use only the objects that it can find: A x  new A; x = y; … –After x  y there is no way to access the newly allocated object

Adapted from Prof. Necula CS 169, Berkeley13 Garbage An object x is reachable if and only if: –a register contains a pointer to x, or –another reachable object y contains a pointer to x You can find all reachable objects by starting from registers and following all the pointers An unreachable object can never be used –such objects are garbage

Adapted from Prof. Necula CS 169, Berkeley14 Reachability is an Approximation Consider the program: x  new A; y  new B; x  y; if(alwaysTrue()) { x  new A } else { x.foo() } After x  y (assuming y becomes dead there) –the object A is unreachable –the object B is reachable (through x) –thus B is not garbage and is not collected but object B is never going to be used

Adapted from Prof. Necula CS 169, Berkeley15 A Simple Example We start tracing from registers and stack –These are the roots Note B and D are unreachable from acc and stack –Thus we can reuse their storage A BC Frame 1 Frame 2 DEacc SP

Adapted from Prof. Necula CS 169, Berkeley16 Elements of Garbage Collection Every garbage collection scheme has the following steps 1.Allocate space as needed for new objects 2.When space runs out: a)Compute what objects might be used again (generally by tracing objects reachable from a set of “root” registers) b)Free the space used by objects not found in (a) Some strategies perform garbage collection before the space actually runs out

Adapted from Prof. Necula CS 169, Berkeley17 Notes on Garbage Collection Much safer than explicit memory management –Crashes due to memory errors disappear –And easy to use But exacerbates other problems –Memory leaks can be hard to find Because memory usage in general is hidden –Different GC approaches have different performance trade-offs

Adapted from Prof. Necula CS 169, Berkeley18 Notes (Continued) Fastest GCs do not perform well if live data is significant percentage of physical memory Should be < 30% If > 50%, quite dramatic performance degradation Pauses are not acceptable in some applications –Use real-time GC, which is more expensive Allocation can be very fast Amortized deallocation can be very fast, too

Adapted from Prof. Necula CS 169, Berkeley19 Finding Memory Leaks A simple automatic technique is effective at finding memory leaks Record allocations and accesses to objects Periodically check –Live objects that have not been used in some time –These are likely leaked objects This can find bugs even in GC languages!

Adapted from Prof. Necula CS 169, Berkeley20 A Different Approach: Regions Traditional memory management: free GC Safety - + Control + - Ease of use - + Space usage + - A different approach: regions safety and efficiency, expressiveness

Adapted from Prof. Necula CS 169, Berkeley21 Region-based Memory Management Regions represent areas of memory Objects are allocated “in” a given region Easy to deallocate a whole region Region r = newregion(); for (i = 0; i < 10; i++) { int *x = ralloc(r, (i + 1) * sizeof(int)); work(i, x); } deleteregion(r);

Adapted from Prof. Necula CS 169, Berkeley22 Why Regions ? Performance Locality benefits Expressiveness Memory safety

Adapted from Prof. Necula CS 169, Berkeley23 Region Performance: Allocation and Deallocation Applies to delete all-at-once only Basic strategy: –Allocate a big block of memory –Individual allocation is: pointer increment overflow test –Deallocation frees the list of big blocks ) All operations are fast a region alloc point wastage

Adapted from Prof. Necula CS 169, Berkeley24 Region Performance: Locality Regions can express locality: –Sequential allocs in a region can share cache line –Allocs in different regions less likely to pollute cache for each other Example: moss (plagiarism detection software) –Small objects: short lived, many clustered accesses –Large objects: few accesses

Adapted from Prof. Necula CS 169, Berkeley25 1-region version: small & large objects in 1 region 2-region version: small & large objects in 2 regions 45% fewer cycles lost to r/w stalls in 2-region version Region Performance: Locality - moss

Adapted from Prof. Necula CS 169, Berkeley26 Region Expressiveness Adds some structure to memory management Few regions: –Easier to keep track of –Delay freeing to convenient "group" time End of an iteration, closing a device, etc No need to write "free this data structure" functions

Adapted from Prof. Necula CS 169, Berkeley27 Region Expressiveness: lcc The lcc C compiler –regions bring structure to an application's memory perm func stmt Time

Adapted from Prof. Necula CS 169, Berkeley28 Region Expressiveness: lcc The lcc C compiler, written using unsafe regions –regions bring structure to an application's memory perm func stmt Time

Adapted from Prof. Necula CS 169, Berkeley29 Region Expressiveness: lcc The lcc C compiler, written using unsafe regions –regions bring structure to an application's memory perm func stmt Time

Adapted from Prof. Necula CS 169, Berkeley30 Region Expressiveness: lcc The lcc C compiler, written using unsafe regions –regions bring structure to an application's memory perm func stmt Time

Adapted from Prof. Necula CS 169, Berkeley31 Region Expressiveness: lcc The lcc C compiler, written using unsafe regions –regions bring structure to an application's memory perm func stmt Time

Adapted from Prof. Necula CS 169, Berkeley32 Region Expressiveness: lcc The lcc C compiler, written using unsafe regions –regions bring structure to an application's memory perm func stmt Time

Adapted from Prof. Necula CS 169, Berkeley33 Summary regionsfree GC Safety Control Ease of use = - + Space usage Time + + +

Adapted from Prof. Necula CS 169, Berkeley34 Region Notes Regions are fast –Very fast allocation –Very fast (amortized) deallocation –Can express locality Only known technique for doing so Good for memory-intensive programs –Efficient and fast even if high % of memory in use

Adapted from Prof. Necula CS 169, Berkeley35 Region Notes (Continued) Does waste some memory –In between malloc/free and GC Requires more thought than GC –Have to organize allocations into regions

Adapted from Prof. Necula CS 169, Berkeley36 Summary You must pay attention to memory management –Can affect the design of many system components For applications with low-memory, no real time constraints, use GC –Easiest strategy for programmer For high-memory or high-performance applications, use regions

Adapted from Prof. Necula CS 169, Berkeley37 Run-Time Monitoring Recall from testing: –How do you know that a test succeeds? –Can check intermediate results, using assert This is called run-time monitoring (RTM) –Makes testing more effective

Adapted from Prof. Necula CS 169, Berkeley38 What do we Monitor? Check the result of computation –E.g., the result of matrix inversion Hardware-enforced monitoring –E.g., division-by-zero, segmentation fault Programmer-inserted monitoring –E.g., assert statements

Adapted from Prof. Necula CS 169, Berkeley39 Automated Run-Time Monitoring Given a property Q that must hold always … and a program P Produce a program P’ such that: –P’ always produces the same result as P –P’ has lots of assert(Q) statements, at all places where Q may be violated –P’ is called the instrumented program We are interested in automatic instrumentation

Adapted from Prof. Necula CS 169, Berkeley40 RTM for Memory Safety A technique for finding memory bugs –Applies to C and C++ C/C++ are not type safe –Neither the compiler nor the runtime system enforces type abstractions Possible to read or write outside of your intended data structure

Adapted from Prof. Necula CS 169, Berkeley41 Picture memoryobjects A Access to A

Adapted from Prof. Necula CS 169, Berkeley42 The Idea Each byte of memory is in one of three states: Unallocated –Cannot be read or written Allocated but uninitialized –Cannot be read Allocated and initialized –Anything goes

Adapted from Prof. Necula CS 169, Berkeley43 State Machine Associate an automaton with each byte Missing transition edges indicate an error Unallocated Uninitialized Initialized malloc write free write read

Adapted from Prof. Necula CS 169, Berkeley44 Instrumentation Check the state of each byte on each access Binary instrumentation –Add code before each load and store –Represent states as giant array 2 bits per byte of memory 25% memory overhead –Catches byte-level errors –Won’t catch bit-level errors

Adapted from Prof. Necula CS 169, Berkeley45 Picture memoryobjects A Access to A Note: We can detect invalid accesses to red areas, but not to blue areas.

Adapted from Prof. Necula CS 169, Berkeley46 Improvements We can only detect bad accesses if they are to unallocated or uninitialized memory So try to make most of the bad accesses be of those two forms –Especially, the common off-by-one errors

Adapted from Prof. Necula CS 169, Berkeley47 Red Zones Leave buffer space between allocated objects –The “red zone” –In what state do we put this zone? Guarantees that walking off the end of an array accesses unallocated memory

Adapted from Prof. Necula CS 169, Berkeley48 Aging Freed Memory When memory is freed, do not reallocate immediately –Wait until the memory has “aged” Helps catch dangling pointer errors Red zones and aging are easily implemented in the malloc library

Adapted from Prof. Necula CS 169, Berkeley49 Another Class of Errors: Memory Leaks A memory leak occurs when memory is allocated but never freed. Memory leaks are at least as serious as memory corruption errors We can find many memory leaks using techniques borrowed from garbage collection

Adapted from Prof. Necula CS 169, Berkeley50 The Basic Idea Any memory with no pointers to it is leaked –There is no way to free this memory Run a garbage collector –But don’t free any garbage –Just detect the garbage –Any inaccessible memory is leaked memory

Adapted from Prof. Necula CS 169, Berkeley51 Issues with C/C++ It is sometimes hard to tell what is inaccessible in a C/C++ program Cases –No pointers to a malloc’d block Definitely garbage –No pointers to the head of a malloc’d block Maybe garbage

Adapted from Prof. Necula CS 169, Berkeley52 Leak Detection Summary From time to time, run a garbage collector –Use mark and sweep Report areas of memory that are definitely or probably garbage –Need to report who malloc’d the blocks originally –Store this information in the red zone between objects

Adapted from Prof. Necula CS 169, Berkeley53 Tools for Memory Debugging Purify –Robust industrial tool for detecting all major memory faults –Developed by Rational, now part of IBM Valgrind –Open source tool for linux – “Poor man’s purify” –Implement basic memory checking at source code level –Sample project includes a simple debugger called simpurify