# Temporary UObject creation and cleanup

**URL:** <https://forums.unrealengine.com/t/temporary-uobject-creation-and-cleanup/140865>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [April 2, 2020, 10:57pm UTC](https://forums.unrealengine.com/t/temporary-uobject-creation-and-cleanup/140865 "2020-04-02T22:57:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_4aa5607e1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4aa5607e1](https://forums.unrealengine.com/u/anonymous_user_4aa5607e1)\
**Post date:** [April 2, 2020, 10:57pm UTC](https://forums.unrealengine.com/t/temporary-uobject-creation-and-cleanup/140865/1 "2020-04-02T22:57:35Z")

</div>

We’re seeing lots of UObjects pile up from temporary UObjects created both in-Editor and during PIE (we use them at runtime and recreate them at other points for debug and other purposes). They don’t seem to be getting garbage collected, and forcing GC every frame in the editor only stops newly-created objects hanging around - it doesn’t clean up the existing objects.

We’re creating them like this:

```auto

{
    // SomeUClassPtr is TSubClassof<UMyObject>
    UMyObject* Obj = NewObject<UMyObject>(GetTransientPackage(), SomeUClassPtr);
    // do stuff with Obj - no references from UProperties
} // Obj goes out of scope

```

Is that correct? I assume we want them in the transient package as we don’t want them to get saved. Should we expect to see these objects getting destroyed frequently?

We’re mainly concerned about slowdowns and crashes in extended Editor or gameplay runs.

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [April 28, 2020, 7:52pm UTC](https://forums.unrealengine.com/t/temporary-uobject-creation-and-cleanup/140865/3 "2020-04-28T19:52:36Z")

</div>

> [@samtkeen](#):
>
> For clean up you can use
> 
> ```auto
> 
> delete UMyObject;
> 
> ```

No, you really can’t. UObjects are handled by the garbage collector and can’t be deleted directly. Please don’t suggest stuff that will cause guaranteed crashes.

The code looks fine and I’d expect them to be GCd. You can use the console command

```auto

obj refs name="myobjname"

```

to dump output on reference chains to see what’s keeping an object alive (the command can be slow).

```auto

obj list class="myclassname"

```

is useful too if you don’t know the names.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4aa5607e1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4aa5607e1](https://forums.unrealengine.com/u/anonymous_user_4aa5607e1)\
**Post date:** [May 19, 2020, 12:50am UTC](https://forums.unrealengine.com/t/temporary-uobject-creation-and-cleanup/140865/4 "2020-05-19T00:50:31Z")

</div>

Thanks very much, I’ll give those console commands a go. Documentation seems sparse, where should I be looking in the source to find these kinds of commands?

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [May 19, 2020, 4:45pm UTC](https://forums.unrealengine.com/t/temporary-uobject-creation-and-cleanup/140865/5 "2020-05-19T16:45:56Z")

</div>

You can call ‘MarkPendingKill()’ on the object before it goes out of scope, that should force GC to clean it up on the next pass.

Could be wrong here, but IIRC the transient package won’t clean up until the current UWorld is unloaded so that might be what’s keeping it around.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4aa5607e1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4aa5607e1](https://forums.unrealengine.com/u/anonymous_user_4aa5607e1)\
**Post date:** [May 19, 2020, 5:31pm UTC](https://forums.unrealengine.com/t/temporary-uobject-creation-and-cleanup/140865/6 "2020-05-19T17:31:36Z")

</div>

Ah that explains it. Yeah we discovered MarkPendingKill helped, we’ll keep using that then. Often we just want the objects around for the scope of a single function.
