Memory Leak when using Play in Editor

I suspect there is a memory leak when using Play-In-Editor.

I am using a (very slightly modified) version of the Shooter Game Example project. Whenever I use the Play-In-Editor option for testing, I see the memory usage of the UE4Editor.exe process rise in the Windows Task Manager. This is to be expected, considering characters, weapons, controllers, etc. are spawned and initialized for gameplay. However, the memory usage does not drop back down when quitting play-mode and returning to the editor. I can also repeat this process a number of times (entering play-mode and leaving again), and every time the memory usage will increase again and never drop, so after a large amount of tests this can result in the computer running out of memory and would require re-starting the editor in between tests to clean up again.

In the basic Shooter Game Example project this is not a big problem yet, but it becomes a much larger issue when I turn on my own new code that I actually want to test, because it is creating large numbers of small objects quite rapidly. Many of these objects should be garbage-collected (I am creating them using the NewObject() function to create them, and most of them should also stop being referenced quite rapidly again and therefore be garbage collected), but the memory usage really increases rapidly with this code running and never goes back down again.

Normally I would expect this to be a mistake on my part, leaving the objects referenced somewhere so that they cannot be garbage collected, but given what I described above, that the same problem occurs on a smaller scale without any of my new code running, I suspect the issue may be in the Engineā€™s garbage collection itself. Also, even if I messed up my code and left references to my new objects, I would still expect them to get cleaned up when leaving play-mode.

I have also used the ā€˜memreport -fullā€™ console command, once during gameplay and once with the editor still open after leaving play-mode. In the first log (during gameplay) in the Object List I could see reasonable counts of the objects of my new classes being alive (which makes sense, since during gameplay my code is running and creating some of these objects). In the second log (after leaving play-mode), these objects of my new classes no longer showed up in the Object List (indicating that they appear to have been cleaned up), but the Process Physical Memory (both used and peak) were HIGHER than during gameplay.

  1. Is this a known issue? I have seen reports of memory leaks on answerhub, but most of them seem to be about using the editor for a long time and not necessarily about using Play mode.

  2. Does anyone have any ideas for workarounds to force my objects to get garbage collected?

Iā€™m using Unreal Engine version 4.9.2.

(Note: I previously posted the same question in the C++ section ( Memory Leak when using Play In Editor - Programming & Scripting - Unreal Engine Forums ), but in hindsight decided Bug Reports might be a better section since I suspect it is an issue in the engine code and not my custom C++ code.

hello,
im experiencing the same issue, so im kinda interested in an answer

Iā€™m still experiencing the issue described in my original post above. Iā€™ve overridden the BeginDestroy() and FinishDestroy() methods in some of the classes of which Iā€™m generating many objects to print log messages when they get called.

These log messages do indeed appear when Iā€™d expect them (either during gameplay after waiting for a while, I believe I somewhere in the past that the garbage collector runs once every minute, so thatā€™s probably the delay?), but also right away if I exit play-mode.

So, to me it seems like the garbage collector correctly identifies when these objects should be cleaned up, but the cleaning up doesnā€™t actually happen as it should, and memory usage as shown by the Windows Task Manager does not go down at all.

Hey DennisSoemers-

Iā€™ve entered a bug report (UE-22689) about the memory leak for further investigation.

Cheers

Hey,

Iā€™ve done some more experimenting, and think I know what the problem is. Iā€™ve toggled ā€œShow Frame Rate and Memoryā€ on in Editor Preferences ā†’ Miscellaneous ā†’ Performance to be able to keep track of the Memory usage and the number of Objects. First, with this on, again I saw the same issue confirmed here too; number of Objects went down once Garbage Collector kicked in, Memory usage did not.

Then I played around with the Garbage Collectorā€™s ā€œTime Between Purging Pending Kill Objectsā€ in the Project Settings. Once I set that to a much lower time (for instance, 5 seconds instead of the default of 60.0), I noticed the following;

When gameplay starts, Memory usage and number of Objects both go up rapidly. After 5 seconds, when garbage collection kicks in, Memory usage stays the same and the number of Objects goes way down. Then, as my code is still continuing to create new objects, the number of Objects grows rapidly again but Memory usage stays constant until the new number of Objects exceeds the number of Objects that were alive before the first round of Garbage Collection.

So, I hypothesize all the objects I am creating though NewObject() are placed in a TArray (object pool), of which the capacity is growing rapidly because I am creating many objects rapidly. When garbage collection kicks in, the created objects are correctly destructed, but the object poolā€™s capacity does not shrink, which means a large amount of memory remains allocated, and this is the memory usage that Iā€™m seeing which does not get freed anymore.

If this is whatā€™s happening, it means that NewObject() is not really suitable for what I want to do (rapid creation of many objects that also become eligible for destruction rapidly again), definitely not with a long delay between Garbage Collection rounds. So to test this I need to find a good alternative. Worst case I can manually use new and delete, but Iā€™m going to look for a better way first. Iā€™d be interested in any suggestions here!

1 Like

Hi, I suggest you to use a object pool, so you donā€™t need to destroy and create objects at runtime. At startup you allocate how many object you need and use it taking from the pool and after instead of destroying you can re-add them on the pool.

What I wrote in my comment to actually turned out to be correct. I solved the issue by no longer using UObject as a base class. Then I simply created my objects using the good old regular new operator, and put them in shared pointers for memory management. But your solution could be a good one too.

DennisSoemers, excellent post! Did oyu ever get this figured out? I am spawing actors and destroying them and trying to figure out what the best values to use for. ā€œTime Between Purging Pending Kill Objectsā€. Did you stay with 5 seconds or go back to 60 seconds?

Itā€™s been a few years since I worked with this, so I donā€™t really remember muchā€¦ I do see I pretty much answered as much as Iā€™d been able to figure out in my reply to the answer marked as accepted. I shifted away from using UObject as a base class for the thing I was instantiating frequently.