SlideShare a Scribd company logo
1 of 18
Garbage Collection
Garbage Collection 主要分為 參考計數式(reference-counting) 追蹤式(tracing) 追蹤式演算法又延伸出多種變形或綜合型式演算法如: 標記-清除法(mark-sweep) 複製法(copying) 標記-壓縮法(mark-compact) 搭配以世代法(generation)之概念多種型式演算法。
參考計數式(reference-counting) George Collins在1960年為了Lisp所提出的演算法。 每個物件都有一個欄位,來存放參考計數值。如果有另一個物件參考到,則加一,取消參考時,減一。變成零的時候,代表沒有其它物件參考到了,則成為垃圾,進行回收。 1 1 0 1 1 0 1
參考計數式(reference-counting) 優點: 簡單實做,最簡單的只要對每個物件加入一個參考計數欄位。並實做更新機制。 立即回收,當參考計數為零。 具有區域性,更新物件只影響到一個物件,而處理環狀結構時也針對可能的物件做處理,不會掃描全部記憶體。 即時性,分散負擔,當參考計數為零立即回收,大部分在執行時便會被回收。 缺點: 需要參考計數欄位,比追蹤式浪費空間。 分散在執行時間,即使記憶體還足夠,也會浪費CPU時間做回收。 無法直接處理環狀垃圾,需其它演算法來解決。
標記-清除法(mark-sweep) 1 0 1 1960年時油McCarthy提出且真正應用於Lisp的演算法,也是目前最廣被採用的演算法。 在記憶體不足時執行,會掃描整個記憶體兩次,第一次掃描時會判斷記憶體中的每一個物件是否仍在使用,如果仍在使用則做一個標記,否則就不做標記,第二次掃描清除沒有做標記的物件。所以只需要額外一個位元記錄。 0 0 1 0 0 1
標記-清除法(mark-sweep) 優點: 額外空間小。 沒有環狀問題。 必要時才回收。 缺點: 記憶體越大掃描越久。 暫停應用程式較久。
複製法(copying) 0 1 1 1963年Minsky提出。 將記憶體分成兩個區域,配置物件時全配置在同一個區域,記憶體不足時,掃描該區域,活著的物件移到另一個區域,搬完後剩下垃圾物件,直接回收整個區域,再將兩個區域角色互換。 0 1 0 1 1 0 0 1
複製法(copying) 優點: 配置簡單,固定回收整個區域。 沒有環狀問題。 必要時才回收。 缺點: 一次只能使用一半記憶體。  記憶體越大,掃描及搬移越久。 暫停應用程式較久。
標記-壓縮法(mark-compact) 0 1 此演算法結合了“標記-清除”和“複製”兩個演算法的優點。也是分兩階段,第一階段從根節點開始標記所有被引用物件,第二階段遍歷整個heap,把清除未標記物件並且把存活物件“壓縮”到heap的其中一塊,按順序排放。此演算法避免了“標記-清除”的碎片問題,同時也避免了“複製”演算法的空間問題。 0 1 0 1 1 0 0 1 1
世代法(generation) 最早由Henry Lieberman與Carl Hewitt在1983年提出,他不是一個獨立的演算法,需搭配參考計數或追蹤法使用。 主要概念是記憶體中存活越久的物件,越不可能是垃圾,它給每個物件一個欄位來代表物件年齡。據Lins提出的世代環狀參考計數法提到,有80~98%的物件會在年輕的時候死亡,所以越年輕越容易死亡。
世代法(generation) 演算法可以根據這個結果調整執行的時間和範圍,根據物件的年齡執行不同演算法。 例如物件複製法不會搬移死亡的物件,如果執行實大部份的物件都死亡,它的搬移量就少,執行速度因此提升,所以對年輕的物件有很好的效率。
參考計數式v.s.追蹤式 參考計數式執行時間可分為兩部分: 應用程式執行時,物件的指標改變時,可回收大部分垃圾,但無法回收環狀垃圾。 記憶體不足時,針對環狀垃圾做處理。 追蹤式統一在記憶體不足時,平時不會帶給應用程式負擔,但需做較長時間的暫停。
環狀偵測演算法 Martinez區域標記-掃描演算法。(三色追蹤演算法(Tri-Color)) Lins延遲追蹤演算法。 Bacon演算法。
Martinez區域標記-掃描演算法 Martinez在1990年提出,需要對圖形做三次追蹤與著色,所以也稱三色追蹤演算法(Tri-Color)。 每當物件的參考計數值減少且不為零時執行,以該物件為根節點,對其子節點進行追蹤,過程中會用三種顏色來記錄物件的狀態,追蹤又分三個階段。 第一階段為標記(MarkGrey),會每個子節點的參考計數值減一,並作標記。 第二階段會掃描每個點(Scan),如果有標記過就標記成垃圾,如果不為零就取消標記。並對其子節點參考計數值加一。 第三階段回收第二階段標記為垃圾的節點。(CollectWhite)
Lins延遲追蹤演算法 由Martinez區域標記-掃描演算法衍生而來。由於區域標記-掃描法每找到可能為環狀結構的根節點,便會進行掃描,這樣會造成同樣的子圖會被重複掃描好幾次。相當沒有效率。 Lins延遲追蹤演算法,就可能為環狀結構的根節點儲存到緩衝區中,等到一定量時才進行掃描。
Bacon演算法 Bacon在2001年提出,延伸自Martinez區域標記-掃描演算法及Lins延遲追蹤演算法。 Lins延遲追蹤演算法存在二次問題(同一個物件被多次重覆處理)如圖2-1,以A為根節點掃描時,會掃描A、B、C,以D為根節點掃描時,會掃描D、E、F、C、A、B,以G為根節點掃描時會掃描全部節點,G、H、I、D、E、F、C、A、B,其中A、B、C被掃描3次, D、E、F被掃描2次,複雜度為O(n^2)。
Bacon演算法 Bacon演算法會一次處理整個圖,而不是一個子圖一個子圖處理,所以不會有二次問題。
參考資料 國立成功大學工程科學學系碩士論文-高效率環狀偵測器之設計與實作:應用於參考記數系統(研究生:侯曄暘)

More Related Content

Featured

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Featured (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

Garbage collection