DOC PREVIEW
Princeton COS 217 - Dynamic Memory Management

This preview shows page 1-2-3-19-20-39-40-41 out of 41 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Dynamic Memory ManagementSlide 2Slide 3Goals of Today’s LectureMemory Layout: HeapAllocating & Deallocating MemoryOption #1: Garbage CollectionChallenges of Garbage CollectionOption #2: Manual DeallocationManual Deallocation Can Lead to BugsSlide 11Slide 12Challenges for Malloc and FreeHeap: Dynamic MemorySlide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Goals for Malloc and FreeKeeping Track of Free BlocksNeed to Minimize FragmentationSimple “K&R-Like” ApproachAllocate Memory in Multiples of Base SizeLinked List of Free Blocks“First-Fit” AllocationSplitting an Oversized Free BlockCircular Linked List of Free BlocksMaintaining Free Blocks in OrderCoalescing Adjacent Free BlocksAn Implementation ChallengeStore Information in the Free BlockConclusionsStupid Programmer Tricks, Part 1Expanding on This ApproachStupid Programmer Tricks, Part 2How to Make Purists Cry1Dynamic Memory Management2•What’s worse?50% of instructions/data are cache misses (fetched from RAM)1% of instructions/data are page faults (fetched from disk)3•50% cache misses.50 + .50*100•1% disk misses.99 + .01 * 100,0004Goals of Today’s Lecture•Dynamic memory managementGarbage collection by the run-time system (Java)Manual deallocation by the programmer (C, C++)•Challenges of manual deallocation Arbitrary request sizes in an arbitrary orderComplex evolution of heap as a program runs•Design decisions for the “K&R” implementationCircular linked-list of free blocks with a “first fit” allocationCoalescing of adjacent blocks to create larger blocks5Memory Layout: Heapchar* string = “hello”;int iSize;char* f(){ char* p; scanf(“%d”, &iSize); p = malloc(iSize); return p;}TextBSSStackHeapNeeded when required memory size is not known before the program runsRoDataData6Allocating & Deallocating Memory•Dynamically allocating memoryProgrammer explicitly requests space in memorySpace is allocated dynamically on the heapE.g., using “malloc” in C, and “new” in Java•Dynamically deallocating memoryMust reclaim or recycle memory that is never used againTo avoid (eventually) running out of memory•“Garbage”Allocated block in heap that will not be accessed again Can be reclaimed for later use by the program7Option #1: Garbage Collection•Run-time system does garbage collection (Java)Automatically determines objects that can’t be accessedAnd then reclaims the resources used by these objectsObject x = new Foo();Object y = new Bar();x = new Quux();if (x.check_something()) { x.do_something(y);}System.exit(0);Object Foo() is never used again!8Challenges of Garbage Collection•Detecting the garbage is not always easy“if (complex_function(y)) x = Quux();”Run-time system cannot collect all of the garbage•Detecting the garbage introduces overheadKeeping track of references to objects (e.g., counter)Scanning through accessible objects to identify garbageSometimes walking through a large amount of memory•Cleaning the garbage leads to bursty delaysE.g., periodic scans of the objects to hunt for garbageLeading to unpredictable “freeze” of the running programVery problematic for real-time applications… though good run-time systems avoid long freezes9Option #2: Manual Deallocation•Programmer deallocates the memory (C and C++)Manually determines which objects can’t be accessedAnd the explicitly returns the resources to the heapE.g., using “free” in C or “delete” in C++•AdvantagesLower overheadNo unexpected “pauses” More efficient use of memory•DisadvantagesMore complex for the programmerSubtle memory-related bugsSecurity vulnerabilities in the (buggy) code10Manual Deallocation Can Lead to Bugs•Dangling pointersProgrammer frees a region of memory … but still has a pointer to itDereferencing pointer reads or writes nonsense valuesint main(void) { char *p; p = malloc(10); … free(p); … putchar(*p);}May print nonsense character.11Manual Deallocation Can Lead to Bugs•Memory leakProgrammer neglects to free unused region of memorySo, the space can never be allocated againEventually may consume all of the available memoryvoid f(void) { char *s; s = malloc(50); return;}int main(void) { while (1) f(); return 0;}Eventually, malloc() returns NULL12Manual Deallocation Can Lead to Bugs•Double freeProgrammer mistakenly frees a region more than onceLeading to corruption of the heap data structure… or premature destruction of a different objectint main(void) { char *p, *q; p = malloc(10); … free(p); q = malloc(10); free(p); …}Might free the space allocated to q!13Challenges for Malloc and Free•Malloc() may ask for an arbitrary number of bytes•Memory may be allocated & freed in different order•Cannot reorder requests to improve performancechar *p1 = malloc(3);char *p2 = malloc(1);char *p3 = malloc(4);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);14Heap: Dynamic Memory #include <stdlib.h>void *malloc(size_t size);void free(void *ptr);00xffffffffStack}HeapHeapchar *p1 = malloc(3);char *p2 = malloc(1);char *p3 = malloc(4);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);p115Heap: Dynamic Memory #include <stdlib.h>void *malloc(size_t size);void free(void *ptr);00xffffffffStack}HeapHeapchar *p1 = malloc(3);char *p2 = malloc(1);char *p3 = malloc(4);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);p1p216Heap: Dynamic Memory #include <stdlib.h>void *malloc(size_t size);void free(void *ptr);00xffffffffStack}HeapHeapchar *p1 = malloc(3);char *p2 = malloc(1);char *p3 = malloc(4);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);p1p2p317Heap: Dynamic Memory #include <stdlib.h>void *malloc(size_t size);void free(void *ptr);00xffffffffStack}HeapHeapchar *p1 = malloc(3);char *p2 = malloc(1);char *p3 = malloc(4);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);p1p2p318Heap: Dynamic Memory #include <stdlib.h>void *malloc(size_t size);void free(void *ptr);00xffffffffStack}HeapHeapchar *p1 = malloc(3);char *p2 = malloc(1);char *p3 = malloc(4);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);p1p2p3p419Heap: Dynamic Memory


View Full Document

Princeton COS 217 - Dynamic Memory Management

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Dynamic Memory Management
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Dynamic Memory Management and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Dynamic Memory Management 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?