SlideShare a Scribd company logo
1 of 45
Download to read offline
Garbage collection 介紹
高國棟
演講經歷
● 2013/04 在 taipei.py 演講關於 pdb 的實作。相關投影片:
http://www.slideshare.net/ya790026/recoverpdb
● 2013/05 在 pyconf.tw 演將 CPython 原始碼解析。相關投
影片:http://www.slideshare.net/ya790026/c-python23247730。
● 2013/08 在taipei.py 演講 python 如何執行程式碼。相關
投影片:http://www.slideshare.net/ya790026/python27854881
Garbage Collection
● memory leak, dangling pointer

● Reference count
● Mark and sweep
memory leak

dangling pointer

memory

memory

memory

memory

free
Reference Counting
● Reference count is maintained for each
object on the heap.
● When an object is first created and a
reference to it is assigned to a variable, the
object's reference count is set to one.
Reference Counting
● When any other variable is assigned a
reference to that object, the object's count is
incremented.
● When a reference to an object goes out of
scope or is assigned a new value, the
object's count is decremented.
a = 5000
a

a = 5000
b=a
a

b

a = 5000
b=a
a = 3000

b

a

ob_ival: 5000
ob_refcnt: 1

ob_ival: 5000
ob_refcnt: 1

ob_ival: 5000
ob_refcnt: 2

ob_ival: 3000
ob_refcnt: 1
a = 5000
b=a
a = 3000
b = 4000

b
ob_ival: 5000
ob_refcnt: 0

a
ob_ival: 4000
ob_refcnt: 1

ob_ival: 3000
ob_refcnt: 1
Reference Counting
Advantage:
suitable for real-time environments where
the program can't be interrupted for very long.
Disadvantage:
reference counting does not detect cycles.
a = []
b = []
a.append(b)
b.append(a)
a

b

a = []
b = []
a.append(b)
b.append(a)
a = None
b = None
mark and sweep
1. Find the root objects of the system. These
are things like the global environment (like
the __main__ module in Python) and objects
on the stack.
2. Search from these objects and find all
objects reachable from them. This objects
are all "alive".
3. Free all other objects.
Two-Color Mark & Sweep
Sweep
Free

Sweep

White

Black

New

Mark
Two-Color Mark & Sweep
● the algorithm is non-incremental (atomic
collection)
Tri-Color Incremental Mark & Sweep
● Initially grey set is all the objects that are reachable from
root references but the objects referenced by grey
objects haven't been scanned yet.
● The white setis the set of objects that are candidates for
having their memory recycled.
● The black set is the set of objects that can cheaply be
proven to have no references to objects in the white set.
Free

Sweep

Black

Mark

Sweep
After
Check

White

Barrier
backward

Mark
New

Gray

Barrier
Forward

Mark
Tri-Color Incremental Mark & Sweep
● When there are no more objects in the grey
set, then all the objects remaining in the
white set have been demonstrated not to be
reachable, and the storage occupied by
them can be reclaimed.
Generational Collectors
1. Most objects created by most programs
have very short lives.
2. Most programs create some objects that
have very long lifetimes. A major source of
inefficiency in simple copying collectors is
that they spend much of their time copying
the same long-lived objects again and again.
External Memory fragment
● Free memory is separated into small blocks
and is interspersed by allocated memory.
● Although free storage is available, it is
unusable because it is divided into pieces
that are too small individually to satisfy the
demands of the application.
External Memory fragment
a

del b
del d

b

c

d

a

c

d

a

c

We can’t create a variable with four
blocks.
Compacting and copying
● Move objects on the fly to reduce heap
fragmentation
a

a
table of
object
handles

Object

b

b

Object
stop and copy
● The heap is divided into two regions.
● Only one of the two regions is used at any time.
● Objects are allocated from one of the regions until all
the space in that region has been exhausted.
● Find out live objects and copy them to the other region.
● Memory will be allocated from the new heap region until
it too runs out of space
free

allocated

unused

allocated

unused

unused

Copy
live
objects

free

allocated
Python garbage collection
● Python use both of reference count and
“mark and sweep”.
● “mark and sweep” only work for containers
for solving reference cycles.
● Containers mean list, dict, instance, etc.
● python 的 mark and sweep和傳統方法不一
樣,因為 c extentsion 的存在,因此很難有共
同的 root object。
Python mark and sweep
1. For each container object, set gc_refs equal
to the object's reference count.
2. For each container object, find which
container objects it references and decrement
the referenced container's gc_refs field.
Python mark and sweep
3. All container objects that now have a gc_refs field
greater than one are referenced from outside the set of
container objects. We cannot free these objects so we
move them to a different set.
4. Any objects referenced from the objects moved also
cannot be freed. We move them and all the objects
reachable from them too.
Python mark and sweep
5. Objects left in our original set are referenced
only by objects within that set (ie. they are
inaccessible from Python and are garbage). We
can now go about freeing these objects.
1
gc_refs: 1

