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

[Lua] - Locally scoped variables not gc-ed #1030

Open
1 task
AleksandarEri opened this issue May 24, 2023 · 3 comments
Open
1 task

[Lua] - Locally scoped variables not gc-ed #1030

AleksandarEri opened this issue May 24, 2023 · 3 comments

Comments

@AleksandarEri
Copy link

Description

Using locally scoped variables in lua runtime are not being released correctly, thus memory of table keeps increasing overtime.

Provided Code

`local d = {}
do
local a = {}
local b = 1
d['a'] = a
d['b'] = b
end

for k, v in pairs(d) do
print(k, v)
end
collectgarbage()
print("=====================")
for k, v in pairs(d) do
print(k, v)
end`

Steps to Reproduce

  1. Run provided code in stock lua 5.1 interpreter in VS Code or by using ZeroBrane
  2. Observe that after collectgarbage call, table contains only key b
  3. Run same code in Docker version of the nakama ( Windows ( Docker Desktop ))
  4. Notice that after collectgarbage call, table contains both keys a,b and a hasn't been released.

Expected Result

Inner locally scoped variables are released as soon as they are no longer used and memory is retained correctly.

Actual Result

Memory persists and inner locally scoped variables are kept.

Context

  • Unity - 2021.3.23f

Your Environment

  • Nakama: 3.16.0
  • Database: Cockroach 20.2
  • Environment name and version: Docker Desktop
  • Operating System and version: Win 10
@deflinhec
Copy link
Contributor

I think this is related to yuin/gopher-lua, not specific to Nakama Server Framework. According to yuin/gopher-lua#miscellaneous-notes there are slightly differences between yuin/gopher-lua and lua with the collectgarbage method.

Just a few months ago, there was an context leak with coroutine lib yuin/gopher-lua#438, which I have already fixed and been merged yuin/gopher-lua@8ee9c41. Base of my observation the memory will be freed at some point of time.

@AleksandarEri
Copy link
Author

@deflinhec yes, this is specific to yuin/gopher-lua and partly to nakama and how it is using this lib. We could maybe close this but it would be than nice to extend lua docs and add best pratices when it comes to developing in sad runtime on what to take extra care. Like here for memory releasing time as it could impact session lenght/number of matches performed until "used" memory has been released.

@ig-gh
Copy link

ig-gh commented Dec 1, 2024

@AleksandarEri
When the do ... end block ends, the local variables a and b go out of scope.
Since a and b are local, their names are no longer accessible. However:
The table that a referred to is still referenced by d['a'].
The value 1 is primitive and is still stored in d['b'].

Lua's garbage collector works by reference counting. As long as there is a reference to an object, it will not be garbage collected.

set d['a'] = nil d['b'] = nil for requested result

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

No branches or pull requests

3 participants