当前位置:网站首页>C background GC cause and effect
C background GC cause and effect
2022-07-27 20:03:00 【Gegwu MMQ!!】
One : background
The purpose of writing this article is mainly because .NET Several books in the field about elaboration GC A book on , Are pure theories , Therefore, people who understand naturally understand , People who don't know can't verify it in person , I'll use this one windbg + Source code Let's see for sure .
Two : Why introduce the background GC
- backstage GC What problem has it solved
To solve any problem, we must first say what the problem is , We know Blocking version GC One notable feature is , stay GC During triggering , All user threads are Suspended. , there Pause Is a general designation , The picture is as follows :

such STW(Stop The World) I believe everyone is used to the mode , But there's a big problem here , Whether or not the current GC Is it temporary or full , Or compress or mark ,all in Full freezing , This simple and crude approach is definitely not desirable , It's also backstage GC Prerequisites for introduction .
that backstage GC What problem has it solved ?
The solution is FullGC Mode of Mark clear Recovery period , Release user threads .
Although this is a good Idea, But the complexity is definitely on several levels .
3、 ... and : backstage GC Detailed explanation
backstage GC Code Skeleton diagram
The source code in front , No secret , stay coreclr Project garbage-collection.md In file , It describes backstage GC Code flow chart of .GarbageCollectGeneration()
{
SuspendEE();
garbage_collect();
RestartEE();
}garbage_collect()
{
generation_to_condemn();
// decide to do a background GC
// wake up the background GC thread to do the work
do_background_gc();
}do_background_gc()
{
init_background_gc();
start_c_gc ();//wait until restarted by the BGC. wait_to_proceed();}
bgc_thread_function()
{
while (1)
{
// wait on an event
// wake up
gc1();
}
}gc1()
{
background_mark_phase();
background_sweep();
}
You can clearly see that you are doing Mark clear And the core logic is background_mark_phase() Function , Realize the three stages of marking : 1. Initial marker , 2. Concurrent Tags ,3. Final marker , among Concurrent Tags Stage , User threads are running normally , Realize the whole pause of the original Optimized to 2 A little pause .
- Flow chart analysis
For the sake of illustration , Draw a picture of the three stages as follows :

important clause : Stage 2 The restart of is in background_sweep() In the method , instead of Final marker (background_mark_phase) Stage .
Initial marker
At this stage, the user thread is in a suspended state ,bgc The thing to do is from Thread stack and Terminator queue Search for user root to implement reference graph traversal , Then let all user threads start , The simplified code is as follows :
void gc_heap::background_mark_phase()
{
dprintf(3, (“BGC: stack marking”));
GCScan::GcScanRoots(background_promote_callback,
max_generation, max_generation,
&sc);
dprintf(3, ("BGC: finalization marking"));
finalize_queue->GcScanRoots(background_promote_callback, heap_number, 0);
restart_vm();
}
Next, how to verify Stage 1 Is it a suspended state ? For the convenience of narration , Let's start with the last test code :
internal class Program
{
static List<string> list = new List<string>();
static void Main(string[] args)
{
Debugger.Break();
for (int i = 0; i < int.MaxValue; i++)
{
list.Add(String.Join(",", Enumerable.Range(0, 100)));
if (i % 10 == 0) list.RemoveAt(0);
}
}
}
And then use windbg stay background_mark_phase Function next breakpoint :bp coreclr!WKS::gc_heap::background_mark_phase that will do .
0:009> bp coreclr!WKS::gc_heap::background_mark_phase
0:009> g
Breakpoint 1 hit
coreclr!WKS::gc_heap::background_mark_phase:
00007ff9`e7bf73f4 488bc4 mov rax,rsp
0:008> !t -special
Lock
DBG ID OSID ThreadOBJ State GC Mode GC Alloc Context Domain Count Apt Exception
0 1 55d8 00000000006336B0 2a020 Preemptive 0000000000000000:0000000000000000 000000000062d650 -00001 MTA (GC)
6 2 568c 0000000000662F40 21220 Preemptive 0000000000000000:0000000000000000 000000000062d650 -00001 Ukn (Finalizer)
8 4 5730 0000000000676A90 21220 Preemptive 0000000000000000:0000000000000000 000000000062d650 -00001 Ukn
OSID Special thread type
0 55d8 SuspendEE
5 5688 DbgHelper
6 568c Finalizer
8 5730 GC
You can see it clearly ,0 Thread number shows SuspendEE word , Indicates that all managed threads are frozen at this time .
Concurrent Tags
At this stage, everyone plays his own game , The user thread is executing normally ,bgc Mark further in the background , Because it's parallel , So there is bgc The marked object reference relationship is User threads damage , therefore bgc use reset_write_watch Function with windows Memory page monitoring , The purpose is to find out those dirty pages , In the next stage to correct , The simplified code is as follows :
void gc_heap::background_mark_phase()
{
disable_preemptive(true);
// Dirty page monitoring
reset_write_watch(TRUE);
revisit_written_pages(TRUE, TRUE);
dprintf(3, ("BGC: handle table marking"));
GCScan::GcScanHandles(background_promote,
max_generation, max_generation,
&sc);
disable_preemptive(false);
}
To verify that the user thread is released at this time , Can be in revisit_written_pages The next breakpoint of the function , Use command :bp coreclr!WKS::gc_heap::revisit_written_pages .
0:008> bp coreclr!WKS::gc_heap::revisit_written_pages
0:008> g
coreclr!WKS::gc_heap::revisit_written_pages:
0:008> !t -special
Lock
DBG ID OSID ThreadOBJ State GC Mode GC Alloc Context Domain Count Apt Exception
0 1 55d8 00000000006336B0 2a020 Cooperative 000000000D1FD920:000000000D1FE120 000000000062d650 -00001 MTA
6 2 568c 0000000000662F40 21220 Preemptive 0000000000000000:0000000000000000 000000000062d650 -00001 Ukn (Finalizer)
8 4 5730 0000000000676A90 21220 Cooperative 0000000000000000:0000000000000000 000000000062d650 -00001 Ukn
OSID Special thread type
5 5688 DbgHelper
6 568c Finalizer
8 5730 GC
See no , that SuspendEE Miraculously disappeared , and 0 Of thread GC The mode is also changed to Cooperative, Indicates that manipulation is allowed Managed heap .
Final marker
etc. bgc It's almost done backstage , You can do it again SupendEE, take Concurrent Tags During this period, the dirty references caused by user threads are finally corrected , The data source of correction is monitored Windows Dirty page , The code won't work , Let's talk about how to verify. Phase 2 is back SuspendEE state ? Can be in background_sweep() Function next breakpoint , command : bp coreclr!WKS::gc_heap::background_sweep .
0:000> bp coreclr!WKS::gc_heap::background_sweep
0:000> g
coreclr!WKS::gc_heap::background_sweep:
00007ff9`e7b7a2e0 4053 push rbx
0:008> !t -special
Lock
DBG ID OSID ThreadOBJ State GC Mode GC Alloc Context Domain Count Apt Exception
0 1 55d8 00000000006336B0 2a020 Preemptive 0000000000000000:0000000000000000 000000000062d650 -00001 MTA
6 2 568c 0000000000662F40 21220 Preemptive 0000000000000000:0000000000000000 000000000062d650 -00001 Ukn (Finalizer)
8 4 5730 0000000000676A90 21220 Preemptive 0000000000000000:0000000000000000 000000000062d650 -00001 Ukn (GC)
OSID Special thread type
5 5688 DbgHelper
6 568c Finalizer
8 5730 GC SuspendEE
ha-ha , You can see that SuspendEE Back again .
- backstage GC Only in fullGC Mode ?
This is the last question that we should let everyone see for real , stay gc During triggering , The interior will maintain a gc_mechanisms Structure , It records the current GC All kinds of information triggered , It can be used windbg Lead it out and see .
0:008> x coreclr!settings
00007ff9e7f82e90 coreclr!WKS::gc_heap::settings = class WKS::gc_mechanisms 0:008> dt coreclr!WKS::gc_heap::settings 00007ff9e7f82e90
+0x000 gc_index : 0xb3
+0x008 condemned_generation : 0n2
+0x00c promotion : 0n1
+0x010 compaction : 0n0
+0x014 loh_compaction : 0n0
+0x018 heap_expansion : 0n0
+0x01c concurrent : 1
+0x020 demotion : 0n0
+0x024 card_bundles : 0n1
+0x028 gen0_reduction_count : 0n0
+0x02c should_lock_elevation : 0n0
+0x030 elevation_locked_count : 0n0
+0x034 elevation_reduced : 0n0
+0x038 minimal_gc : 0n0
+0x03c reason : 0 ( reason_alloc_soh )
+0x040 pause_mode : 1 ( pause_interactive )
+0x044 found_finalizers : 0n1
+0x048 background_p : 0n0
+0x04c b_state : 0 ( bgc_not_in_process )
+0x050 allocations_allowed : 0n1
+0x054 stress_induced : 0n0
+0x058 entry_memory_load : 0x49
+0x060 entry_available_physical_mem : 0x00000001`0a50d000
+0x068 exit_memory_load : 0
from condemned_generation=2 It can be seen that the current trigger is 2 generation GC, The reason is that the generation is full reason : 0 ( reason_alloc_soh ) .
边栏推荐
- mysql数据库中的数据如何加密呢?mysql8.0自带新特性
- VALN 11.9
- Sword finger offer 25. merge two sorted linked lists
- 中国业务型CDP白皮书 | 爱分析报告
- 解决 ViewUI 表格无数据时展示滚动条的问题
- Gesturedetector (gesture recognition)
- 文件操作防护
- PC博物馆(3) MITS Altair 8800
- 10.31 extended configuration of static route
- C # find perfect numbers, output daffodils and use of classes
猜你喜欢

PC博物馆(3) MITS Altair 8800

How to encrypt the data in MySQL database? Mysql8.0 comes with new features

Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.

全局函数

UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xff in position 0: invalid start byte

Version announcement | Apache Doris 1.1 release version officially released!

antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key

由单片机XTALIN引脚和XTALOUT引脚导出的对晶体震荡电路的深入理解

化工巨头巴斯夫&Pasqal:利用量子神经网络优化天气预报

Introduction to socke programming
随机推荐
【C#】正序、逆序、最大值、最小值和平均值
[Redis] Redis几种部署方式
Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.
Can go to QQ but can't open the web page
Version announcement | Apache Doris 1.1 release version officially released!
总线Bus是什么意思
Software configuration | tigervnc download, installation and configuration
访问控制
Sword finger offer 25. merge two sorted linked lists
函数优先顺序
Unity-FairyGUI播放视频(Lua)
China business CDP white paper | love Analysis Report
AutoCompleteTextView (input box pre match)
程序设计综合实验三
Chemical giant BASF & Pasqual: using quantum neural network to optimize weather forecast
C191:密码编译
连接池-归还连接详解(上)
pytorch乘法以及广播机制
GridView (implement table display icon)
Marqueetextview (running lantern)