Original address
For garbage collection, there are two conditions: one, to know where all the objects are placed, that is, where they are in memory. To know the object's memory, what does the data mean, whether it is a float number or an int number or an object pointer value. With both of these conditions, you can traverse all the objects, find the object that is not referenced, and then delete the release.
U3 is how to meet these two conditions, first look at the first one. Almost all of the classes in U3, Uobject as the base class, the general core base classes are as concise as possible, but Uobject is very complex, there are a large number of static member variables and member functions, to implement including object construction, object management, garbage collection, resource loading and other functions.
First, look at Gobjobjects, a static tarray<uobject*> pointer array that records the address of an object that was constructed after the game was started. The object is constructed by using the Staticconstructobject () function, which first finds by name, whether the object already exists, and if not, calls malloc () to request memory, Then set the Defaultproperties value AH config value ah wait, and then call AddObject () put the object into the Gobjobjects array, but also into the Gobjhash hash table, convenient to find the object later. Of course, instead of putting it directly at the end of the gobjobjects array, there is a tarray<int> gobjavailable array that records the index of the null item in the gobjobjects array. These are the pits that are left behind by garbage collection objects.
So when an object is placed in an array, its index value will never change until it dies, leaving the pit behind. So to traverse all the objects, just traverse the gobjobjects array. How do you determine the data in the object's memory, whether it is a float or an int or an object pointer? UE3 is written in C + +, the C + + itself does not support metadata ah, how to do it, the key is in the script. Scripts are generally used to write game logic, which is handy, but UE3 is script-based. Almost all classes need to be defined in the script, then compiled to automatically generate the. h header file, and then manually add the. cpp file to implement the native function. When compiling the script, the metadata for all variables and functions in the class is recorded, saved in the generated. u file, and each variable in each class has a corresponding Uobject object corresponding to each function, which is equivalent to the metadata. For example, the Drawscale variable in the actor class, is a float type, represented by a Ufloatproperty object, Ufloatproperty inherits from Uproperty,uproperty inherit from Ufield, Ufield inherits from Uobject. In order to record a variable information, inherited so many levels, and uobject is so large, really generous. So when the game is running, build a bunch of Ufloatproperty,uintproperty,ustructproperty objects into the gobjobjects array, and when the number of scripts is the number of classes, the whole heap is spectacular. The degree of prosperity will be explained later. OK, now you can iterate through all of the objects, and you can do garbage collection based on the metadata from the script to determine the data type.
Garbage collection is divided into two steps, one of which is to iterate over all objects and mark the objects that need to be recycled. The process is to traverse the gobjobjects array, find the objects that have rf_rootset these identities, put them in an array, and then identify all the other objects as rf_unreachable. It then iterates through the array, parses the type of the member variable in the object, such as the Gcrt_object type, knows that it is an object pointer, and then clears the object it points out of the unreachable identity. When this is done, there are objects that are rf_unreachable identified, which are objects that are no longer referenced.
The second step is to recover the object and release the memory.
This is the memory of the recovery of the approximate impression, if the details come, in fact, have not read. How many of these objects have not been ufloatproperty? Wrote a small function, traversing the gobjobjects array, the FullName and index of all objects to print out, a look had to, the object of a total of nearly 90,000, and ufloatproperty such objects, incredibly about 60,000, accounted for two-thirds, really generous.
The Uobject objects of these metadata will certainly persist and do not require garbage collection, so they should be skipped during the object traversal phase. UE3 has defined a variable gobjfirstgcindex, traversing the gobjobjects array, starts with this subscript. Just the default value of this value is 0, change it to 6000, and then slightly measured, traverse the time spent, from 0.28s to 0.12s, the effect is significant. Do not know to do games with UE3, is it necessary to manually set this variable according to the actual number of classes in the project? It would be nice to have a chance to send an email to ask.
PS: Later look at the code, the original engine has been set, if it is seekfreeloading mode, then use the value of Defaultengine.ini MAXOBJECTSNOTCONSIDEREDBYGC. This value should be configured to how large, according to Uobject::staticinit () in the note, the MAKEOBJECTSTODISREGARDFORGC () function in the statistics of the objects belonging to the root set and recorded in the log, according to the log records of the value, To configure MAXOBJECTSNOTCONSIDEREDBYGC, you can
Original address: http://www.cnblogs.com/flycodes/articles/2721367.html
(reprint) Unreal Engine 3--ue3 garbage collection mechanism