当前位置:网站首页>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 );
边栏推荐
- 5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes
- Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
- Micro service knowledge sorting - cache technology
- PR notes:
- 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
- Machine learning support vector machine SVM
- Wechat applet quick start (including NPM package use and mobx status management)
- Acquisition and transmission of parameters in automatic testing of JMeter interface
- Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
- Today's work summary and plan: February 14, 2022
猜你喜欢

Teach you how to quickly recover data by deleting recycle bin files by mistake

2022 Xinjiang latest road transportation safety officer simulation examination questions and answers
![AI enhanced safety monitoring project [with detailed code]](/img/a9/cb93f349229e86cbb05ad196ae9553.jpg)
AI enhanced safety monitoring project [with detailed code]

Phpstudy set LAN access

Virtual machine installation deepin system

kubernetes集群搭建efk日志收集平台

Xctf attack and defense world crypto advanced area best_ rsa
![2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]](/img/b6/5d6b946d8001e2d73c2cadbdce72fc.png)
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]

Upgrade PIP and install Libraries

Camera calibration (I): robot hand eye calibration
随机推荐
Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
Global and Chinese market of electrolyte analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of active matrix LCD 2022-2028: Research Report on technology, participants, trends, market size and share
Strict data sheet of new features of SQLite 3.37.0
First knowledge of database
HCIA-USG Security Policy
11-grom-v2-05-initialization
How to check the permission to write to a directory or file- How do you check for permissions to write to a directory or file?
Upgrade PIP and install Libraries
2.5 conversion of different data types (2)
What is the difference between a kill process and a close process- What are the differences between kill process and close process?
Xctf attack and defense world crypto advanced area best_ rsa
[raid] [simple DP] mine excavation
BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
Oak-d raspberry pie cloud project [with detailed code]
CMD implements the language conversion of locale non Unicode programs
How to improve data security by renting servers in Hong Kong
2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]
Test changes in Devops mode -- learning and thinking