官术网_书友最值得收藏!

Destroying UObject-derived classes

Removing any UObject derivative is simple in UE4. When you are ready to delete your UObject-derived class, we will simply call a single function (ConditionalBeginDestroy()) on it to begin teardown. We do not use the native C++ delete command on UObject derivatives. We show this in the following recipe.

Getting ready

You need to call ConditionalBeginDestroy() on any unused UObject-derived classes so that they get removed from memory. Do not call delete on a UObject-derived class to recoup the system memory. You must use the internal engine-provided memory management functions instead. The way to do this is shown next.

How to do it...

  1. Call objectInstance->ConditionalBeginDestroy() on your object instance.
  2. Null all your references to objectInstance in your client code, and do not use objectInstance again after ConditionalBeginDestroy() has been called on it.

How it works…

The ConditionalBeginDestroy() function begins the destruction process by removing all internal engine linkages to it. This marks the object for destruction as far as the engine is concerned. The object is then destroyed some time later by destroying its internal properties, followed by actual destruction of the object.

After ConditionalBeginDestroy() has been called on an object, your (client) code must consider the object to be destroyed, and must no longer use it.

Actual memory recovery happens some time later than when ConditionalBeginDestroy() has been called on an object. There is a garbage collection routine that finishes clearing the memory of objects that are no longer referenced by the game program at fixed time intervals. The time interval between garbage collector calls is listed in C:\Program Files (x86)\Epic Games\4.11\Engine\Config \BaseEngine.ini, and defaults to one collection every 60 seconds:

gc.TimeBetweenPurgingPendingKillObjects=60

Tip

If memory seems low after several ConditionalBeginDestroy() calls, you can trigger memory cleanup by calling GetWorld()->ForceGarbageCollection(true) to force an internal memory cleanup.

Usually, you do not need to worry about garbage collection or the interval unless you urgently need memory cleared. Do not call garbage collection routines too often, as this may cause unnecessary lag in the game.

主站蜘蛛池模板: 荆门市| 广安市| 饶河县| 平泉县| 广德县| 平南县| 宜黄县| 永州市| 巩义市| 嘉定区| 萝北县| 隆德县| 山西省| 蛟河市| 盱眙县| 右玉县| 新平| 鄄城县| 平陆县| 类乌齐县| 纳雍县| 平邑县| 临安市| 甘肃省| 湖南省| 万荣县| 冕宁县| 古丈县| 搜索| 石首市| 皋兰县| 凉山| 尖扎县| 青岛市| 芷江| 龙岩市| 金川县| 札达县| 班玛县| 嵊泗县| 郓城县|