当前位置:网站首页>Analysis of memory management mechanism of (UE4 4.26) UE4 uobject
Analysis of memory management mechanism of (UE4 4.26) UE4 uobject
2022-06-12 06:03:00 【Senior brother Dai Dai】
Preface
In the last chapter UE4 C++ FMemory Memory management goes from getting started to going deep It introduces FMemory Memory management mode of , This section introduces UObject Memory management for ,UObject Memory management is based on FMemory This set of underlying memory allocation mechanism . This article focuses on UObject Of UPROPERTY Mark memory reclamation , as for UObject Memory allocation and so on .
UObject Memory allocation
UObject The general flow of memory allocation

The specific process is as shown above , UObject Our memory allocator is called GUObjectAllocator, Its memory allocation is in FMemory On the basis of a package , I won't introduce too much here . as for UObject After being allocated memory , every last UObject Object is replaced by a FUObjectItem Object wrap to GUObjectArray Array , Conduct overall management .FUObjectItem and GUObjectArray Let's talk about it in detail .
UObject Memory garbage collection (GC) The overall execution process
Here is the main explanation UPROPERTY Marked UObject Variable memory recycling mechanism .
GC Call Stack

Generally summarized as :

analysis UPROPERTY, Generate tag reference information
UClass Analyze all... When registering UPROPERTY Annotated variables , Generated class FGCReferenceTokenStream Information

FGCReferenceTokenStream class The internal variable is a TArray<uint32>, Contains UObject in UPROPERTY Reference information of variables (FGCReferenceInfo), about GC Mark Stage , Used to analyze being UPROPERTY Of the tag UObject Whether the reference chain is valid
GC Conditions trigger

UE4 Trigger GC To execute the entry function UEngine::PerformGarbageCollectionAndCleanupActors()
Whether to trigger GC The judgment of is made at every frame , Once the conditions are met, proceed GC

UE4 Every once in a while GC once , This “ A span ” It can be set in the engine

GC perform

As you can see from the above figure GC There are mainly two stages :
GC Mask(GC Mark Object State phase ) and UObject Memory Collect Memory clear
GC Objects and GC state
Have a look first UObject GC state Relevant main data structures


You can see each one UObject Objects are represented by a FUObjectItem Manages the , FChunkedFixedUObjectArray Managing memory blocks , A memory block contains N individual FUObjectItem object , You can simply think of it this way ,GUObjectArray Is management FUObjectItem(UObject) A large array of objects . FUObjectItem The object has a int32 Flags Variable Used especially to indicate UObject Object at this time GC state

for instance , We call AActor::Destroy Destroy one Actor object

AActor perform Destroy When Put your own FUObjectItem The tag status is EInternalObjectFlags::PendingKill

This is the time UObject The object is in a state that can be reclaimed at any time , So judge a UObject Whether the object is secure , It is not only to judge whether it is empty , It is also necessary to judge whether it is in PendingKill state
if(Object != nullptr && !Object->IsPendingKill()As mentioned in the previous article, calling UObject::AddToRoot() Would not be UE4 For memory recycling , Because at this time FUObjectItem The tag status is EInternalObjectFlags::RootSet, In this state UObject Will not be recycled by memory . The specific reasons will be discussed below .
GC The labeling

GC The marking process mainly occurs in GarbageCollection.cpp::CollectGarbageInternal, At this point, all UObject Object first Accessibility analysis , Reachability analysis mainly analyzes a UObject Whether the object is reachable , If not, mark it as EInternalObjectFlags::Unreachable, Is considered invalid memory to further participate in the following GC Step is reclaiming memory . It is mainly divided into two parts ( The blue part )

1. Let's start with nothing KeepFlags and EInternalObjectFlags::GarbageCollectionKeepFlags All of the signs UObject Object marked as EInternalObjectFlags::Unreachable, This process is multithreaded .

stay MarkObjectsAsUnreachable When we judge the reachability in parallel, we see such a code , For being in EInternalObjectFlags::RootSet Objects of are not marked as unreachable , This explains the above UObject::AddToRoot It can make UObject Objects will not be GC That's what I'm saying .

2. Yes UPROPERTY Of the tag Object Class object for reachability analysis
This analysis process ProcessObjectArray It is multithreaded

In analyzing accessibility , Yes UClass Variable reference information for FGCReferenceTokenStream. in the light of UPROPERTY Mark UObject Various cases of variables ( Single UObject object ,TArray<UObject>, contain UObject Of TMap wait ) Carry out different classification to analyze the accessibility of objects , If the reference chain UObject Be judged unreachable , Will also be marked as EInternalObjectFlags::Unreachable

The above two parts are accessibility analysis , Will be held invalid UObject Object carried out EInternalObjectFlags::Unreachable Mark
Collect unreachable UObject object

stay GatherUnreachableObjects Collect unreachable FUObjectItem object , Put in a global array of unreachable objects
Start cleaning Object Stage
UnhashUnreachableObjects, For the unreachable UObject Carry out further cleaning , eliminate UObject Some information , Such as linker's export table wait , Last marked as RF_BeginDestroyed( Notice that this is Object The state of being rather than FUObjectItem A mark of state )



complete UObject Information cleaning stage

UObject The preliminary information has been cleared in the previous step , We have to clean up all the information here , Last marked as RF_FinishDestroyed



Memory recovery phase
Of the above stage UObject Finally, all kinds of information are cleared , Marked as RF_FinishDestroyed, And then to UObject Do real memory reclamation .UObject The memory of is recycled in FAsyncPurge In the middle of

You can see FAsyncPurge Inherit FRunnable, yes UE4 A class used in multithreading scenarios , But in memory reclamation
UE4 There may be a single thread ( Sync ) Or multithreading ( asynchronous ) Memory reclamation :

Reference
边栏推荐
- Halcon 3D 1 Reading 3D data
- 468. verifying the IP address
- Leetcode 第 80 場雙周賽題解
- Database Experiment 3: data query
- Quickly master makefile file writing
- Performance optimization metrics and tools
- Sqlite Cross - compile Dynamic Library
- Introduction to thread pool: ThreadPoolExecutor
- Leetcode-1705. Maximum number of apples to eat
- Filter的注解配置
猜你喜欢

sqlite交叉編譯動態庫

Un mois de DDD hépatique.

Conversion of Halcon 3D depth map to 3D image

Who is more fierce in network acceleration? New king reappeared in CDN field

Guns框架多数据源配置,不修改配置文件

从传统网络IO 到 IO多路复用

Redis transaction

Houdini & UE4 programmed generation of mountains and multi vegetation scattering points

March 4, 2021

Login authentication filter
随机推荐
BlockingQueue interface introduction
Project management and overall planning
March 23, 2021
MySQL notes
Leetcode-717. 1-bit and 2-bit characters (O (1) solution)
Introduction to redis high availability
Halcon uses points to fit a plane
Directx11 advanced tutorial PBR (1) summary of physical phenomena of light
Automatic annotation of target detection based on lffd model to generate XML file
Quickly master makefile file writing
Findasync and include LINQ statements - findasync and include LINQ statements
数据库实验三:数据查询
Leetcode-1604. Warning people who use the same employee card more than or equal to three times within one hour
Date ()
[PowerShell] command line output and adding system environment variables
China Aquatic Fitness equipment market trend report, technical innovation and market forecast
sqlite交叉編譯動態庫
TCP and UDP introduction
将一个文件夹图片分成训练集和测试集
Guns framework multi data source configuration without modifying the configuration file