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

[3.x] Repeatedly calling JavaScript->Godot callback causes infinite memory consumption increases. #81005

Closed
SysError99 opened this issue Aug 25, 2023 · 5 comments

Comments

@SysError99
Copy link
Contributor

Godot version

3.5.2-stable

System information

Linux 6.1.44-1-MANJARO

Issue description

Calling Godot Callback from JavaScript causes memory consumption to go higher than expected, even with empty project that does nothing but keep calling Godot's callback via JavaScript or Godot's JavaScript interface.

Steps to reproduce

  1. On the game's script. create Godot Callback for JavaScript.
  2. Bind the callback to reachable JavaScript interface (window in particular)
  3. Call the callback by either using native JavaScript code on browser, or from Godot's JavaScript interface in high amount of times per second (in the reproduce, ~300 times per second).
  4. Memory consumption in JavaScript's heap keeps increasing.

This happens on both Firefox and Chromium-based browsers, WebKit is untested.
image
image

Minimal reproduction project

Archive.zip

@SysError99 SysError99 changed the title [3.x] Repeatedly calling Godot callback from JavaScript causes infinite memory consumption increasing. [3.x] Repeatedly calling Godot callback from JavaScript causes infinite memory consumption increases. Aug 25, 2023
@SysError99 SysError99 changed the title [3.x] Repeatedly calling Godot callback from JavaScript causes infinite memory consumption increases. [3.x] Repeatedly calling JavaScript->Godot callback causes infinite memory consumption increases. Aug 25, 2023
@SysError99
Copy link
Contributor Author

I left it for awhile and it still keeps increasing, on an empty project.
image

@Chaosus Chaosus added this to the 3.x milestone Aug 27, 2023
@SysError99
Copy link
Contributor Author

Further testing on bleeding edge 3.x branch found out that after disabling internal call line from JavaScriptObjectImpl::_callback, its memory footprint grows much less, however it's still growing. Suggesting that something is wrong in some sort of memory reserving mechanism in this function.

image
image

Since my experience with C++ and manual memory management is still very limited, I cannot analyse the problem any further, but I will try my test to figure out the culprit.

@SysError99
Copy link
Contributor Author

Looks like the problem sits down somewhere at the callback function, since repeatedly calling it even with zero arguments still pile the array up. GodotJsWrapper seemed to not free the arg passing array on callback for some reason.

Clipboard - August 28, 2023 7_25 PM

@SysError99
Copy link
Contributor Author

As for heads up, I found a culprit that GodotJsWrapper never frees up freshy-created and 'proxied' array in the callback. I wrapped the array with GodotJSWrapper.MyProxy instead of GodotJSWrapper.get_proixed and then free out the proxy manually after callback execution.

image

Still, I can't be too sure if this is the 'right' solution since JS GC can be wonky to deal with especially when interfacing with WASM. However, as for my test bench code it doesn't ruin anything and value still gets copied and referenced properly.

This message is mirrored from Godot Contributors Chat.

@akien-mga
Copy link
Member

Fixed by #81090.

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

No branches or pull requests

4 participants