330 likes | 339 Views
Learn about the importance of pointers and dynamic memory in memory management and how smart pointers can eliminate the need for manual memory deletion. Explore concepts such as references, garbage collection, and the use of smart pointers in different programming languages like C++, Java, Python, and Swift.
E N D
Dumb Pointers • Pointers Necessary • Dynamic memory • Sharing memory
Dumb Pointers • Pointers Necessary • Dynamic memory • Sharing memory • But they are tricky • Memory leaks • Bad pointers
References • References : safer pointers • C++ : • Non-nullable
References • Java : • All objects are on heap • Stack has • Numeric Variables • References • References are nullable • Cannot delete memory
References • Python • Everything is a reference… even numbers x = 1
References • Python • Everything is a reference… even numbers x = 1x = 2
References • Python • Everything is a reference… even numbers x = 1x = 2y = x
References • Python • Everything is a reference… even numbers x = 1x = 2y = xx = 4
References • Python • Everything is a reference… even numbers x = 1x = 2y = xx = 4x = x + 1
Garbage Collection • Garbage collection: • Automatically reclaim unused memory from heap • Various strategies • Reference counting • Mark and Sweep
Reference Counting • Store count of references to each heap allocation • 0 references = garbage • Once removed, things they point to may become 0 count
Reference Counting • Problem: • Cycles are not identified
Mark & Sweep • Mark all heap allocations as dead • Start from stack based variables • Traverse pointers and mark hit allocations live • Delete dead memory
Compaction • Mark & Sweep may leave fragmented memory:
Compaction • Mark & Sweep may leave fragmented memory: • Allocations must be contiguous • Allocation might fail even with enough space
Compaction • Compaction : copy allocations so they are packed tightly:
Compaction • Compaction : copy allocations so they are packed tightly: • Expensive • Need to “freeze” user code while addresses updated
Time • Memory tends to be short lived or long lived:
Time • Advanced GC uses different pools for: • Young allocations – checked frequently • Old allocatins – checked less frequently
Garbage Collection • Pros • No need to worry about deleting memory • Cons • System decides when to schedule cleanup work • Less predictable lifespan for objects
Smart Pointers • C++11 brought smart pointers • Pointers do reference counting • Memory released when last reference is released
Shared Ptr • shared_ptr : counts number sharing the object • When shared_ptr leaves scope, count-- • Memory deleted when count == 0
Issue • An object can be garbage without having a ref count of 0 • Circular references
Weak Ptr • weak_ptr : Pointer that will not keep object alive • Can not be used directly • Use to ask for a real shared ptr when needed • Used to avoid cycles of shared_ptr
Demo • Students trackclasses withshared ptr • Classes trackstudents withweak ptr
Demo • Student1destroyed…
Demo • Student1destroyed… • Course 1 refcount now 0.It is destroyed.
Demo • Student2destroyed…
Demo • Student2destroyed… • Both classesstill have refcount of 1 • Class 2 needsto check weakpointer to learnstudent2 is gone
Unique Ptr • unique_ptr : unshared pointer • Can't copy can only move • Deletes memory when pointer leaves scope
Smart Pointers • Smart pointers • Avoid manually deciding when to delete • Give us a language to think about relative lifespan • Shared : target will outlive this object • Weak : target may not outlive this object • Unique : no one else should have this - same lifespan • Still require careful reasoning
Swift • Can pick: • Manual release • Garbage collection • ARC : Automatic Reference Counting • Like shared_ptrs and weak_ptrs