Download presentation
Presentation is loading. Please wait.
1
Smart Pointers
2
Dumb Pointers Pointers Necessary Dynamic memory Sharing memory
3
Dumb Pointers Pointers Necessary But they are tricky Dynamic memory
Sharing memory But they are tricky Memory leaks Bad pointers
4
References References : safer pointers C++ : Non-nullable
5
References Java : All objects are on heap Stack has
Numeric Variables References References are nullable Cannot delete memory
6
References Python Everything is a reference… even numbers x = 1 1 x
7
References Python x = 1 x = 2 Everything is a reference… even numbers
8
References Python x = 1 x = 2 y = x
Everything is a reference… even numbers x = 1 x = 2 y = x 1 x 2 y
9
References Python x = 1 x = 2 y = x x = 4
Everything is a reference… even numbers x = 1 x = 2 y = x x = 4 1 x 2 y 4
10
References Python x = 1 x = 2 y = x x = 4 x = x + 1
Everything is a reference… even numbers x = 1 x = 2 y = x x = 4 x = x + 1 1 x 2 y 4 5
11
Garbage Collection Garbage collection:
Automatically reclaim unused memory from heap Various strategies Reference counting Mark and Sweep
12
Reference Counting Store count of references to each heap allocation
0 references = garbage Once removed, things they point to may become 0 count
13
Reference Counting Problem: Cycles are not identified
14
Mark & Sweep Mark all heap allocations as dead
Start from stack based variables Traverse pointers and mark hit allocations live Delete dead memory
15
Compaction Mark & Sweep may leave fragmented memory:
16
Compaction Mark & Sweep may leave fragmented memory:
Allocations must be contiguous Allocation might fail even with enough space
17
Compaction Compaction : copy allocations so they are packed tightly:
18
Compaction Compaction : copy allocations so they are packed tightly:
Expensive Need to “freeze” user code while addresses updated
19
Time Memory tends to be short lived or long lived:
20
Time Advanced GC uses different pools for:
Young allocations – checked frequently Old allocatins – checked less frequently
21
Garbage Collection Pros Cons No need to worry about deleting memory
System decides when to schedule cleanup work Less predictable lifespan for objects
22
Smart Pointers C++11 brought smart pointers
Pointers do reference counting Memory released when last reference is released
23
Shared Ptr shared_ptr : counts number sharing the object
When shared_ptr leaves scope, count-- Memory deleted when count == 0
24
Weak Ptr weak_ptr : keeps track of object without "counting"
Can not be used directly Use to ask for a real shared ptr when needed Used to avoid cycles in shared_ptr
25
Unique Ptr unique_ptr : unshared pointer Can't copy can only move
Deletes memory when pointer leaves scope
26
Courses With Strong/Weak
Course Manager responsible for loading from DB
27
Courses With Strong/Weak
Student asks for courses to be loaded Course manager creates objects, stores weak_ptr AND…
28
Courses With Strong/Weak
Student asks for courses to be loaded Course manager creates objects, stores weak_ptr AND… Student given shared_ptrs refCount = 1 refCount = 1
29
Courses With Strong/Weak
Student2 is keeping the Courses in memory courseManager knows where they are, can access if needed refCount = 1 refCount = 1
30
Courses With Strong/Weak
New student asks for a new class Same process refCount = 1 refCount = 1 refCount = 1
31
Courses With Strong/Weak
New student asks for an existing class CS162 courseManager checks its array, realizes it exists, gives Student1 a shared_ptr CS162 now has refcount of 2 refCount = 2 refCount = 1 refCount = 1
32
Courses With Strong/Weak
Student2 destroyed… decrement refcount for CS271 and CS162 refCount = 1 refCount = 1 refCount = 0
33
Courses With Strong/Weak
Student2 destroyed… decrement refcount for CS271 and CS162 shared_ptrs with refcount == 0 automatically deleted refCount = 1 refCount = 1
34
Courses With Strong/Weak
courseManager has no way of knowing CS271 is deleted… Must check status of weak_ptr before using refCount = 1 refCount = 1
35
Objective C Can pick: Manual release
Garbage collection (some platforms) ARC : Automatic Reference Counting Like shared_ptrs and weak_ptrs
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.