当前位置:网站首页>Introduction to golang garbage collection
Introduction to golang garbage collection
2022-07-03 20:14:00 【alwaysrun】
List of articles
The core of garbage collection is to mark which memory is still in use ( Is quoted to ), Which memory is no longer used ( It's not quoted ), Reclaim unreferenced memory , For subsequent memory allocation .
Garbage collection
Common garbage collection algorithms :
- Reference count : Maintain a reference count for each object , When the object that references the object is destroyed , Quote count minus 1; When the reference counter is 0 When the object is recycled .
- advantage : Objects can be recycled quickly , There will be no memory exhaustion or until a certain threshold is reached .
- shortcoming : It doesn't handle circular references very well , And maintain reference counts in real time , There is also a price .
- Representative language :Python、PHP、Swift
- Mark - eliminate : Traverse all referenced objects from the root variable , The referenced object is marked as ” Cited ”, Those that are not marked are recycled .
- advantage : Solved the shortcoming of reference counting .
- shortcoming : need STW, That is to stop the program temporarily .
- Representative language :Golang( It adopts three color marking method )
- Generational collection : Divide the generation space according to the life cycle of the object , Long life cycle into the elderly , The new generation is short , Different generations have different recycling algorithms and frequency .
- advantage : Good recycling performance
- shortcoming : Algorithm is complicated
- Representative language :JAVA
go Garbage collection
Garbage collection starts from root Object starts scanning , hold root The memory referenced by the object is marked “ Cited ”( Need to recursively handle pointers ); After all the marks are made , All unmarked identifiers are unassigned .
stay span There is a bit mark for allocating memory in the structure :
gcmarkBits Record each memory tag , Its structure and allocaBits Exactly the same as ; At the end of the mark , take allocBits Point to gcmarkBits,gcmarkBits Reassign at next mark .
Tricolor notation ( There are three states in the recycling process ):
- gray : The object is still waiting in the tag queue ;
- black : Object has been marked (gcmarkBits The corresponding bit is 1)
- white : The object is not marked (gcmarkBits The corresponding bit is 0), Will be GC Recycle .
Recycling optimization
STW(StopTheWorld) Is to stop all goroutine, Concentrate on garbage collection , Wait until the garbage collection is over goroutine.
Write barriers (Write Barrier): It's Jean goroutine And GC Means to run simultaneously ; stay GC At a specific time , When the pointer is turned on, the pointer will be marked with ( This round is not recycled );
auxiliary GC(Mutator Assist):GC In the process of execution , if goroutine Need to allocate memory , Then let this goroutine Take part GC The job of .
trigger
When the memory allocation reaches the threshold : threshold = The last time GC Memory allocation * Memory growth rate
The memory growth rate is determined by GOGC control , The default is 100, That is, it starts when the memory is doubled GC.
Trigger regularly : By default , The longest 2 Minutes to trigger GC.
runtime.GC(), Can be triggered manually GC.
Escape analysis
Escape analysis (Escape analysis) It means that the compiler decides the memory allocation location ; The compiler determines whether to escape according to whether the object is externally referenced by the function :
- If there is no reference outside the function , Put it on the stack first ( Big object , When the stack storage capacity is exceeded , Will be put in the heap );
- If there is a reference outside the function , It must be in the pile ;
The parameter of the function is interface type , It is difficult to determine the specific type during compilation , There will also be escape ; Local variables referenced in closures will also produce escape ;
- Allocating memory on the stack is more efficient than allocating memory in the heap
- Memory allocated on the stack is not required GC Handle
- The memory allocated on the heap will be handed over to GC Handle
- The purpose of escape analysis is to determine whether the internal allocation address is stack or heap
- Escape analysis is completed in the compilation phase
Passing pointers can reduce the copy of the underlying value , Can improve efficiency , But if the amount of data copied is small , Due to the pointer passing, there will be escape , The heap may be used , It may also increase GC The burden of , So passing pointers doesn't have to be efficient .
Code optimization
To improve performance , Need to reduce the allocation of objects ( Increase reuse , Reduce GC pressure ):
- Reduce object allocation :
- Reuse objects as much as possible ;
- When there are many small objects , Consider encapsulating it into a large object ( adopt struct Combine objects ,GC At the time of scanning , Just judge whether the whole object is valid );
- slice And map Wait for pre allocated memory ;
- string And byte[] When switching between , It will involve memory reallocation and data replication , Try to reduce ;
- Avoid a large number of string connection operations ( adopt + Connect ):
- fmt.Sprintf: Easy to splice strings ;
- strings.Join: Convenient concatenation of string arrays ;
- strings.Builder: Convenient string connection , Try to pre allocate capacity ;
- Slice try to pre allocate capacity : Reduce append Memory reallocation and data replication caused by automatic growth in the process ;
- Clean up in time ( adopt defer Close handle, etc );
边栏推荐
- Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
- 2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
- BOC protected amino acid porphyrins TAPP ala BOC, TAPP Phe BOC, TAPP Trp BOC, Zn · TAPP ala BOC, Zn · TAPP Phe BOC, Zn · TAPP Trp BOC Qiyue
- Global and Chinese market of electrolyte analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- 2022-06-27 advanced network engineering (XII) IS-IS overhead type, overhead calculation, LSP processing mechanism, route revocation, route penetration
- 11-grom-v2-05-initialization
- Global and Chinese markets of polyimide tubes for electronics 2022-2028: Research Report on technology, participants, trends, market size and share
- HCIA-USG Security Policy
- First knowledge of database
- MPLS configuration
猜你喜欢
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
AcWing 1460. Where am i?
Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)
2.1 use of variables
Wechat applet quick start (including NPM package use and mobx status management)
原生表格-滚动-合并功能
2022 Xinjiang latest road transportation safety officer simulation examination questions and answers
How to improve data security by renting servers in Hong Kong
BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
Use of aggregate functions
随机推荐
How to set the system volume programmatically- How to programmatically set the system volume?
【c】 Digital bomb
FAQs for datawhale learning!
Leetcode daily question solution: 540 A single element in an ordered array
IP address is such an important knowledge that it's useless to listen to a younger student?
2.7 format output of values
WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
How to improve data security by renting servers in Hong Kong
BOC protected alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC supplied by Qiyu
Parental delegation mechanism
Rad+xray vulnerability scanning tool
PR notes:
MySQL learning notes - single table query
Xctf attack and defense world crypto advanced area best_ rsa
Bool blind note - score query
Professional interpretation | how to become an SQL developer
Micro service knowledge sorting - asynchronous communication technology
MPLS configuration
Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue
Micro service knowledge sorting - three pieces of micro Service Technology