gc_refs: 1

2
gc_refs: 1

gc_refs: 0

3
gc_refs: 1

gc_refs: 0

GC_TENTATIVE
LY_UNREACHAB
LE
4
gc_refs: 1

gc_refs: 1
1
gc_refs: 1

gc_refs: 1

2
gc_refs: 0

gc_refs: 0

3
gc_refs: 0

gc_refs: 0

GC_TENTATIVE
LY_UNREACHAB
LE
4
gc_refs: 0

gc_refs: 0
Java Reference
Strong reference
SoftReference
WeakReference
PhantomReference
Soft Reference
● The garbage collector may reclaim the
memory occupied by a softly reachable
object.
● It’s useful for cache.
Weak Reference
● The garbage collector must reclaim the
memory occupied by a weakly reachable
object.
● Canonicalizing mappings
Phantom Reference
● Similar with weak reference
● Whereas the garbage collector enqueues
soft and weak reference objects when their
referents are leaving the relevant
reachability state, it enqueues phantom
references when the referents are entering
the relevant state.
● Establish more flexible pre-mortem cleanup
Python Reference
Strong reference
Weak reference

weakref.ref(object[, callback])
Python gc 介面
gc.enable()
gc.disable()
c.isenabled()
gc.collect([generation])
gc.set_threshold(threshold0[, threshold1[, threshold2]])
gc.get_count()
gc.get_threshold()
Python gc 介面
gc.set_debug(flags)
gc.get_referrers(*objs)
gc.get_referents(*objs)
gc.garbage
In [1]: import gc
In [2]: gc.set_debug(gc.DEBUG_STATS)
In [3]: gc.collect()
gc: collecting generation 2...
gc: objects in each generation: 159 2655 7538
gc: done, 10 unreachable, 0 uncollectable, 0.0123s
elapsed.
>>>
...
...
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>

class Finalizable:
def __del__(self): pass
a = Finalizable()
b = Finalizable()
a.x = b
b.x = a
del a
del b
import gc
gc.collect()
● memory-bound
○

可以考慮調低 threshold 用時間換取空間

● cpu-bound
○ 可以考慮調高 threshold 用空間換取時間
○ 但是不可以調太高 以免每次 gc 時間過久
○ 在部分要求低延遲的程式碼 可以暫時停用 gc
結論
● python 的 gc 演算法很有趣
● python 的記憶體管理機制,能夠減少記憶體破
碎的情形發生。但是 gc 無法解決
ExternalMemory fragment 的問題
● python 的 gc 是 atomic
參考資料
●
●
●
●
●

New-Garbage-Collector for lua
Garbage Collection
gc module docs
Details on Garbage Collection for Python
python source code(Modules/gcmodule.c)
PyConf 場務徵人
Thank you

More Related Content

What's hot

How my visualization tools use little memory: A tale of incrementalization an...
How my visualization tools use little memory: A tale of incrementalization an...How my visualization tools use little memory: A tale of incrementalization an...
How my visualization tools use little memory: A tale of incrementalization an...
Eugene Kirpichov
 
OpenDiscovery
OpenDiscoveryOpenDiscovery
OpenDiscovery
gwprice
 
Los Angeles R users group - July 12 2011 - Part 2
Los Angeles R users group - July 12 2011 - Part 2Los Angeles R users group - July 12 2011 - Part 2
Los Angeles R users group - July 12 2011 - Part 2
rusersla
 

What's hot (13)

Understanding JVM GC: advanced!
Understanding JVM GC: advanced!Understanding JVM GC: advanced!
Understanding JVM GC: advanced!
 
Python Coding Examples for Drive Time Analysis
Python Coding Examples for Drive Time AnalysisPython Coding Examples for Drive Time Analysis
Python Coding Examples for Drive Time Analysis
 
Understanding low latency jvm gcs V2
Understanding low latency jvm gcs V2Understanding low latency jvm gcs V2
Understanding low latency jvm gcs V2
 
Understanding jvm gc advanced
Understanding jvm gc advancedUnderstanding jvm gc advanced
Understanding jvm gc advanced
 
How my visualization tools use little memory: A tale of incrementalization an...
How my visualization tools use little memory: A tale of incrementalization an...How my visualization tools use little memory: A tale of incrementalization an...
How my visualization tools use little memory: A tale of incrementalization an...
 
Object Detection with Tensorflow
Object Detection with TensorflowObject Detection with Tensorflow
Object Detection with Tensorflow
 
Understanding low latency jvm gcs
Understanding low latency jvm gcsUnderstanding low latency jvm gcs
Understanding low latency jvm gcs
 
