SlideShare a Scribd company logo
1 of 30
Download to read offline
WhatwelovetoknowaboutGarbage
Collectoranditsimplementation
@huydx
LINECorp
Highlevelview
CollecttheGarbage
Preventsdanglingpointers
ReducechancebutNOTeliminatememoryleak!
Somefunfacts
HowmanylanguageshaveembededGC?
Answer:78/256
HowmuchperformancepenaltyGC
created
Answer:17%
Whileitistruethatautomaticmemorymanagementdoesimposea
performancepenaltyontheprogram,itisnotasmuchasis
commonlyassumed
IsGCalgorithmhard?
It'sfuckinghard
WhyunderstandGCimplementationis
important?
Presiquisite:Youdon'twanttomanagememoryyourself!
Latencycriticalapplication
Throughputcriticalapplication
Youdon'twantGCtohang(STW)yourapplicationforx0sseconds
SomeGCconcepts/term
Heap:arrayofmemorywords,orobjectgraph
Mutator:mutatetheheap(new/delete/update..)
Collector:executeGCcode
Mostlysinglethread:toreducecomplexity
That'swhyweneedtoStopTheWorld(STW)
Liveness:liveobject==accessibleobject
Reachability:reachabledirectlyorbychain
GCtasks
Allocatespacefornewobjects
Identifyliveobjects
Reclaimspacebydeadobjects
GCalgorithms
Alot
Marksweep
Copying
Markcompact
Refcounting
Ormixbetweenthose
MarkSweepGC
Mostcommonone
GoodsampletoshowhowhardGCimplementationis
2phasescollection:
Traverseobjectgraphandmarking(sometimeswecallit
"tracing"
Examineeveryunmarkobjectandreclaimitsspace
from(http://www.narihiro.info/)
markingphase
markFromRoots():
initialise(worklist)
for each fld in Roots
ref ← *fld
if ref ≠ null && not isMarked(ref)
setMarked(ref)
add(worklist, ref)
mark()
initialise(worklist):
worklist ← empty
mark():
while not isEmpty(worklist)
ref ← remove(worklist)
/* ref is marked */
for each fld in Pointers (ref)
child ← *fld
if child ≠ null && not isMarked(child)
setMarked(child)
add(worklist, child)
sweepingphase
sweep(start, end):
scan ← start
while scan < end
if isMarked(scan)
unsetMarked(scan)
else free(scan)
scan ← nextObject(scan)
Exploringminigc
https://github.com/authorNari/minigc
Simpleandminimum
MarkandSweep
ConservativeGC
Selfimplementmemoryallocator(K&Rmalloc)
ConservativevsPreciseGC
Conservative:knownothingaboutthelanguage,justwalkaround
inmemoryaddressrangeandfindwhatlookslikeobjectaddress
Precise:Knowexactlywhatisobjectaddressornot.
Requirehookwithcompilerphasetogetknowledgeaboutthe
runtime
ConservativeGC
Thetricktoconservativecollectionistotreatthestackasan
ordinaryrangeofmemory,andassumethateverywordonthe
stackisapointerintotheheap.Thus,thecollectormarksall
objectswhoseaddressesappearanywhereinthestack,without
knowingforsurehowthatwordismeanttobeinterpreted.
Problem:
intprimitivevsaddress!
thereforefullcopyingcollectorisnotabletoimplemented
(differentstory)
minigcgc
void
garbage_collect(void)
{
size_t i;
/* marking machine context */
gc_mark_register();
gc_mark_stack();
/* marking roots */
for (i = 0; i < root_ranges_used; i++) {
gc_mark_range(root_ranges[i].start, root_ranges[i].end)
}
/* sweeping */
gc_sweep();
}
markregister:markeverythingincurrentcontext
usingsetjmp
static void
gc_mark_register(void)
{
jmp_buf env;
size_t i;
setjmp(env);
for (i = 0; i < sizeof(env); i++) {
gc_mark(((void **)env)[i]);
}
}
markstack:markfromstartofstacktoendofstack
static void
gc_mark_stack(void)
{
set_stack_end();
if (stack_start > stack_end) {
gc_mark_range(stack_end, stack_start);
}
else {
gc_mark_range(stack_start, stack_end);
}
}
markingusingheaderbitset
static void
gc_mark(void * ptr)
{
GC_Heap *gh;
Header *hdr;
/* mark check */
if (!(gh = is_pointer_to_heap(ptr))) return;
if (!(hdr = get_header(gh, ptr))) return;
if (!FL_TEST(hdr, FL_ALLOC)) return;
if (FL_TEST(hdr, FL_MARK)) return;
/* marking */
FL_SET(hdr, FL_MARK);
DEBUG(printf("mark ptr : %p, header : %pn", ptr, hdr));
/* mark children */
gc_mark_range((void *)(hdr+1), (void *)NEXT_HEADER(hdr));
}
Number.Hard.1
Howanobjectcouldbemarked?
Usingheadermakethewholememoryrangedirty,whichleadto
pagefault
Alternativeoptionbesideheader
Usingsidetabletostoremarkbits
Number.Hard.2
Representstateofobjectsduringcollection
Tricolourabstraction:white,gray,black(beingprocessed,
processed,notyetprocessed)
Number.Hard.3
K&Rmallocusinglinkedlist
Shouldhavebetterimplementation
Tonsofthingscouldbeimproved:
Improvementofmarkingmethod:smallerheader(tagmarkflag
intosizepointer)
Cachehit/miss
Multithreaded
Compactornotcompact(anotherstory)
Reference:
GChandbook
SurveyofGCalgorithm
ftp://ftp.cs.utexas.edu/pub/garbage/gcsurvey.ps
http://www.hboehm.info/gc/
https://ruby‑hacking‑guide.github.io/gc.html
https://speakerdeck.com/tenderlove/building‑a‑compacting‑gc

More Related Content

Similar to Some note about GC algorithm

Similar to Some note about GC algorithm (20)

A Fast Intro to Fast Query with ClickHouse, by Robert Hodges
A Fast Intro to Fast Query with ClickHouse, by Robert HodgesA Fast Intro to Fast Query with ClickHouse, by Robert Hodges
A Fast Intro to Fast Query with ClickHouse, by Robert Hodges
 
What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)
What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)
What's new with JavaScript in GNOME: The 2020 edition (GUADEC 2020)
 
Choosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
Choosing Right Garbage Collector to Increase Efficiency of Java Memory UsageChoosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
Choosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
 
Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Clo...
Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Clo...Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Clo...
Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Clo...
 
Java memory problem cases solutions
Java memory problem cases solutionsJava memory problem cases solutions
Java memory problem cases solutions
 
Maximize The Performance of HTML5 Video in RPI2 (Embedded Linux Conference 2016)
Maximize The Performance of HTML5 Video in RPI2 (Embedded Linux Conference 2016)Maximize The Performance of HTML5 Video in RPI2 (Embedded Linux Conference 2016)
Maximize The Performance of HTML5 Video in RPI2 (Embedded Linux Conference 2016)
 
Top-5-java-perf-problems-jax_mainz_2024.pptx
Top-5-java-perf-problems-jax_mainz_2024.pptxTop-5-java-perf-problems-jax_mainz_2024.pptx
Top-5-java-perf-problems-jax_mainz_2024.pptx
 
GC Tuning Confessions Of A Performance Engineer
GC Tuning Confessions Of A Performance EngineerGC Tuning Confessions Of A Performance Engineer
GC Tuning Confessions Of A Performance Engineer
 
Elastic JVM for Scalable Java EE Applications Running in Containers #Jakart...
Elastic JVM  for Scalable Java EE Applications  Running in Containers #Jakart...Elastic JVM  for Scalable Java EE Applications  Running in Containers #Jakart...
Elastic JVM for Scalable Java EE Applications Running in Containers #Jakart...
 
Tools for Metaspace
Tools for MetaspaceTools for Metaspace
Tools for Metaspace
 
Adtech scala-performance-tuning-150323223738-conversion-gate01
Adtech scala-performance-tuning-150323223738-conversion-gate01Adtech scala-performance-tuning-150323223738-conversion-gate01
Adtech scala-performance-tuning-150323223738-conversion-gate01
 
Adtech x Scala x Performance tuning
Adtech x Scala x Performance tuningAdtech x Scala x Performance tuning
Adtech x Scala x Performance tuning
 
Tales of modernizing trello's web stack
Tales of modernizing trello's web stackTales of modernizing trello's web stack
Tales of modernizing trello's web stack
 
