✅ 操作成功!

三色标记法

发布时间:2023-06-06 作者:admin 来源:文学

三色标记法

三色标记法

贴的音序-中国功夫英文

2023年2月20日发(作者:工作总结个人)

golang的gc:三⾊标记清除法

⾸先来看下go处理gc的源码:

/GCrunsagarbagecollectionandblocksthecalleruntilthe

//lsoblocktheentire

//program.

funcGC(){

//Weconsideracycletobe:sweeptermination,mark,mark

//termination,nctionshouldn'treturn

//untilafullcyclehasbeencompleted,frombeginningto

//,wealwayswanttofinishupthecurrentcycle

//ans:

//

//ptermination,mark,ormarkterminationofcycle

//N,waituntilmarkterminationNcompletesandtransitions

//tosweepN.

//

//pN,helpwithsweepN.

//

//AtthispointwecanbeginafullcycleN+1.

//

//rcycleN+1bystartingsweepterminationN+1.

//

//rmarkterminationN+1tocomplete.

//

//thsweepN+1untilit'sdone.

//

//Thisallhastobewrittentodealwiththefactthatthe

//mple,whenweblock

//untilmarkterminationN,wemaywakeupincycleN+2.

//Waituntilthecurrentsweeptermination,mark,andmark

//terminationcomplete.

n:=(&)

gcWaitOnMark(n)

//We'rGCcycleN+1,which

//willfirstfinishsweepNifnecessaryandthenentersweep

//terminationN+1.

gcStart(gcTrigger{kind:gcTriggerCycle,n:n+1})

//WaitformarkterminationN+1tocomplete.

gcWaitOnMark(n+1)

//FinishsweepN+isbothto

//()isoftenused

//aspartoftestsandbenchmarkstogetthesystemintoa

//relativelystableandisolatedstate.

(&)==n+1&&sweepone()!=^uintptr(0){

ep++

Gosched()

}

//Callersmayassumethattheheapprofilereflectsthe

//just-completedcyclewhenthisreturns(historicallythis

//happenedbecausethiswasaSTWGC),butrightnowthe

//profilestillreflectsmarkterminationN,notN+1.

//

//AssoonasallofthesweepfreesfromcycleN+1aredone,

//wecangoaheadandpublishtheheapprofile.

//

//First,waitforsweepingtofinish.(Weknowthereareno

//morespansonthesweepqueue,butwemaybeconcurrently

//morespansonthesweepqueue,butwemaybeconcurrently

//sweepingspans,sowehavetowait.)

(&)==n+1&&(&mheap_.sweepers)!=0{

Gosched()

}

//Nowwe'rereallydonewithsweeping,sowecanpublishthe

//thisifwehaven'talreadyhit

//anothermarktermination.

mp:=acquirem()

cycle:=(&)

ifcycle==n+1||(gcphase==_GCmark&&cycle==n+2){

mProf_PostSweep()

}

releasem(mp)

}

go的垃圾回收,官⽅形容为:⾮分代,⾮紧缩,写屏障,三⾊并发标记清理算法。

⾮分代:不像java那样氛围年轻代和⽼年代,滋润也没有minor和majogc的区别

⾮紧缩:在垃圾回收之后不会进⾏内存整理以清除内存碎⽚

写屏障:在并发标记的过程中,如果应⽤程序修改了对象图,就可能出现标记遗漏的可能,写屏障是为了处理标记遗漏的问题。

三⾊:将GC中的对象按照搜索情况分成三种:

1.⿊⾊:对象在这次GC中已标记,且这个对象包含的⼦对象也已标记

2.灰⾊:对象在这次GC中已标记,但这个对象包含的⼦对象未标记

3.⽩⾊:对象在这次GC中未标记。

并发:可以和应⽤程序在⼀定程度上并发执⾏。

标记清理:GC算法分为2步:

1.标记阶段找出要回收的对象

2.清理阶段回收未被标记的对象(要被回收的对象)

👁️ 阅读量:0