DotNetFest - Let’s refresh our memory! Memory management in .NET
DotNetFest - Let’s refresh our memory! Memory management in .NETDotNetFest - Let’s refresh our memory! Memory management in .NET
DotNetFest - Let’s refresh our memory! Memory management in .NET
 
3 Презентация Kotlin - why not?
3 Презентация Kotlin - why not?3 Презентация Kotlin - why not?
3 Презентация Kotlin - why not?
 
Go and the garbage collection
Go and the garbage collectionGo and the garbage collection
Go and the garbage collection
 
OpenDiscovery
OpenDiscoveryOpenDiscovery
OpenDiscovery
 
Los Angeles R users group - July 12 2011 - Part 2
Los Angeles R users group - July 12 2011 - Part 2Los Angeles R users group - July 12 2011 - Part 2
Los Angeles R users group - July 12 2011 - Part 2
 
Reddirt2011
Reddirt2011Reddirt2011
Reddirt2011
 

Similar to Garbage collection 介紹

Similar to Garbage collection 介紹 (20)

Memory Management In Python The Basics
Memory Management In Python The BasicsMemory Management In Python The Basics
Memory Management In Python The Basics
 
Garbage collector in python
Garbage collector in pythonGarbage collector in python
Garbage collector in python
 
Memory Management & Garbage Collection
Memory Management & Garbage CollectionMemory Management & Garbage Collection
Memory Management & Garbage Collection
 
Java GC
Java GCJava GC
Java GC
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
Garbage Collection .Net
Garbage Collection .NetGarbage Collection .Net
Garbage Collection .Net
 
Python: Thanks for the memories
Python: Thanks for the memoriesPython: Thanks for the memories
Python: Thanks for the memories
 
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4
 
Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)Chronicles Of Garbage Collection (GC)
Chronicles Of Garbage Collection (GC)
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
 
Building reusable components with generics and protocols
Building reusable components with generics and protocolsBuilding reusable components with generics and protocols
Building reusable components with generics and protocols
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
 
I didn't know you could do that with groovy
I didn't know you could do that with groovyI didn't know you could do that with groovy
I didn't know you could do that with groovy
 
garbage collector
garbage collectorgarbage collector
garbage collector
 
Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)
 
Singularity Registry HPC
Singularity Registry HPCSingularity Registry HPC
Singularity Registry HPC
 
A tour on Spur for non-VM experts
A tour on Spur for non-VM expertsA tour on Spur for non-VM experts
A tour on Spur for non-VM experts
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
 
Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)
Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)
Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)
 
Talking trash
Talking trashTalking trash
Talking trash
 

More from kao kuo-tung

那些年,我們一起看的例外
那些年,我們一起看的例外那些年,我們一起看的例外
那些年,我們一起看的例外
kao kuo-tung
 

More from kao kuo-tung (16)

用 Open source 改造鍵盤
用 Open source 改造鍵盤用 Open source 改造鍵盤
用 Open source 改造鍵盤
 
Immutable infrastructure 介紹與實做:以 kolla 為例
Immutable infrastructure 介紹與實做:以 kolla 為例Immutable infrastructure 介紹與實做:以 kolla 為例
Immutable infrastructure 介紹與實做:以 kolla 為例
 
Python to scala
Python to scalaPython to scala
Python to scala
 
Intorduce to Ceph
Intorduce to CephIntorduce to Ceph
Intorduce to Ceph
 
Openstack swift, how does it work?
Openstack swift, how does it work?Openstack swift, how does it work?
Openstack swift, how does it work?
 
Why is a[1] fast than a.get(1)
Why is a[1]  fast than a.get(1)Why is a[1]  fast than a.get(1)
Why is a[1] fast than a.get(1)
 
減少重複的測試程式碼的一些方法
減少重複的測試程式碼的一些方法減少重複的測試程式碼的一些方法
減少重複的測試程式碼的一些方法
 
Openstack taskflow 簡介
Openstack taskflow 簡介Openstack taskflow 簡介
Openstack taskflow 簡介
 
Async: ways to store state
Async:  ways to store stateAsync:  ways to store state
Async: ways to store state
 
Openstack 簡介
Openstack 簡介Openstack 簡介
Openstack 簡介
 
Docker 原理與實作
Docker 原理與實作Docker 原理與實作
Docker 原理與實作
 
那些年,我們一起看的例外
那些年,我們一起看的例外那些年,我們一起看的例外
那些年,我們一起看的例外
 
Python 中 += 與 join比較
Python 中 += 與 join比較Python 中 += 與 join比較
Python 中 += 與 join比較
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行
 
C python 原始碼解析 投影片
C python 原始碼解析 投影片C python 原始碼解析 投影片
C python 原始碼解析 投影片
 
recover_pdb 原理與介紹
recover_pdb 原理與介紹recover_pdb 原理與介紹
recover_pdb 原理與介紹
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Garbage collection 介紹