godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.6k stars 21.28k forks source link

Apparent memory leak when an error occurs after allocating a PackedArray in the same scope #78526

Open pi4erd opened 1 year ago

pi4erd commented 1 year ago

Godot version

Reproducible in: v4.0.3.stable.official [5222a99f5], v4.3.rc1, v4.3.rc2.official [3978628c6]

System information

Windows 11 - Godot v4.0.3.stable.official - Vulkan (Forward+)

Issue description

If an error is thrown in the same scope after we have allocated a packed array of specific size (up until 1048568 bytes) Godot leaks memory even after the game is closed. The leaked memory stays until Godot is restarted.

Also memory leaked seems unreasonably big for an array 1MiB in size.

Normal usage: image

After a few runs (with errors): image

Same thing on Arch Linux (KDE, v4.3.stable.mono.arch_linux): image

After 1 single run: image

The game is not running in either of those pictures. The memory usage stays on the same level until Godot is restarted.

If the code doesn't throw any errors, the memory is cleaned up properly.

Steps to reproduce

  1. Create any scene with any node
  2. Attach a script from MRP to any node
  3. Wait until it crashes
  4. Check memory usage in system monitor
  5. Run a few times
  6. Notice that it steadily goes up

Minimal reproduction project

real_minimal_repro.zip

pi4erd commented 4 months ago

Sorry for a long period of inactivity. Just tested on Godot v4.3.rc1 - macOS 15.0.0 - Vulkan (Forward+) - integrated Apple M2 - Apple M2 (8 Threads), the issue seems to persist with the reproduction project in the same way as on Windows. I am going to test it on Linux when I can. I made the code runnable without errors, and in the end the memory doesn't persist after successful runs.

pi4erd commented 4 months ago

Just tested Mobile and Compatibility renderers. Mobile has the same issue but Compatibility clears up everything properly on the same setup. Could it be an issue in Vulkan backend somehow?

Calinou commented 4 months ago

I made the code runnable without errors, and in the end the memory doesn't persist after successful runs.

Can you update the MRP in the original post accordingly? Thanks in advance :slightly_smiling_face:

pi4erd commented 4 months ago

Can you update the MRP in the original post accordingly

I'm sorry, but do I understand correctly that I should upload successfully run code as well? If that's the case, I will do it shortly.

pi4erd commented 4 months ago

I uploaded a successful MRP, you can check. Just in case I tested beforehand, there doesn't seem to be any signs of memory leak when the project is run successfully.

pi4erd commented 3 months ago

After a few tests I've discovered that this memory leak has nothing to do with rendering. Memory leak happens if we allocate a packed array not exceeding the size of 1048568 bytes and any error happens before the end of the scope (presumably before the array is destroyed). If the array size is any bigger than this number, then there is no apparent leak even if an error occurs. I am going to debug the engine later to check if I can find anything of note.

I am still confused that Compatibility didn't leak memory for me before. I can't reproduce it anymore, my script leaks memory even in Compatibility mode.