当前位置:网站首页>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

User login 【 I 】

Simple spiral ladder generation for Houdini program modeling

A preliminary understanding of function

Understanding of distributed transactions

BRDF of directx11 advanced tutorial PBR (2)

Es6-es11 learning

前台展示LED数字(计算器上数字类型)

Unable to access this account. You may need to update your password or grant the account permission to synchronize to this device. Tencent corporate email

E-book analysis
随机推荐
Halcon uses points to fit a plane
网络加速谁更猛?CDN领域再现新王者
Data integration framework seatunnel learning notes
E-book analysis
Niuke daily question -day1
Nrf52832 services et fonctionnalités personnalisés
log4j 1. X upgrade to 2 Solution of X dependency incompatibility
Liunx Foundation
Getting started with houdininengine HDA and UE4
China embolic coil market trend report, technical innovation and market forecast
nrf52832--官方例程ble_app_uart添加led特性,实现电脑uart和手机app控制开发板led开和关
Research Report on truffle fungus industry - market status analysis and development prospect forecast
肝了一个月的 DDD,一文带你掌握
First note
nus_ data_ Handler source code interprets data types such as structure
[Yu Yue education] basic reference materials of accounting of Nanjing Normal University
前台展示LED数字(计算器上数字类型)
The application could not be installed: INSTALL_ FAILED_ TEST_ ONLY
User login 【 I 】
Idea common configuration