Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Godot leaking massive memory just by itself #82769

Closed
Sighan opened this issue Oct 4, 2023 · 11 comments · Fixed by #83096
Closed

Godot leaking massive memory just by itself #82769

Sighan opened this issue Oct 4, 2023 · 11 comments · Fixed by #83096

Comments

@Sighan
Copy link

Sighan commented Oct 4, 2023

Godot version

4.1.1 stable

System information

macOS Montery 12.5.1, 16GB RAM, Forward+ Renderer

Issue description

When running a project from godot (pressing the play button), the new window will just keep leaking memory, regardless of project size.

The Tests:
I set up a new project, added a scene and tested the following:

  • Just a single Node as the scene root, nothing else
  • Just a single Node2D as the scene root, nothing else
  • Node as Scene Root, then 20 Node2Ds as children
  • Node as scene root, then 20 Node2Ds with processing disabled as children
  • Finally another run with just a node, to see if it would yield different results as the first run with a single node (it did)

While running the tests I didn't do anything else, just kept the Godot window open in the background.

The Results:
data

I tested every configuration for at least 2 hours, some longer (except for the last one, ran out of time)

As you can see even just an empty scene in an empty project will keep growing in memory usage, my guess is indefinetly. Notably the last test (with an empty scene, containing just an empty Node) grew the fastest. Likewise the tests with 20 Node2Ds yielded similar resuts, regardless of processing being turned on or of. This leads me to believe that godot itself is leaking memory, not the nodes.

I will also attach all screenshots I took during those tests, showing my activity monitor (which is apples task manager). You can see how the memory usage changed over time. I tried to take a screenshot about every 30 minutes.

Single Node:

node-01
node-02
node-03
node-04
node-05
node-06

Single Node2D:

node2d-1
node2d-2
node2d-3
node2d-4
node2d-5
node2d-6
node2d-07
node2d-08
node2d-09
node2d-10

20 Node2Ds:

20node2ds-01
20node2ds-02
20node2ds-03
20node2ds-04
20node2ds-05

20 Node2Ds with processing turned off:

20node2ds-no-processing-01
20node2ds-no-processing-02
20node2ds-no-processing-03
20node2ds-no-processing-04
20node2ds-no-processing-05

Single Node (again after all other tests)

single-node-finale-01
single-node-finale-02
single-node-finale-03
single-node-finale-04

Steps to reproduce

Create an empty project. Create an empty scene. Run that project. Observe memory usage growing.

Minimal reproduction project

empty-game.zip

@bitsawer
Copy link
Member

bitsawer commented Oct 4, 2023

Tested on Windows, doesn't seem to leak after a pretty quick test so could be a MacOS issue. Possibly related to #62507

If you change the renderer to Compatibility (editor button on top right), does the leaking stop or change? When running the project, do the memory stats in bottom panel Debugger -> Monitors grow or stay even? Also, can you test 4.1.2 and latest 4.2 dev6? Both available in https://github.com/godotengine/godot-builds/releases/

@Sighan
Copy link
Author

Sighan commented Oct 4, 2023

I did a quick test with compability renderer, same result (memory usage starts out lower, but keeps growing regardless). Memory stats in the debugger are steady with both renderers. I will try out the other builds later today and post the results.

Edit: Both 4.1.2 and 4.2.dev6 have the same issue macOS

@lostminds
Copy link

Tested in 4.2.dev6 on a M2 Pro macOS 13.6 now and I can see the same issue using both Compatibility and Mobile renderer. Memory seems to be leaking at about 0.1MB/sec according to macOS Activity monitor, both from the run project and just the Godot Project manager when you that. If I toggle on the Update continuously editor setting I can see a similar memory increase for the editor, but not if it's disabled. The leak rate also seems to vary with update rate, as it appears to leak quicker at 120fps than 60fps.

@bruvzg
Copy link
Member

bruvzg commented Oct 10, 2023

It seems to be macOS bug, leak seem to happen inside the following calls:

[NSScreen visibleFrame]
[NSWindow isOnActiveSpace]

Both are called every frame, relevant code (visibleFrame is called inside window_get_mode):