Kostiantyn Yelisavenko "Mastering Macro Benchmarking in .NET"
Kostiantyn Yelisavenko "Mastering Macro Benchmarking in .NET"Kostiantyn Yelisavenko "Mastering Macro Benchmarking in .NET"
Kostiantyn Yelisavenko "Mastering Macro Benchmarking in .NET"
 
Troubleshooting Memory Problems in Java Applications
Troubleshooting Memory Problems in Java ApplicationsTroubleshooting Memory Problems in Java Applications
Troubleshooting Memory Problems in Java Applications
 
How to Become a Winner in the JVM Performance-Tuning Battle
How to Become a Winner in the JVM Performance-Tuning BattleHow to Become a Winner in the JVM Performance-Tuning Battle
How to Become a Winner in the JVM Performance-Tuning Battle
 
Deciphering development technologies
Deciphering development technologiesDeciphering development technologies
Deciphering development technologies
 
Magic with groovy & grails
Magic with groovy & grailsMagic with groovy & grails
Magic with groovy & grails
 
ZGC-SnowOne.pdf
ZGC-SnowOne.pdfZGC-SnowOne.pdf
ZGC-SnowOne.pdf
 
Introduction to segmentation fault handling
Introduction to segmentation fault handling Introduction to segmentation fault handling
Introduction to segmentation fault handling
 

More from Huy Do

CacheとRailsの簡単まとめ
CacheとRailsの簡単まとめCacheとRailsの簡単まとめ
CacheとRailsの簡単まとめ
Huy Do
 
[Htmlday]present
[Htmlday]present[Htmlday]present
[Htmlday]present
Huy Do
 

More from Huy Do (17)

Distributed Tracing, from internal SAAS insights
Distributed Tracing, from internal SAAS insightsDistributed Tracing, from internal SAAS insights
Distributed Tracing, from internal SAAS insights
 
Write on memory TSDB database (gocon tokyo autumn 2018)
Write on memory TSDB database (gocon tokyo autumn 2018)Write on memory TSDB database (gocon tokyo autumn 2018)
Write on memory TSDB database (gocon tokyo autumn 2018)
 
Engineering Efficiency in LINE
Engineering Efficiency in LINEEngineering Efficiency in LINE
Engineering Efficiency in LINE
 
GOCON Autumn (Story of our own Monitoring Agent in golang)
GOCON Autumn (Story of our own Monitoring Agent in golang)GOCON Autumn (Story of our own Monitoring Agent in golang)
GOCON Autumn (Story of our own Monitoring Agent in golang)
 
Story Writing Byte Serializer in Golang
Story Writing Byte Serializer in GolangStory Writing Byte Serializer in Golang
Story Writing Byte Serializer in Golang
 
Akka と Typeの話
Akka と Typeの話Akka と Typeの話
Akka と Typeの話
 
[Scalameetup]spark shuffle
[Scalameetup]spark shuffle[Scalameetup]spark shuffle
[Scalameetup]spark shuffle
 
DI in ruby
DI in rubyDI in ruby
DI in ruby
 
Itlc2015
Itlc2015Itlc2015
Itlc2015
 
Consistent Hashingの小ネタ
Consistent Hashingの小ネタConsistent Hashingの小ネタ
Consistent Hashingの小ネタ
 
Thriftを用いた分散型のNyancatを作ってきた
Thriftを用いた分散型のNyancatを作ってきたThriftを用いた分散型のNyancatを作ってきた
Thriftを用いた分散型のNyancatを作ってきた
 
NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]
 
実践Akka
実践Akka実践Akka
実践Akka
 
CA15卒勉強会 メタプログラミングについて
CA15卒勉強会 メタプログラミングについてCA15卒勉強会 メタプログラミングについて
CA15卒勉強会 メタプログラミングについて
 
Making CLI app in ruby
Making CLI app in rubyMaking CLI app in ruby
Making CLI app in ruby
 
CacheとRailsの簡単まとめ
CacheとRailsの簡単まとめCacheとRailsの簡単まとめ
CacheとRailsの簡単まとめ
 
[Htmlday]present
[Htmlday]present[Htmlday]present
[Htmlday]present
 

Recently uploaded

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 

Recently uploaded (20)

tonesoftg
tonesoftgtonesoftg
tonesoftg
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 

Some note about GC algorithm