godotengine / godotengine/godot
Big memory usage when too many `Area2D`s collision pair's
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### Tested versions
- Reproducible in: v4.3.stable.mono.official [77dcf97d8]
### System information
Windows 10 - Vulkan(Foward+) - Nvidea 1070
### Issue description
~~There seems to be a big memory leak when spawning too many **Area2D's** too fast.~~
~~This leak also happens when using **JoltPhysics2D**.~~
**Edit:**
> Area2D uses too much memory when there are a lot of collision pairs.
> Comparing them with CharacterBody2D, they use more then 3 times the memory
> CharacterBody2D: 500k pairs | **279Mb** memory
> Area2D: 500k pairs | **906 MB**
> See: https://github.com/godotengine/godot/issues/100600#issuecomment-2557028345
Here it is **working normally**, spawning moving bullets.
Because the FPS drops, it spawns slower and doesn't leak memory.
**298 MB** static memory after 1000 `Area2D` spawned.
And 132k collision pairs
https://github.com/user-attachments/assets/cfc1854f-aad0-4e1f-a869-765d8605d369
---
And here it is **leaking memory**.
When I try to spawn non-moving bullets.
It spawns them very fast. But the memory goes up very fast.
**906 MB** static memory after 1000 `Area2D` spawned.
And 500k collision pairs
https://github.com/user-attachments/assets/8a5a6e4e-4087-443e-8502-776735114c51
I've also tested the same with Node2D and Character2D but they work fine.
This only happens with `Area2D`s
---
PS: Its strange that the FPS in the MRP tanks to 4 FPS so fast with only 500+ nodes too.
I was getting a stable 40 frames with 2k bullets on my other project.
I thought maybe it was because I was spawning them there with coroutines, but the performance on the MRP stays at 4 FPS even after it finishes spawning the 1k bullets.
So something else might be wrong.
---
As a side-note, when the memory-leak example finishes spawning my computer starts screeching.
Which I'm guessing is my old HDD asking for help.
This also rarely happens here when windows starts indexing or reading tons of small files.
Not sure this is related but a fun tidbit.
### Steps to reproduce
MRP:
- Run the MRP project
- Look at the monitor tab static memory
Expected: NOT to increase so much. Expected around 200-300 MBs.
Result: static memory 900+Mbs
Non-MRP:
- Spawn 5 Area2D's each frame
Expected: Normal static memory increase.
Result: static memory increase very fast.
### Minimal reproduction project (MRP)
[MRP_memory-leak-physics.zip](https://github.com/user-attachments/files/18196183/MRP_memory-leak-physics.zip)
Contributor guide
Research direction
Run the attached MRP and reproduce the comparison between 1,000 Area2D nodes with roughly 132k versus 500k collision pairs, watching static memory and FPS. Compare the Area2D results with CharacterBody2D and the stated 200–300 MB expectation. Done means the excessive memory growth and severe performance drop are addressed for the reported collision-pair scenario.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, godot
- Domain
- game-dev, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100