SlideShare a Scribd company logo
1 of 31
Download to read offline
わだあつし @wats
Ruby 2.0 の
Bitmap Marking GC
って美味しいの?
Tuesday, December 4, 2012
自己紹介
• わだあつし @wats
• 株式会社 寿限無やってます
• fukuoka.rb とか coder dojo とか fukuoka.py
とか明星和楽とか
• 来年からスクールをはじめます!
Tuesday, December 4, 2012
• Garbage Collection ( =ゴミ 収集)
• メモリの自動開放機構
GCのおさらい
Tuesday, December 4, 2012
• 不要になったオブジェクト = メモリ領域
• どうやって判定するのか?
ゴミとは?
Tuesday, December 4, 2012
• シンプルなMark & Sweep 方式
• 必要なオブジェクトをマークして、マークされなかっ
たものをゴミ認定
• JavaのGenerational GCのような生存期間に合わせた再
配置などもない。
• そもそもコンパクションの機構もないみたい!
今までの Ruby GC
Tuesday, December 4, 2012
• Ruby1.9まではマークされたことを示すフラグは各オブジェク
ト構造体の中にある。
• GCを行うと生存するオブジェクトに変更が入るということ
• マークする = write している。
マーク
( = オブジェクトへの変更)
Tuesday, December 4, 2012
• fork()というシステムコールを内部で使っ
ているのがほとんどらしい
• fork() : 子プロセスを作る
唐突にRack アプリケーション
の一般的な構造
Tuesday, December 4, 2012
• 生成当初は親プロセスのメモリをそのまま参照する。
子プロセス生成の動き1
(OSによるかも。)
Tuesday, December 4, 2012
• 子プロセスからそこへの書き込み = write が発生して初め
てプロセス毎の差異がでる。
子プロセス生成の動き
(OSによるかも。)
Tuesday, December 4, 2012
• その時その部分のメモリ領域を子プロセスが複製 =
copyし、そこに書き込む = write する。
• これがいわゆる copy on write
• だから、親プロセスからはメモリは元の状態のまま
子プロセス生成の動き
(OSによるかも。)
Tuesday, December 4, 2012
write があるとcopy
Tuesday, December 4, 2012
GCが走ると生存しているオ
ブジェクトにマークのため
writeされる。
Tuesday, December 4, 2012
オブジェクトに実質的に差異が発生し
ていないのにプロセスごとに全てコ
ピーされる
Tuesday, December 4, 2012
これがサーバサイドRubyのメ
モリ消費を増大させている。
Tuesday, December 4, 2012
• writeされているのは、生存していること
を示すマーク用の1bitだけ
• なら、オブジェクトはそのままプロセス
間で共有させておきたい。
• マーク用1bitだけを別領域にだしちゃお
う
これを何とかしたい!
Tuesday, December 4, 2012
• GCのマーク用1bitだけを、オブジェクト
から別領域に抜き出し
• GC時のwriteがその別領域だけになるの
で、オブジェクトは共有のまま
• 最低限のオブジェクトのコピーだけが発
生するようになる。
それが Bitmap Marking GC
Tuesday, December 4, 2012
• Unicorn や Passenger使っているところで
はメモリ消費が下がるはず。
• GCの際のマークに1step増えるので、GC
の時間は微増らしい
• 子プロセス作らんようなところ(ただの
rubyスクリプトとか)には恩恵ないかと
まとめ
Tuesday, December 4, 2012
• なんでOSのスレッド使わんのですっけ?
そもそも
Tuesday, December 4, 2012
• 今日のパクリ元
• http://patshaughnessy.net/2012/3/23/why-you-should-be-excited-about-
garbage-collection-in-ruby-2-0
• http://www.narihiro.info/resource/presen/bitmap_gc.pdf
• Rubyのスタックフレーム
• http://i.loveruby.net/ja/hack/frame.html
• Ruby Enterprise Edition はそもそもそんなGCらしい
• http://www.rubyenterpriseedition.com/download.html
参考
Tuesday, December 4, 2012
大分おおざっぱなメ
モリモデルのおさらい
Tuesday, December 4, 2012
スタック領域とヒープ領域に
論理的に分けて使う
Tuesday, December 4, 2012
メソッドコール毎にスタック
にスタックフレームが積まれ
る
Tuesday, December 4, 2012
Rubyの場合、全てオブジェク
トなのでヒーブに作られる。
Tuesday, December 4, 2012
• ※実際は、ローカル変数を束縛したProcを返したりす
るので、もっと複雑。あくまで概要
メソッドが終了すると、そのスタックフレームがpopされ、
前のメソッド(スタックフレーム)の途中から再開
Tuesday, December 4, 2012
Mark & Sweepのおさらい
Tuesday, December 4, 2012
ヒープにはどんどんオブジェクトがで
きるのでいつか足りなくなる。
Tuesday, December 4, 2012
もう使わなくなったオブジェ
クトもあるはず。。。
Tuesday, December 4, 2012
• 参照は、直接も間接も含む
使わなくなったオブジェクト = メソッド(スタックフレー
ム)からの参照がない
Tuesday, December 4, 2012
• こいつが「write」してる
スタックから参照があるオブジェクトを再帰的にたどって
マークする。
Tuesday, December 4, 2012
• = 要らない子
• = GC対象
マークされなかったものは参
照されていない。
Tuesday, December 4, 2012
そのアドレスを要らない子リストに追加して、今後そこか
らメモリを供給していく
Tuesday, December 4, 2012

More Related Content

Featured

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
Kurio // The Social Media Age(ncy)
 
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
Saba 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
 

Bitmap marking GC