bool DisplayServerMacOS::window_can_draw(WindowID p_window) const {
return (window_get_mode(p_window) != WINDOW_MODE_MINIMIZED) && [windows[p_window].window_object isOnActiveSpace];
}
bool DisplayServerMacOS::can_any_window_draw() const {
_THREAD_SAFE_METHOD_
for (const KeyValue<WindowID, WindowData> &E : windows) {
if ((window_get_mode(E.key) != WINDOW_MODE_MINIMIZED) && [E.value.window_object isOnActiveSpace]) {
return true;
}
}
return false;
}

@bruvzg
Copy link
Member

bruvzg commented Oct 10, 2023

Stack traces for the leaks:

__CFAllocateObject	
__NSArrayM_new	
+[NSArray array]	
-[NSConcreteMapTable allValues]	
+[_NSFullScreenSpace fullScreenSpaces]	
+[_NSFullScreenSpace visibleFullScreenSpaces]	
+[_NSFullScreenSpace visibleFullScreenSpaceOnScreen:]	
-[NSScreen _hasMenuBar:]	
-[NSScreen _layoutMenuBarHeight]	
-[NSScreen _layoutFrame]	
-[NSScreen visibleFrame]	
DisplayServerMacOS::window_get_mode(int) const	
__CFAllocateObject	
__NSSingleObjectArrayI_new	
+[NSArray arrayWithObjects:count:]	
-[NSWindow _isInSomeVisibleSpace]	
-[NSWindow isOnActiveSpace]	
DisplayServerMacOS::window_can_draw(int) const	

@lostminds
Copy link

Perhaps an option could be to use NSWindows visible and occlusionState properties instead of using the screen visibleFrame and isOnActiveSpace? They seem like a good fit to determine if the window can be drawn to, and might also let us catch some other issues like #64708

@bruvzg
Copy link
Member

bruvzg commented Oct 10, 2023

Perhaps an option could be to use NSWindows visible and occlusionState properties

This seems to be working, not sure if both isVisible and occlusionState checks are necessary (occlusionState check alone seems to be enough), will test various scenarios a bit later. But it's not leaking.

#83096

We probably can cache the value in the windowDidChangeOcclusionState: instead of calling it every frame as well.

@akien-mga akien-mga added this to the 4.2 milestone Oct 10, 2023
@lostminds
Copy link

lostminds commented Oct 10, 2023

not sure if both isVisible and occlusionState checks are necessary

Yes, it seems occlusionState alone will be sufficient, I wasn't sure it would be updated correctly for hidden windows but after checking some more now it seems like it is.

@joeharris14
Copy link

I am still experiencing this memory leak, using Compatability on Macbook Pro:
image
It grows until it crashes my game on a simple game (memory read as 30gb on activity monitor). Tried testing it with an even simpler game and it grows slower but still continuously.

Exporting the game had the same issue.

But when I switched to Forward+ it resolved and now only uses 1gb of memory according to activity

I'm on: monitor.v4.2.stable.official [46dc277]

@bruvzg
Copy link
Member

bruvzg commented Dec 15, 2023

I am still experiencing this memory leak, using Compatability on Macbook Pro
I'm on: monitor.v4.2.stable.official

This is not related leak, ANGLE Compatibility backend is broken and causing it, ANGLE was disabled in 4.2.1.

If you want to use Compatibility renderer in 4.2 (why?), go to the Project settingsRenderingGL CompatibilityDriver.macos and set it to opengl3.

@PMach17
Copy link

PMach17 commented Dec 15, 2023

I am still experiencing this memory leak, using Compatability on Macbook Pro
I'm on: monitor.v4.2.stable.official

This is not related leak, ANGLE Compatibility backend is broken and causing it, ANGLE was disabled in 4.2.1.

If you want to use Compatibility renderer in 4.2 (why?), go to the Project settingsRenderingGL CompatibilityDriver.macos and set it to opengl3.

I'm experiencing similar problem. Even with simple scene with timer, label and a script that changes the label every 10s.

screenshot1

I tried this and it did help to a point where it didn't stop the growth of memory usage but it did slow it down to around 0.1Mb/5s. Before changing this it was about 10Mb/5s.

The same project is working fine on Windows - no leaks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment