
Memory Attributes CMSC 330: Organization of Memory to store data in programming Programming Languages languages has the following lifecycle • Allocation ! When the memory is allocated to the program • Lifetime ! How long allocated memory is used by the program Memory Management and • Recovery Garbage Collection ! When the system recovers the memory for reuse The allocator is the system feature that performs allocation and recovery CMSC 330 1 CMSC 330 2 Memory Attributes (cont.) Memory Classes Most programming languages are concerned Static memory – Usually at a fixed address with some subset of the following 4 memory • Lifetime – The execution of program classes • Allocation – For entire execution 1. Static (or fixed) memory • Recovery – By system when program terminates 2. Automatic memory • Allocator – Compiler 3. Dynamically allocated memory Automatic memory – Usually on a stack 4. Persistent memory • Lifetime – Activation of method using that data • Allocation – When method is invoked • Recovery – When method terminates • Allocator – Typically compiler, sometimes programmer CMSC 330 3 CMSC 330 4 Memory Classes (cont.) Memory Classes (cont.) Dynamic memory – Addresses allocated on Persistent memory – Usually the file system demand in an area called the heap • Lifetime – Multiple executions of a program • Lifetime – As long as memory is needed ! E.g., files or databases • Allocation – Explicitly by programmer, or implicitly by • Allocation – By program or user compiler ! Often outside of program execution • Recovery – Either by programmer or automatically • Recovery – When data no longer needed (when possible and depends upon language) • Dealing with persistent memory → databases • Allocator – Manages free/available space in heap ! CMSC 424 CMSC 330 5 CMSC 330 6 Memory Management in C Memory Management Errors Local variables live on the stack May forget to free memory (memory leak) • Allocated at function invocation time { int *x = (int *) malloc(sizeof(int)); } • Deallocated when function returns May use freed memory (dangling pointer) • Storage space reused after function returns { int *x = ...malloc(); Has security free(x); Implications! *x = 5; /* oops! */ Space on the heap allocated with malloc() } • Must be explicitly freed with free() May try to free something twice • Called explicit or manual memory management { int *x = ...malloc(); free(x); free(x); } ! Deletions must be done by the user ! This may corrupt the memory management data structures • E.g., the memory allocator maintains a free list of space on the heap that’s available CMSC 330 7 CMSC 330 8 Ways to Avoid Mistakes in C Fragmentation Don’t allocate memory on the heap Another memory management problem • Could lead to confusing code; or may not work at all Example sequence of calls Never free memory allocate(a); • OS will reclaim process’s memory anyway at exit allocate(x); • Memory is cheap; who cares about a little leak? allocate(y); free(a); But: Both of the above two may be impractical allocate(z); free(y); Can avoid all three problems by using automatic memory management allocate(b); • Though it does not prevent all leaks, as we will see ⇒ Not enough contiguous space for b CMSC 330 9 CMSC 330 10 Automatic memory management Strategy Primary goal: automatically reclaim dynamic memory • Secondary goal: also avoid fragmentation At any point during execution, can divide the objects in the heap into two classes • Live objects will be used later • Dead objects will never be used again ! They are garbage Insight: You can do reclamation and avoid fragmentation if you can identify every pointer in a program Thus we need garbage collection (GC) • You can move the allocated storage, then redirect pointers to it algorithms that can ! Compact it, to avoid fragmentation • Compiler ensures perfect knowledge LISP, OCAML, Java, Prolog 1. Distinguish live from dead objects (with caveats), but not in C, C++, Pascal, Ada 2. Reclaim the dead objects and retain the live ones CMSC 330 11 CMSC 330 12 Determining Liveness Liveness by Reachability In most languages we can’t know for sure which An object is reachable if it can be accessed by objects are really live or dead dereferencing (“chasing”) pointers from live data • Undecidable, like solving the halting problem Safe policy: delete unreachable objects Thus we need to make a safe approximation • An unreachable object can never be accessed again • OK if we decide something is live when it’s not by the program • But we’d better not deallocate an object that will be ! The object is definitely garbage used later on • A reachable object may be accessed in the future ! The object could be garbage but will be retained anyway ! Could lead to memory leaks CMSC 330 13 CMSC 330 14 Roots Reference Counting At a given program point, we define liveness as Idea: Each object has count of number of being data reachable from the root set pointers to it from the roots or other objects • Global variables • When count reaches 0, object is unreachable ! What are these in Java? Ruby? OCaml? Count tracking code may be manual or automatic • Local variables of all live method activations In regular use ! I.e., the stack • C++ (smart pointer library), Cocoa (manual), Python At the machine level Method doesn’t address fragmentation problem • Also consider the register set ! Usually stores local or global variables Invented by Collins in 1960 Next • “A method for overlapping and erasure of lists.” • Techniques for determining reachability Communications of the ACM, December 1960 CMSC 330 15 CMSC 330 16 Reference Counting Example Reference Counting Example (cont.) stack stack 1 2 1 2 1 1 1 1 CMSC 330 17 CMSC 330 18 Reference Counting Example (cont.) Reference Counting Example (cont.) stack stack 1 2 1 1 2 1 1 1 0 1 1 CMSC 330 19 CMSC 330 20 Reference Counting Example (cont.) Reference Counting Example (cont.) stack stack 1 2 1 1 2 1 0 1 1 CMSC 330 21 CMSC 330 22 Reference Counting Example (cont.) Reference Counting Tradeoffs stack Advantage • Incremental technique ! Generally small, constant amount of work per memory write ! With more effort, can even bound running time Disadvantages • Cascading decrements can be expensive 1 • Requires extra storage for reference counts • Need other means to collect cycles, for which counts never go to 0 CMSC 330 23 CMSC 330 24 Tracing Garbage Collection Mark and Sweep GC Idea: Determine reachability as needed, rather Two phases than by stored counts • Mark phase: trace the heap and mark all reachable objects Every so often, stop the world and • Follow pointers from live objects (starting at roots) to • Sweep phase: go through the entire heap and expand the live object set reclaim all unmarked objects ! Repeat until no more reachable objects • Deallocate any non-reachable objects Two main variants of tracing GC • Mark/sweep (McCarthy 1960) and stop-and-copy (Cheney 1970) CMSC 330 25 CMSC 330 26 Mark and Sweep Example Mark and Sweep Example (cont.) stack stack CMSC 330 27 CMSC 330 28 Mark and Sweep Example (cont.) Mark and Sweep Example (cont.) stack stack CMSC 330 29 CMSC 330 30 Mark and Sweep Example (cont.) Mark and Sweep Example (cont.) stack stack CMSC 330 31 CMSC 330 32 Mark and Sweep Example (cont.) Mark and Sweep Advantages stack No problem with cycles Memory writes have no cost Non-moving • Live objects stay where they are • Makes conservative GC possible ! Used when identification of pointer vs. non-pointer uncertain ! More later CMSC 330 33 CMSC 330 34 Mark and Sweep Disadvantages Copying GC Fragmentation Like mark and sweep, but only touches live • Available space broken up into many small pieces objects ! Thus many mark-and-sweep systems may also have a • Divide heap into two equal parts (semispaces) compaction phase (like defragmenting your disk) • Only one semispace active at a time Cost proportional to heap size • At GC time, copy live data, and flip semispaces • Sweep phase needs to traverse whole heap – it 1. Trace the live data starting from the roots touches dead memory to put it back on to the free list 2. Copy live data into other semispace 3. Declare everything in current semispace dead 4. Flip to other semispace CMSC 330 35 CMSC 330 36 Copying GC Example Copying GC Example (cont.) stack stack ① ① CMSC 330 37 CMSC 330 38 Copying GC Example (cont.) Copying GC Example (cont.) stack stack ① ① ③ ② ② ③ ② ② ① ① CMSC 330 39 CMSC 330 40 Copying GC Tradeoffs Stop the World: Potentially Long Pause Advantages Both of the previous algorithms “stop the world” • Only touches live data by prohibiting program execution during GC • No fragmentation (automatically compacts) • Ensures that previously processed memory is not ! Will probably increase locality changed or accessed, creating inconsistency Disadvantages But the execution pause could be too long • Requires (at most) twice the memory space • Bad if your car’s braking system performs GC while you are trying to stop at a busy intersection! How can we reduce the pause time of GC? • Don’t collect the whole heap at once (incremental) CMSC 330 41 CMSC 330 42 The Generational Principle Generational Collection Long lived objects visited multiple times • Idea: Have more than one heap region, divide into Young ⇒ objects generations die quickly; ! Older generations collected less often old objects ! Objects that survive many collections get promoted into keep living older generations ! Need to track pointers from old to young generations to use as roots for young generation collection • Tracking one in the remembered set More objects live live objects More One popular setup: Generational, copying GC Object lifetime increases ⇒ CMSC 330 43 CMSC 330 44 Conservative Garbage Collection (for C) Memory Management in Ruby For C, we cannot be sure which elements of an Local variables live on the stack object are pointers • Storage reclaimed when method returns • Because of incomplete type information, the use of unsafe casts, etc.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages14 Page
-
File Size-